mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-19 14:04:08 +01:00
refac: drop plantTypes from farms
This commit is contained in:
parent
3df25a7c2f
commit
dd94c24918
@ -7,7 +7,6 @@ import (
|
||||
"github.com/danielgtaylor/huma/v2"
|
||||
"github.com/forfarm/backend/internal/domain"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (a *api) registerFarmRoutes(_ chi.Router, api huma.API) {
|
||||
@ -50,7 +49,6 @@ type CreateFarmInput struct {
|
||||
Lat []float64 `json:"lat"`
|
||||
Lon []float64 `json:"lon"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
PlantTypes []uuid.UUID `json:"plant_types"`
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,7 +64,6 @@ func (a *api) createFarmHandler(ctx context.Context, input *CreateFarmInput) (*C
|
||||
Lat: input.Body.Lat,
|
||||
Lon: input.Body.Lon,
|
||||
OwnerID: input.Body.OwnerID,
|
||||
PlantTypes: input.Body.PlantTypes,
|
||||
}
|
||||
|
||||
err := a.farmRepo.CreateOrUpdate(ctx, farm)
|
||||
|
||||
@ -2,9 +2,9 @@ package domain
|
||||
|
||||
import (
|
||||
"context"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
"github.com/google/uuid"
|
||||
"time"
|
||||
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
type Farm struct {
|
||||
@ -15,7 +15,6 @@ type Farm struct {
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
OwnerID string
|
||||
PlantTypes []uuid.UUID
|
||||
}
|
||||
|
||||
func (f *Farm) Validate() error {
|
||||
|
||||
@ -2,10 +2,10 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/forfarm/backend/internal/domain"
|
||||
"github.com/google/uuid"
|
||||
"github.com/lib/pq"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type postgresFarmRepository struct {
|
||||
@ -26,7 +26,6 @@ func (p *postgresFarmRepository) fetch(ctx context.Context, query string, args .
|
||||
var farms []domain.Farm
|
||||
for rows.Next() {
|
||||
var f domain.Farm
|
||||
var plantTypes pq.StringArray
|
||||
if err := rows.Scan(
|
||||
&f.UUID,
|
||||
&f.Name,
|
||||
@ -35,19 +34,10 @@ func (p *postgresFarmRepository) fetch(ctx context.Context, query string, args .
|
||||
&f.CreatedAt,
|
||||
&f.UpdatedAt,
|
||||
&f.OwnerID,
|
||||
&plantTypes,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, plantTypeStr := range plantTypes {
|
||||
plantTypeUUID, err := uuid.Parse(plantTypeStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.PlantTypes = append(f.PlantTypes, plantTypeUUID)
|
||||
}
|
||||
|
||||
farms = append(farms, f)
|
||||
}
|
||||
return farms, nil
|
||||
@ -83,11 +73,6 @@ func (p *postgresFarmRepository) CreateOrUpdate(ctx context.Context, f *domain.F
|
||||
f.UUID = uuid.New().String()
|
||||
}
|
||||
|
||||
plantTypes := make([]string, len(f.PlantTypes))
|
||||
for i, pt := range f.PlantTypes {
|
||||
plantTypes[i] = pt.String()
|
||||
}
|
||||
|
||||
query := `
|
||||
INSERT INTO farms (uuid, name, lat, lon, created_at, updated_at, owner_id, plant_types)
|
||||
VALUES ($1, $2, $3, $4, NOW(), NOW(), $5, $6)
|
||||
@ -108,7 +93,6 @@ func (p *postgresFarmRepository) CreateOrUpdate(ctx context.Context, f *domain.F
|
||||
f.Lat,
|
||||
f.Lon,
|
||||
f.OwnerID,
|
||||
pq.StringArray(plantTypes),
|
||||
).Scan(&f.UUID, &f.CreatedAt, &f.UpdatedAt)
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,2 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE farms DROP COLUMN plant_types;
|
||||
Loading…
Reference in New Issue
Block a user