refac: drop plantTypes from farms

This commit is contained in:
Sosokker 2025-02-14 06:40:26 +07:00
parent 3df25a7c2f
commit dd94c24918
4 changed files with 21 additions and 39 deletions

View File

@ -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) {
@ -46,11 +45,10 @@ func (a *api) registerFarmRoutes(_ chi.Router, api huma.API) {
type CreateFarmInput struct {
Header string `header:"Authorization" required:"true" example:"Bearer token"`
Body struct {
Name string `json:"name"`
Lat []float64 `json:"lat"`
Lon []float64 `json:"lon"`
OwnerID string `json:"owner_id"`
PlantTypes []uuid.UUID `json:"plant_types"`
Name string `json:"name"`
Lat []float64 `json:"lat"`
Lon []float64 `json:"lon"`
OwnerID string `json:"owner_id"`
}
}
@ -62,11 +60,10 @@ type CreateFarmOutput struct {
func (a *api) createFarmHandler(ctx context.Context, input *CreateFarmInput) (*CreateFarmOutput, error) {
farm := &domain.Farm{
Name: input.Body.Name,
Lat: input.Body.Lat,
Lon: input.Body.Lon,
OwnerID: input.Body.OwnerID,
PlantTypes: input.Body.PlantTypes,
Name: input.Body.Name,
Lat: input.Body.Lat,
Lon: input.Body.Lon,
OwnerID: input.Body.OwnerID,
}
err := a.farmRepo.CreateOrUpdate(ctx, farm)

View File

@ -2,20 +2,19 @@ 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 {
UUID string
Name string
Lat []float64
Lon []float64
CreatedAt time.Time
UpdatedAt time.Time
OwnerID string
PlantTypes []uuid.UUID
UUID string
Name string
Lat []float64
Lon []float64
CreatedAt time.Time
UpdatedAt time.Time
OwnerID string
}
func (f *Farm) Validate() error {

View File

@ -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)
}

View File

@ -0,0 +1,2 @@
-- +goose Up
ALTER TABLE farms DROP COLUMN plant_types;