From aee7cbc52629c82930495e9235da6b1cf6e0f98e Mon Sep 17 00:00:00 2001 From: Vojtech Mares Date: Thu, 27 Jun 2024 09:14:15 +0200 Subject: [PATCH] feat(server): implement training by slug endpoint --- internal/server/api.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/internal/server/api.go b/internal/server/api.go index cef7d3f..96dd70c 100644 --- a/internal/server/api.go +++ b/internal/server/api.go @@ -176,6 +176,41 @@ func (h *APIHandlers) GetTraining(ctx context.Context, req GetTrainingRequestObj }, nil } +func (h *APIHandlers) GetTrainingBySlug(ctx context.Context, req GetTrainingBySlugRequestObject) (GetTrainingBySlugResponseObject, error) { + t, err := h.trainingRepository.FindBySlug(req.TrainingSlug) + if errors.Is(err, training.ErrTrainingNotFound) { + return GetTrainingBySlug404ApplicationProblemPlusJSONResponse{ + NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{ + Status: fiber.StatusNotFound, + Title: "Not Found: Training not found", + }}, nil + } else if err != nil { + return GetTrainingBySlug500ApplicationProblemPlusJSONResponse{ + 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.String(), + Currency: p.Currency, + Type: TrainingPriceType(p.Type), + } + } + return GetTrainingBySlug200JSONResponse{ + Id: t.ID, + Name: t.Name, + Slug: t.Slug, + Days: t.Days, + Description: t.Description, + Pricing: pricing, + }, nil +} + func (h *APIHandlers) UpdateTraining(ctx context.Context, req UpdateTrainingRequestObject) (UpdateTrainingResponseObject, error) { pricing := make([]training.Price, len(req.Body.Pricing)) for idx, p := range req.Body.Pricing {