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/danielgtaylor/huma/v2"
|
||||||
"github.com/forfarm/backend/internal/domain"
|
"github.com/forfarm/backend/internal/domain"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *api) registerFarmRoutes(_ chi.Router, api huma.API) {
|
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 {
|
type CreateFarmInput struct {
|
||||||
Header string `header:"Authorization" required:"true" example:"Bearer token"`
|
Header string `header:"Authorization" required:"true" example:"Bearer token"`
|
||||||
Body struct {
|
Body struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Lat []float64 `json:"lat"`
|
Lat []float64 `json:"lat"`
|
||||||
Lon []float64 `json:"lon"`
|
Lon []float64 `json:"lon"`
|
||||||
OwnerID string `json:"owner_id"`
|
OwnerID string `json:"owner_id"`
|
||||||
PlantTypes []uuid.UUID `json:"plant_types"`
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,11 +60,10 @@ type CreateFarmOutput struct {
|
|||||||
|
|
||||||
func (a *api) createFarmHandler(ctx context.Context, input *CreateFarmInput) (*CreateFarmOutput, error) {
|
func (a *api) createFarmHandler(ctx context.Context, input *CreateFarmInput) (*CreateFarmOutput, error) {
|
||||||
farm := &domain.Farm{
|
farm := &domain.Farm{
|
||||||
Name: input.Body.Name,
|
Name: input.Body.Name,
|
||||||
Lat: input.Body.Lat,
|
Lat: input.Body.Lat,
|
||||||
Lon: input.Body.Lon,
|
Lon: input.Body.Lon,
|
||||||
OwnerID: input.Body.OwnerID,
|
OwnerID: input.Body.OwnerID,
|
||||||
PlantTypes: input.Body.PlantTypes,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := a.farmRepo.CreateOrUpdate(ctx, farm)
|
err := a.farmRepo.CreateOrUpdate(ctx, farm)
|
||||||
|
|||||||
@ -2,20 +2,19 @@ package domain
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Farm struct {
|
type Farm struct {
|
||||||
UUID string
|
UUID string
|
||||||
Name string
|
Name string
|
||||||
Lat []float64
|
Lat []float64
|
||||||
Lon []float64
|
Lon []float64
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
OwnerID string
|
OwnerID string
|
||||||
PlantTypes []uuid.UUID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Farm) Validate() error {
|
func (f *Farm) Validate() error {
|
||||||
|
|||||||
@ -2,10 +2,10 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/forfarm/backend/internal/domain"
|
"github.com/forfarm/backend/internal/domain"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/lib/pq"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type postgresFarmRepository struct {
|
type postgresFarmRepository struct {
|
||||||
@ -26,7 +26,6 @@ func (p *postgresFarmRepository) fetch(ctx context.Context, query string, args .
|
|||||||
var farms []domain.Farm
|
var farms []domain.Farm
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var f domain.Farm
|
var f domain.Farm
|
||||||
var plantTypes pq.StringArray
|
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&f.UUID,
|
&f.UUID,
|
||||||
&f.Name,
|
&f.Name,
|
||||||
@ -35,19 +34,10 @@ func (p *postgresFarmRepository) fetch(ctx context.Context, query string, args .
|
|||||||
&f.CreatedAt,
|
&f.CreatedAt,
|
||||||
&f.UpdatedAt,
|
&f.UpdatedAt,
|
||||||
&f.OwnerID,
|
&f.OwnerID,
|
||||||
&plantTypes,
|
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
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)
|
farms = append(farms, f)
|
||||||
}
|
}
|
||||||
return farms, nil
|
return farms, nil
|
||||||
@ -83,11 +73,6 @@ func (p *postgresFarmRepository) CreateOrUpdate(ctx context.Context, f *domain.F
|
|||||||
f.UUID = uuid.New().String()
|
f.UUID = uuid.New().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
plantTypes := make([]string, len(f.PlantTypes))
|
|
||||||
for i, pt := range f.PlantTypes {
|
|
||||||
plantTypes[i] = pt.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO farms (uuid, name, lat, lon, created_at, updated_at, owner_id, plant_types)
|
INSERT INTO farms (uuid, name, lat, lon, created_at, updated_at, owner_id, plant_types)
|
||||||
VALUES ($1, $2, $3, $4, NOW(), NOW(), $5, $6)
|
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.Lat,
|
||||||
f.Lon,
|
f.Lon,
|
||||||
f.OwnerID,
|
f.OwnerID,
|
||||||
pq.StringArray(plantTypes),
|
|
||||||
).Scan(&f.UUID, &f.CreatedAt, &f.UpdatedAt)
|
).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