diff --git a/backend/internal/api/api.go b/backend/internal/api/api.go index 24d50e9..a66a918 100644 --- a/backend/internal/api/api.go +++ b/backend/internal/api/api.go @@ -22,27 +22,29 @@ type api struct { logger *slog.Logger httpClient *http.Client - userRepo domain.UserRepository - cropRepo domain.CroplandRepository - farmRepo domain.FarmRepository + userRepo domain.UserRepository + cropRepo domain.CroplandRepository + farmRepo domain.FarmRepository + plantRepo domain.PlantRepository } func NewAPI(ctx context.Context, logger *slog.Logger, pool *pgxpool.Pool) *api { client := &http.Client{} - // Initialize repositories for users and croplands userRepository := repository.NewPostgresUser(pool) croplandRepository := repository.NewPostgresCropland(pool) farmRepository := repository.NewPostgresFarm(pool) + plantRepository := repository.NewPostgresPlant(pool) return &api{ logger: logger, httpClient: client, - userRepo: userRepository, - cropRepo: croplandRepository, - farmRepo: farmRepository, + userRepo: userRepository, + cropRepo: croplandRepository, + farmRepo: farmRepository, + plantRepo: plantRepository, } } @@ -70,15 +72,13 @@ func (a *api) Routes() *chi.Mux { config := huma.DefaultConfig("ForFarm Public API", "v1.0.0") api := humachi.New(router, config) - // Register Authentication Routes router.Group(func(r chi.Router) { a.registerAuthRoutes(r, api) a.registerCropRoutes(r, api) + a.registerPlantRoutes(r, api) }) - // Register Cropland Routes, including Auth Middleware if required router.Group(func(r chi.Router) { - // Apply Authentication middleware to the Cropland routes api.UseMiddleware(m.AuthMiddleware(api)) a.registerHelloRoutes(r, api) a.registerFarmRoutes(r, api) diff --git a/backend/internal/api/plant.go b/backend/internal/api/plant.go new file mode 100644 index 0000000..51add35 --- /dev/null +++ b/backend/internal/api/plant.go @@ -0,0 +1,40 @@ +package api + +import ( + "context" + "net/http" + + "github.com/danielgtaylor/huma/v2" + "github.com/forfarm/backend/internal/domain" + "github.com/go-chi/chi/v5" +) + +func (a *api) registerPlantRoutes(_ chi.Router, api huma.API) { + tags := []string{"plant"} + prefix := "/plant" + + huma.Register(api, huma.Operation{ + OperationID: "getAllPlant", + Method: http.MethodGet, + Path: prefix, + Tags: tags, + }, a.getAllPlantHandler) +} + +type GetAllPlantsOutput struct { + Body struct { + Plants []domain.Plant `json:"plants"` + } +} + +func (a *api) getAllPlantHandler(ctx context.Context, input *struct{}) (*GetAllPlantsOutput, error) { + resp := &GetAllPlantsOutput{} + plants, err := a.plantRepo.GetAll(ctx) + if err != nil { + return nil, err + } + + resp.Body.Plants = plants + + return resp, nil +} diff --git a/backend/internal/repository/postgres_plant.go b/backend/internal/repository/postgres_plant.go index 797f498..963c31a 100644 --- a/backend/internal/repository/postgres_plant.go +++ b/backend/internal/repository/postgres_plant.go @@ -33,7 +33,7 @@ func (p *postgresPlantRepository) fetch(ctx context.Context, query string, args &plant.PlantingDetail, &plant.IsPerennial, &plant.DaysToEmerge, &plant.DaysToFlower, &plant.DaysToMaturity, &plant.HarvestWindow, &plant.PHValue, &plant.EstimateLossRate, &plant.EstimateRevenuePerHU, - &plant.HarvestUnitID, &plant.WaterNeeds, &plant.CreatedAt, &plant.UpdatedAt, + &plant.HarvestUnitID, &plant.WaterNeeds, ); err != nil { return nil, err }