mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-18 21:44:08 +01:00
feat: add plant query api
This commit is contained in:
parent
8ddf1c82e6
commit
3df25a7c2f
@ -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)
|
||||
|
||||
40
backend/internal/api/plant.go
Normal file
40
backend/internal/api/plant.go
Normal file
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user