Seed provenance + inventory: source links and seed lots (#50) (#64)
Build image / build-and-push (push) Successful in 8s
Build image / build-and-push (push) Successful in 8s
Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #64.
This commit is contained in:
@@ -18,6 +18,7 @@ const (
|
||||
minPlantSpacingCM = 0.1
|
||||
maxPlantSpacingCM = 10_000 // 100 m — headroom for large trees/shrubs
|
||||
maxDaysToMaturity = 3650 // ~10 years
|
||||
maxPlantVendorLen = 200
|
||||
)
|
||||
|
||||
// plantCategories is the set of valid category enum values (mirrors the schema
|
||||
@@ -40,7 +41,11 @@ type PlantInput struct {
|
||||
Color string
|
||||
Icon string
|
||||
DaysToMaturity *int
|
||||
Notes string
|
||||
// Where this variety came from — the page you'd go back to to buy it again.
|
||||
// What you actually bought, and how much is left, is a SeedLot instead.
|
||||
SourceURL string
|
||||
Vendor string
|
||||
Notes string
|
||||
}
|
||||
|
||||
// PlantPatch is a partial update: nil fields are left unchanged. DaysToMaturity
|
||||
@@ -54,6 +59,8 @@ type PlantPatch struct {
|
||||
Icon *string
|
||||
SetDays bool
|
||||
DaysToMaturity *int
|
||||
SourceURL *string
|
||||
Vendor *string
|
||||
Notes *string
|
||||
}
|
||||
|
||||
@@ -73,6 +80,8 @@ func (s *Service) CreatePlant(ctx context.Context, actorID int64, in PlantInput)
|
||||
Color: in.Color,
|
||||
Icon: strings.TrimSpace(in.Icon),
|
||||
DaysToMaturity: in.DaysToMaturity,
|
||||
SourceURL: strings.TrimSpace(in.SourceURL),
|
||||
Vendor: strings.TrimSpace(in.Vendor),
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
}
|
||||
if err := finalizePlant(p); err != nil {
|
||||
@@ -121,6 +130,12 @@ func (s *Service) UpdatePlant(ctx context.Context, actorID, plantID int64, patch
|
||||
// planting (even a removed one — the FK is RESTRICT) is refused with
|
||||
// ErrPlantInUse rather than orphaning history; the client clones-and-edits or
|
||||
// clears the plantings first.
|
||||
//
|
||||
// Seed lots are refused for a different reason: seed_lots.plant_id is ON DELETE
|
||||
// CASCADE, so without this check deleting a plant would silently destroy the
|
||||
// purchase records attached to it — vendor, cost, germination rate, all gone
|
||||
// with no warning. Refusing lets the owner delete the lots deliberately if that
|
||||
// is really what they meant.
|
||||
func (s *Service) DeletePlant(ctx context.Context, actorID, plantID int64) error {
|
||||
if _, err := s.writablePlant(ctx, actorID, plantID); err != nil {
|
||||
return err
|
||||
@@ -132,6 +147,13 @@ func (s *Service) DeletePlant(ctx context.Context, actorID, plantID int64) error
|
||||
if n > 0 {
|
||||
return domain.ErrPlantInUse
|
||||
}
|
||||
lots, err := s.store.CountSeedLotsForPlant(ctx, plantID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if lots > 0 {
|
||||
return domain.ErrPlantInUse
|
||||
}
|
||||
return s.store.DeletePlant(ctx, plantID)
|
||||
}
|
||||
|
||||
@@ -155,6 +177,12 @@ func applyPlantPatch(p *domain.Plant, patch PlantPatch) {
|
||||
if patch.SetDays {
|
||||
p.DaysToMaturity = patch.DaysToMaturity
|
||||
}
|
||||
if patch.SourceURL != nil {
|
||||
p.SourceURL = strings.TrimSpace(*patch.SourceURL)
|
||||
}
|
||||
if patch.Vendor != nil {
|
||||
p.Vendor = strings.TrimSpace(*patch.Vendor)
|
||||
}
|
||||
if patch.Notes != nil {
|
||||
p.Notes = strings.TrimSpace(*patch.Notes)
|
||||
}
|
||||
@@ -184,5 +212,13 @@ func finalizePlant(p *domain.Plant) error {
|
||||
if len(p.Notes) > maxPlantNotesLen {
|
||||
return domain.ErrInvalidInput
|
||||
}
|
||||
if len(p.Vendor) > maxPlantVendorLen {
|
||||
return domain.ErrInvalidInput
|
||||
}
|
||||
// Rendered as a clickable link, so the scheme check is load-bearing (see
|
||||
// validSourceURL) rather than tidiness.
|
||||
if !validSourceURL(p.SourceURL) {
|
||||
return domain.ErrInvalidInput
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user