mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-06 01:20:31 +00:00
#352 - require one date to be set to save
This commit is contained in:
parent
4a8ba6231d
commit
d42f1fc3cf
2 changed files with 49 additions and 22 deletions
|
@ -2,6 +2,7 @@ package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -18,15 +19,38 @@ import (
|
||||||
type MaintenanceEntryRepository struct {
|
type MaintenanceEntryRepository struct {
|
||||||
db *ent.Client
|
db *ent.Client
|
||||||
}
|
}
|
||||||
type (
|
|
||||||
MaintenanceEntryCreate struct {
|
type MaintenanceEntryCreate struct {
|
||||||
|
CompletedDate types.Date `json:"completedDate"`
|
||||||
|
ScheduledDate types.Date `json:"scheduledDate"`
|
||||||
|
Name string `json:"name" validate:"required"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Cost float64 `json:"cost,string"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc MaintenanceEntryCreate) Validate() error {
|
||||||
|
if mc.CompletedDate.Time().IsZero() && mc.ScheduledDate.Time().IsZero() {
|
||||||
|
return errors.New("either completedDate or scheduledDate must be set")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type MaintenanceEntryUpdate struct {
|
||||||
CompletedDate types.Date `json:"completedDate"`
|
CompletedDate types.Date `json:"completedDate"`
|
||||||
ScheduledDate types.Date `json:"scheduledDate"`
|
ScheduledDate types.Date `json:"scheduledDate"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Cost float64 `json:"cost,string"`
|
Cost float64 `json:"cost,string"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mu MaintenanceEntryUpdate) Validate() error {
|
||||||
|
if mu.CompletedDate.Time().IsZero() && mu.ScheduledDate.Time().IsZero() {
|
||||||
|
return errors.New("either completedDate or scheduledDate must be set")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type (
|
||||||
MaintenanceEntry struct {
|
MaintenanceEntry struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
CompletedDate types.Date `json:"completedDate"`
|
CompletedDate types.Date `json:"completedDate"`
|
||||||
|
@ -36,14 +60,6 @@ type (
|
||||||
Cost float64 `json:"cost,string"`
|
Cost float64 `json:"cost,string"`
|
||||||
}
|
}
|
||||||
|
|
||||||
MaintenanceEntryUpdate struct {
|
|
||||||
CompletedDate types.Date `json:"completedDate"`
|
|
||||||
ScheduledDate types.Date `json:"scheduledDate"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
Cost float64 `json:"cost,string"`
|
|
||||||
}
|
|
||||||
|
|
||||||
MaintenanceLog struct {
|
MaintenanceLog struct {
|
||||||
ItemID uuid.UUID `json:"itemId"`
|
ItemID uuid.UUID `json:"itemId"`
|
||||||
CostAverage float64 `json:"costAverage"`
|
CostAverage float64 `json:"costAverage"`
|
||||||
|
|
|
@ -29,20 +29,31 @@ func DecodeQuery[T any](r *http.Request) (T, error) {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Validator interface {
|
||||||
|
Validate() error
|
||||||
|
}
|
||||||
|
|
||||||
func DecodeBody[T any](r *http.Request) (T, error) {
|
func DecodeBody[T any](r *http.Request) (T, error) {
|
||||||
var v T
|
var val T
|
||||||
|
|
||||||
err := server.Decode(r, &v)
|
err := server.Decode(r, &val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return v, errors.Wrap(err, "body decoding error")
|
return val, errors.Wrap(err, "body decoding error")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = validate.Check(v)
|
err = validate.Check(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return v, errors.Wrap(err, "validation error")
|
return val, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, nil
|
if v, ok := any(val).(Validator); ok {
|
||||||
|
err = v.Validate()
|
||||||
|
if err != nil {
|
||||||
|
return val, errors.Wrap(err, "validation error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func RouteUUID(r *http.Request, key string) (uuid.UUID, error) {
|
func RouteUUID(r *http.Request, key string) (uuid.UUID, error) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue