diff --git a/backend/internal/domain/cropland.go b/backend/internal/domain/cropland.go index 17a7192..984e6e4 100644 --- a/backend/internal/domain/cropland.go +++ b/backend/internal/domain/cropland.go @@ -32,5 +32,5 @@ func (c *Cropland) Validate() error { type CroplandRepository interface { GetByID(context.Context, string) (Cropland, error) CreateOrUpdate(context.Context, *Cropland) error - // Delete(context.Context, string) error + Delete(context.Context, string) error } diff --git a/backend/internal/domain/farm.go b/backend/internal/domain/farm.go index 5bc1154..1dd2aaf 100644 --- a/backend/internal/domain/farm.go +++ b/backend/internal/domain/farm.go @@ -29,5 +29,5 @@ func (f *Farm) Validate() error { type FarmRepository interface { GetByID(context.Context, string) (Farm, error) CreateOrUpdate(context.Context, *Farm) error - // Delete(context.Context, string) error + Delete(context.Context, string) error } diff --git a/backend/internal/repository/postgres_cropland.go b/backend/internal/repository/postgres_cropland.go new file mode 100644 index 0000000..7cc48f8 --- /dev/null +++ b/backend/internal/repository/postgres_cropland.go @@ -0,0 +1,111 @@ +package repository + +import ( + "context" + "strings" + + "github.com/google/uuid" + + "github.com/forfarm/backend/internal/domain" +) + +type postgresCroplandRepository struct { + conn Connection +} + +func NewPostgresCropland(conn Connection) domain.CroplandRepository { + return &postgresCroplandRepository{conn: conn} +} + +func (p *postgresCroplandRepository) fetch(ctx context.Context, query string, args ...interface{}) ([]domain.Cropland, error) { + rows, err := p.conn.Query(ctx, query, args...) + if err != nil { + return nil, err + } + defer rows.Close() + + var croplands []domain.Cropland + for rows.Next() { + var c domain.Cropland + if err := rows.Scan( + &c.UUID, + &c.Name, + &c.Status, + &c.Priority, + &c.LandSize, + &c.GrowthStage, + &c.PlantID, + &c.FarmID, + &c.CreatedAt, + &c.UpdatedAt, + ); err != nil { + return nil, err + } + croplands = append(croplands, c) + } + return croplands, nil +} + +func (p *postgresCroplandRepository) GetByID(ctx context.Context, uuid string) (domain.Cropland, error) { + query := ` + SELECT uuid, name, status, priority, land_size, growth_stage, plant_id, farm_id, created_at, updated_at + FROM croplands + WHERE uuid = $1` + + croplands, err := p.fetch(ctx, query, uuid) + if err != nil { + return domain.Cropland{}, err + } + if len(croplands) == 0 { + return domain.Cropland{}, domain.ErrNotFound + } + return croplands[0], nil +} + +func (p *postgresCroplandRepository) GetByFarmID(ctx context.Context, farmID string) ([]domain.Cropland, error) { + query := ` + SELECT uuid, name, status, priority, land_size, growth_stage, plant_id, farm_id, created_at, updated_at + FROM croplands + WHERE farm_id = $1` + + return p.fetch(ctx, query, farmID) +} + +func (p *postgresCroplandRepository) CreateOrUpdate(ctx context.Context, c *domain.Cropland) error { + if strings.TrimSpace(c.UUID) == "" { + c.UUID = uuid.New().String() + } + + query := ` + INSERT INTO croplands (uuid, name, status, priority, land_size, growth_stage, plant_id, farm_id, created_at, updated_at) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW(), NOW()) + ON CONFLICT (uuid) DO UPDATE + SET name = EXCLUDED.name, + status = EXCLUDED.status, + priority = EXCLUDED.priority, + land_size = EXCLUDED.land_size, + growth_stage = EXCLUDED.growth_stage, + plant_id = EXCLUDED.plant_id, + farm_id = EXCLUDED.farm_id, + updated_at = NOW() + RETURNING uuid, created_at, updated_at` + + return p.conn.QueryRow( + ctx, + query, + c.UUID, + c.Name, + c.Status, + c.Priority, + c.LandSize, + c.GrowthStage, + c.PlantID, + c.FarmID, + ).Scan(&c.CreatedAt, &c.UpdatedAt) +} + +func (p *postgresCroplandRepository) Delete(ctx context.Context, uuid string) error { + query := `DELETE FROM croplands WHERE uuid = $1` + _, err := p.conn.Exec(ctx, query, uuid) + return err +} diff --git a/backend/internal/repository/postgres_farm.go b/backend/internal/repository/postgres_farm.go new file mode 100644 index 0000000..e0fa6a4 --- /dev/null +++ b/backend/internal/repository/postgres_farm.go @@ -0,0 +1,102 @@ +package repository + +import ( + "context" + "strings" + + "github.com/google/uuid" + + "github.com/forfarm/backend/internal/domain" +) + +type postgresFarmRepository struct { + conn Connection +} + +func NewPostgresFarm(conn Connection) domain.FarmRepository { + return &postgresFarmRepository{conn: conn} +} + +func (p *postgresFarmRepository) fetch(ctx context.Context, query string, args ...interface{}) ([]domain.Farm, error) { + rows, err := p.conn.Query(ctx, query, args...) + if err != nil { + return nil, err + } + defer rows.Close() + + var farms []domain.Farm + for rows.Next() { + var f domain.Farm + if err := rows.Scan( + &f.UUID, + &f.Name, + &f.Lat, + &f.Lon, + &f.CreatedAt, + &f.UpdatedAt, + &f.OwnerID, + ); err != nil { + return nil, err + } + farms = append(farms, f) + } + return farms, nil +} + +func (p *postgresFarmRepository) GetByID(ctx context.Context, uuid string) (domain.Farm, error) { + query := ` + SELECT uuid, name, lat, lon, created_at, updated_at, owner_id + FROM farms + WHERE uuid = $1` + + farms, err := p.fetch(ctx, query, uuid) + if err != nil { + return domain.Farm{}, err + } + if len(farms) == 0 { + return domain.Farm{}, domain.ErrNotFound + } + return farms[0], nil +} + +func (p *postgresFarmRepository) GetByOwnerID(ctx context.Context, ownerID string) ([]domain.Farm, error) { + query := ` + SELECT uuid, name, lat, lon, created_at, updated_at, owner_id + FROM farms + WHERE owner_id = $1` + + return p.fetch(ctx, query, ownerID) +} + +func (p *postgresFarmRepository) CreateOrUpdate(ctx context.Context, f *domain.Farm) error { + if strings.TrimSpace(f.UUID) == "" { + f.UUID = uuid.New().String() + } + + query := ` + INSERT INTO farms (uuid, name, lat, lon, created_at, updated_at, owner_id) + VALUES ($1, $2, $3, $4, NOW(), NOW(), $5) + ON CONFLICT (uuid) DO UPDATE + SET name = EXCLUDED.name, + lat = EXCLUDED.lat, + lon = EXCLUDED.lon, + updated_at = NOW(), + owner_id = EXCLUDED.owner_id + RETURNING uuid, created_at, updated_at` + + return p.conn.QueryRow( + ctx, + query, + f.UUID, + f.Name, + f.Lat, + f.Lon, + f.OwnerID, + ).Scan(&f.CreatedAt, &f.UpdatedAt) +} + +func (p *postgresFarmRepository) Delete(ctx context.Context, uuid string) error { + query := `DELETE FROM farms WHERE uuid = $1` + _, err := p.conn.Exec(ctx, query, uuid) + return err +} diff --git a/frontend/app/setup/google-map-with-drawing.tsx b/frontend/app/setup/google-map-with-drawing.tsx new file mode 100644 index 0000000..46ddbd5 --- /dev/null +++ b/frontend/app/setup/google-map-with-drawing.tsx @@ -0,0 +1,67 @@ +"use client"; + +import { GoogleMap, LoadScript, DrawingManager } from "@react-google-maps/api"; +import { useState, useCallback } from "react"; + +const containerStyle = { + width: "100%", + height: "500px", +}; + +const center = { lat: 13.7563, lng: 100.5018 }; // Example: Bangkok, Thailand + +const GoogleMapWithDrawing = () => { + const [map, setMap] = useState(null); + + // Handles drawing complete + const onDrawingComplete = useCallback( + (overlay: google.maps.drawing.OverlayCompleteEvent) => { + console.log("Drawing complete:", overlay); + }, + [] + ); + + return ( + + setMap(map)} + > + {map && ( + + )} + + + ); +}; + +export default GoogleMapWithDrawing; diff --git a/frontend/app/setup/harvest-detail-form.tsx b/frontend/app/setup/harvest-detail-form.tsx new file mode 100644 index 0000000..38e2124 --- /dev/null +++ b/frontend/app/setup/harvest-detail-form.tsx @@ -0,0 +1,230 @@ +"use client"; + +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { harvestDetailsFormSchema } from "@/schemas/application.schema"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { Input } from "@/components/ui/input"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; + +type harvestSchema = z.infer; + +export default function HarvestDetailsForm() { + const form = useForm({ + resolver: zodResolver(harvestDetailsFormSchema), + defaultValues: {}, + }); + return ( +
+ + ( + + + Days To Flower + + +
+
+ +
+
+
+ +
+ )} + /> + ( + + + Days To Maturity + + +
+
+ +
+
+
+ +
+ )} + /> + ( + + + Harvest Window + + +
+
+ +
+
+
+ +
+ )} + /> + ( + + + Estimated Loss Rate + + +
+
+ +
+
+
+ +
+ )} + /> + ( + + Harvest Units + + + + + + )} + /> + ( + + + Estimated Revenue + + +
+
+ +
+
+
+ +
+ )} + /> + ( + + + Expected Yield Per100ft + + +
+
+ +
+
+
+ +
+ )} + /> + ( + + + Expected Yield Per Acre + + +
+
+ +
+
+
+ +
+ )} + /> + + + ); +} diff --git a/frontend/app/setup/page.tsx b/frontend/app/setup/page.tsx index cc11086..223ec01 100644 --- a/frontend/app/setup/page.tsx +++ b/frontend/app/setup/page.tsx @@ -1,14 +1,34 @@ import PlantingDetailsForm from "./planting-detail-form"; +import HarvestDetailsForm from "./harvest-detail-form"; import { Separator } from "@/components/ui/separator"; +import GoogleMapWithDrawing from "./google-map-with-drawing"; export default function SetupPage() { return (
-

Plating Details

+
+

Plating Details

+
-
+
+
+

Harvest Details

+
+ +
+ +
+
+
+

Map

+
+ +
+ +
+
); } diff --git a/frontend/app/setup/planting-detail-form.tsx b/frontend/app/setup/planting-detail-form.tsx index 13d8a08..e6cc1c2 100644 --- a/frontend/app/setup/planting-detail-form.tsx +++ b/frontend/app/setup/planting-detail-form.tsx @@ -13,6 +13,15 @@ import { useForm } from "react-hook-form"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { Input } from "@/components/ui/input"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { Textarea } from "@/components/ui/textarea"; +import { Switch } from "@/components/ui/switch"; type plantingSchema = z.infer; @@ -23,7 +32,7 @@ export default function PlantingDetailsForm() { }); return (
- +
)} /> + ( + + Start Method + + + + + + )} + /> + ( + + Light Profile + + + + + + )} + /> + ( + + + Soil Conditions + + + + + + + )} + /> + ( + + + Planting Details + + +
+
+