forked from mirrors/homebox
fix: refactoring errors (#359)
* #352 - require one date to be set to save * fix many type regressions
This commit is contained in:
parent
4a8ba6231d
commit
97fb94d231
22 changed files with 164 additions and 289 deletions
|
@ -61,6 +61,7 @@ func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration)
|
|||
|
||||
switch data.GroupToken {
|
||||
case "":
|
||||
log.Debug().Msg("creating new group")
|
||||
creatingGroup = true
|
||||
group, err = svc.repos.Groups.GroupCreate(ctx, "Home")
|
||||
if err != nil {
|
||||
|
@ -68,6 +69,7 @@ func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration)
|
|||
return repo.UserOut{}, err
|
||||
}
|
||||
default:
|
||||
log.Debug().Msg("joining existing group")
|
||||
token, err = svc.repos.Groups.InvitationGet(ctx, hasher.HashToken(data.GroupToken))
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Failed to get invitation token")
|
||||
|
@ -94,14 +96,14 @@ func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration)
|
|||
// Create the default labels and locations for the group.
|
||||
if creatingGroup {
|
||||
for _, label := range defaultLabels() {
|
||||
_, err := svc.repos.Labels.Create(ctx, group.ID, label)
|
||||
_, err := svc.repos.Labels.Create(ctx, usr.GroupID, label)
|
||||
if err != nil {
|
||||
return repo.UserOut{}, err
|
||||
}
|
||||
}
|
||||
|
||||
for _, location := range defaultLocations() {
|
||||
_, err := svc.repos.Locations.Create(ctx, group.ID, location)
|
||||
_, err := svc.repos.Locations.Create(ctx, usr.GroupID, location)
|
||||
if err != nil {
|
||||
return repo.UserOut{}, err
|
||||
}
|
||||
|
|
|
@ -234,11 +234,17 @@ func (r *GroupRepository) StatsGroup(ctx context.Context, GID uuid.UUID) (GroupS
|
|||
var stats GroupStatistics
|
||||
row := r.db.Sql().QueryRowContext(ctx, q, GID, GID, GID, GID, GID, GID)
|
||||
|
||||
err := row.Scan(&stats.TotalUsers, &stats.TotalItems, &stats.TotalLocations, &stats.TotalLabels, &stats.TotalItemPrice, &stats.TotalWithWarranty)
|
||||
var maybeTotalItemPrice *float64
|
||||
var maybeTotalWithWarranty *int
|
||||
|
||||
err := row.Scan(&stats.TotalUsers, &stats.TotalItems, &stats.TotalLocations, &stats.TotalLabels, &maybeTotalItemPrice, &maybeTotalWithWarranty)
|
||||
if err != nil {
|
||||
return GroupStatistics{}, err
|
||||
}
|
||||
|
||||
stats.TotalItemPrice = orDefault(maybeTotalItemPrice, 0)
|
||||
stats.TotalWithWarranty = orDefault(maybeTotalWithWarranty, 0)
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ type (
|
|||
ImportRef string `json:"-"`
|
||||
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
|
||||
Name string `json:"name" validate:"required,min=1,max=255"`
|
||||
Description string `json:"description" validate:"required,min=1,max=1000"`
|
||||
Description string `json:"description" validate:"max=1000"`
|
||||
AssetID AssetID `json:"-"`
|
||||
|
||||
// Edges
|
||||
|
|
|
@ -2,6 +2,7 @@ package repo
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
@ -18,15 +19,38 @@ import (
|
|||
type MaintenanceEntryRepository struct {
|
||||
db *ent.Client
|
||||
}
|
||||
type (
|
||||
MaintenanceEntryCreate struct {
|
||||
CompletedDate types.Date `json:"completedDate"`
|
||||
ScheduledDate types.Date `json:"scheduledDate"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Cost float64 `json:"cost,string"`
|
||||
}
|
||||
|
||||
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"`
|
||||
ScheduledDate types.Date `json:"scheduledDate"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
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 {
|
||||
ID uuid.UUID `json:"id"`
|
||||
CompletedDate types.Date `json:"completedDate"`
|
||||
|
@ -36,14 +60,6 @@ type (
|
|||
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 {
|
||||
ItemID uuid.UUID `json:"itemId"`
|
||||
CostAverage float64 `json:"costAverage"`
|
||||
|
|
|
@ -29,20 +29,31 @@ func DecodeQuery[T any](r *http.Request) (T, error) {
|
|||
return v, nil
|
||||
}
|
||||
|
||||
type Validator interface {
|
||||
Validate() 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 {
|
||||
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 {
|
||||
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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue