mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-19 14:04:08 +01:00
feat: add plant query api
This commit is contained in:
parent
8ddf1c82e6
commit
3df25a7c2f
@ -25,16 +25,17 @@ type api struct {
|
|||||||
userRepo domain.UserRepository
|
userRepo domain.UserRepository
|
||||||
cropRepo domain.CroplandRepository
|
cropRepo domain.CroplandRepository
|
||||||
farmRepo domain.FarmRepository
|
farmRepo domain.FarmRepository
|
||||||
|
plantRepo domain.PlantRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAPI(ctx context.Context, logger *slog.Logger, pool *pgxpool.Pool) *api {
|
func NewAPI(ctx context.Context, logger *slog.Logger, pool *pgxpool.Pool) *api {
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
// Initialize repositories for users and croplands
|
|
||||||
userRepository := repository.NewPostgresUser(pool)
|
userRepository := repository.NewPostgresUser(pool)
|
||||||
croplandRepository := repository.NewPostgresCropland(pool)
|
croplandRepository := repository.NewPostgresCropland(pool)
|
||||||
farmRepository := repository.NewPostgresFarm(pool)
|
farmRepository := repository.NewPostgresFarm(pool)
|
||||||
|
plantRepository := repository.NewPostgresPlant(pool)
|
||||||
|
|
||||||
return &api{
|
return &api{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
@ -43,6 +44,7 @@ func NewAPI(ctx context.Context, logger *slog.Logger, pool *pgxpool.Pool) *api {
|
|||||||
userRepo: userRepository,
|
userRepo: userRepository,
|
||||||
cropRepo: croplandRepository,
|
cropRepo: croplandRepository,
|
||||||
farmRepo: farmRepository,
|
farmRepo: farmRepository,
|
||||||
|
plantRepo: plantRepository,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,15 +72,13 @@ func (a *api) Routes() *chi.Mux {
|
|||||||
config := huma.DefaultConfig("ForFarm Public API", "v1.0.0")
|
config := huma.DefaultConfig("ForFarm Public API", "v1.0.0")
|
||||||
api := humachi.New(router, config)
|
api := humachi.New(router, config)
|
||||||
|
|
||||||
// Register Authentication Routes
|
|
||||||
router.Group(func(r chi.Router) {
|
router.Group(func(r chi.Router) {
|
||||||
a.registerAuthRoutes(r, api)
|
a.registerAuthRoutes(r, api)
|
||||||
a.registerCropRoutes(r, api)
|
a.registerCropRoutes(r, api)
|
||||||
|
a.registerPlantRoutes(r, api)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Register Cropland Routes, including Auth Middleware if required
|
|
||||||
router.Group(func(r chi.Router) {
|
router.Group(func(r chi.Router) {
|
||||||
// Apply Authentication middleware to the Cropland routes
|
|
||||||
api.UseMiddleware(m.AuthMiddleware(api))
|
api.UseMiddleware(m.AuthMiddleware(api))
|
||||||
a.registerHelloRoutes(r, api)
|
a.registerHelloRoutes(r, api)
|
||||||
a.registerFarmRoutes(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.PlantingDetail, &plant.IsPerennial, &plant.DaysToEmerge,
|
||||||
&plant.DaysToFlower, &plant.DaysToMaturity, &plant.HarvestWindow,
|
&plant.DaysToFlower, &plant.DaysToMaturity, &plant.HarvestWindow,
|
||||||
&plant.PHValue, &plant.EstimateLossRate, &plant.EstimateRevenuePerHU,
|
&plant.PHValue, &plant.EstimateLossRate, &plant.EstimateRevenuePerHU,
|
||||||
&plant.HarvestUnitID, &plant.WaterNeeds, &plant.CreatedAt, &plant.UpdatedAt,
|
&plant.HarvestUnitID, &plant.WaterNeeds,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user