185 lines
5.6 KiB
Go
185 lines
5.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
|
)
|
|
|
|
type APIHandlers struct {
|
|
trainingRepository training.TrainingRepository
|
|
}
|
|
|
|
func NewAPIHandlers(trainingRepository training.TrainingRepository) *APIHandlers {
|
|
return &APIHandlers{
|
|
trainingRepository: trainingRepository,
|
|
}
|
|
}
|
|
|
|
func (h *APIHandlers) ListTrainings(ctx context.Context, req ListTrainingsRequestObject) (ListTrainingsResponseObject, error) {
|
|
trainings, err := h.trainingRepository.FindAll()
|
|
if err != nil {
|
|
return ListTrainings500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to list trainings",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
data := make([]Training, len(trainings))
|
|
for idx, t := range trainings {
|
|
pricing := make([]TrainingPrice, len(t.Pricing))
|
|
for idx, p := range t.Pricing {
|
|
pricing[idx] = TrainingPrice{
|
|
Amount: p.Amount,
|
|
Currency: p.Currency,
|
|
Type: TrainingPriceType(p.Type),
|
|
}
|
|
}
|
|
data[idx] = Training{
|
|
Id: t.ID,
|
|
Name: t.Name,
|
|
Days: t.Days,
|
|
Description: t.Description,
|
|
Pricing: pricing,
|
|
}
|
|
}
|
|
|
|
return ListTrainings200JSONResponse(data), nil
|
|
}
|
|
|
|
func (h *APIHandlers) CreateTraining(ctx context.Context, req CreateTrainingRequestObject) (CreateTrainingResponseObject, error) {
|
|
pricing := make([]training.TrainingPrice, len(req.Body.Pricing))
|
|
for idx, p := range req.Body.Pricing {
|
|
pricing[idx] = training.TrainingPrice{
|
|
Amount: p.Amount,
|
|
Currency: p.Currency,
|
|
Type: training.TrainingPriceType(p.Type),
|
|
}
|
|
}
|
|
|
|
t := training.Training{
|
|
Name: req.Body.Name,
|
|
Days: req.Body.Days,
|
|
Description: req.Body.Description,
|
|
Pricing: pricing,
|
|
}
|
|
|
|
err := h.trainingRepository.Create(&t)
|
|
if err != nil {
|
|
return CreateTraining500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to create training",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
responsePricing := make([]TrainingPrice, len(t.Pricing))
|
|
for idx, p := range t.Pricing {
|
|
responsePricing[idx] = TrainingPrice{
|
|
Amount: p.Amount,
|
|
Currency: p.Currency,
|
|
Type: TrainingPriceType(p.Type),
|
|
}
|
|
}
|
|
|
|
return CreateTraining201JSONResponse{
|
|
Id: t.ID,
|
|
Name: t.Name,
|
|
Days: t.Days,
|
|
Description: t.Description,
|
|
Pricing: responsePricing,
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) DeleteTraining(ctx context.Context, req DeleteTrainingRequestObject) (DeleteTrainingResponseObject, error) {
|
|
err := h.trainingRepository.Delete(req.TrainingID)
|
|
if err == training.ErrTrainingNotFound {
|
|
return DeleteTraining404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training not found",
|
|
}}, nil
|
|
} else if err != nil {
|
|
return DeleteTraining500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to delete training",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
return DeleteTraining204Response{}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) GetTraining(ctx context.Context, req GetTrainingRequestObject) (GetTrainingResponseObject, error) {
|
|
t, err := h.trainingRepository.FindByID(req.TrainingID)
|
|
if err == training.ErrTrainingNotFound {
|
|
return GetTraining404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training not found",
|
|
}}, nil
|
|
} else if err != nil {
|
|
return GetTraining500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
pricing := make([]TrainingPrice, len(t.Pricing))
|
|
for idx, p := range t.Pricing {
|
|
pricing[idx] = TrainingPrice{
|
|
Amount: p.Amount,
|
|
Currency: p.Currency,
|
|
Type: TrainingPriceType(p.Type),
|
|
}
|
|
}
|
|
return GetTraining200JSONResponse{
|
|
Id: t.ID,
|
|
Name: t.Name,
|
|
Days: t.Days,
|
|
Description: t.Description,
|
|
Pricing: pricing,
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) UpdateTraining(ctx context.Context, req UpdateTrainingRequestObject) (UpdateTrainingResponseObject, error) {
|
|
pricing := make([]training.TrainingPrice, len(req.Body.Pricing))
|
|
for idx, p := range req.Body.Pricing {
|
|
pricing[idx] = training.TrainingPrice{
|
|
Amount: p.Amount,
|
|
Currency: p.Currency,
|
|
Type: training.TrainingPriceType(p.Type),
|
|
}
|
|
}
|
|
t := training.Training{
|
|
ID: req.TrainingID,
|
|
Name: req.Body.Name,
|
|
Days: req.Body.Days,
|
|
Description: req.Body.Description,
|
|
Pricing: pricing,
|
|
}
|
|
|
|
err := h.trainingRepository.Update(&t)
|
|
if err != nil {
|
|
return UpdateTraining500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to update training",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
return UpdateTraining200JSONResponse{
|
|
Id: t.ID,
|
|
Name: t.Name,
|
|
Days: t.Days,
|
|
Description: t.Description,
|
|
}, nil
|
|
}
|