feat(server): bump generated api and server implements handlers for new routes
This commit is contained in:
parent
9cd3b7d9fe
commit
2dcaa4c646
3 changed files with 794 additions and 27 deletions
|
|
@ -2,18 +2,21 @@ package server
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
||||
)
|
||||
|
||||
type APIHandlers struct {
|
||||
trainingRepository training.TrainingRepository
|
||||
trainingRepository training.TrainingRepository
|
||||
trainingDateRepository training.TrainingDateRepository
|
||||
}
|
||||
|
||||
func NewAPIHandlers(trainingRepository training.TrainingRepository) *APIHandlers {
|
||||
func NewAPIHandlers(trainingRepository training.TrainingRepository, trainingDateRepository training.TrainingDateRepository) *APIHandlers {
|
||||
return &APIHandlers{
|
||||
trainingRepository: trainingRepository,
|
||||
trainingRepository: trainingRepository,
|
||||
trainingDateRepository: trainingDateRepository,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -183,3 +186,197 @@ func (h *APIHandlers) UpdateTraining(ctx context.Context, req UpdateTrainingRequ
|
|||
Description: t.Description,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const trainingDateStartTimeFormat = "15:04"
|
||||
|
||||
func (h *APIHandlers) ListTrainingDates(ctx context.Context, req ListTrainingDatesRequestObject) (ListTrainingDatesResponseObject, error) {
|
||||
trainingDates, err := h.trainingDateRepository.FindAllByTrainingID(req.TrainingID)
|
||||
if err != nil {
|
||||
return ListTrainingDates500ApplicationProblemPlusJSONResponse{
|
||||
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
||||
Status: fiber.StatusInternalServerError,
|
||||
Title: "Internal Server Error: Failed to list training dates",
|
||||
Detail: err.Error(),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
data := make([]TrainingDate, len(trainingDates))
|
||||
for idx, td := range trainingDates {
|
||||
data[idx] = TrainingDate{
|
||||
Id: td.ID,
|
||||
Date: td.Date,
|
||||
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
||||
Days: td.Days,
|
||||
IsOnline: td.IsOnline,
|
||||
Location: td.Location,
|
||||
Address: td.Address,
|
||||
Capacity: td.Capacity,
|
||||
Price: TrainingDatePrice{
|
||||
Amount: td.Price.Amount,
|
||||
Currency: td.Price.Currency,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return ListTrainingDates200JSONResponse(data), nil
|
||||
}
|
||||
|
||||
func (h *APIHandlers) CreateTrainingDate(ctx context.Context, req CreateTrainingDateRequestObject) (CreateTrainingDateResponseObject, error) {
|
||||
price := training.TrainingDatePrice{
|
||||
Amount: req.Body.Price.Amount,
|
||||
Currency: req.Body.Price.Currency,
|
||||
}
|
||||
|
||||
startTime, err := time.Parse(time.RFC3339, "2000-01-01T"+req.Body.StartTime)
|
||||
if err != nil {
|
||||
return CreateTrainingDate400ApplicationProblemPlusJSONResponse{
|
||||
InvalidInputErrorApplicationProblemPlusJSONResponse: InvalidInputErrorApplicationProblemPlusJSONResponse{
|
||||
Status: fiber.StatusBadRequest,
|
||||
Title: "Invalid Input: Invalid startTime",
|
||||
Detail: err.Error(),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
td := training.TrainingDate{
|
||||
Date: req.Body.Date,
|
||||
StartTime: startTime,
|
||||
Days: req.Body.Days,
|
||||
IsOnline: req.Body.IsOnline,
|
||||
Location: req.Body.Location,
|
||||
Address: req.Body.Address,
|
||||
Capacity: req.Body.Capacity,
|
||||
Price: price,
|
||||
}
|
||||
|
||||
err = h.trainingDateRepository.Create(req.TrainingID, &td)
|
||||
if err != nil {
|
||||
return CreateTrainingDate500ApplicationProblemPlusJSONResponse{
|
||||
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
||||
Status: fiber.StatusInternalServerError,
|
||||
Title: "Internal Server Error: Failed to create training date",
|
||||
Detail: err.Error(),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
return CreateTrainingDate201JSONResponse{
|
||||
Id: td.ID,
|
||||
Date: td.Date,
|
||||
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
||||
Days: td.Days,
|
||||
IsOnline: td.IsOnline,
|
||||
Location: td.Location,
|
||||
Address: td.Address,
|
||||
Capacity: td.Capacity,
|
||||
Price: TrainingDatePrice{
|
||||
Amount: td.Price.Amount,
|
||||
Currency: td.Price.Currency,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *APIHandlers) DeleteTrainingDate(ctx context.Context, req DeleteTrainingDateRequestObject) (DeleteTrainingDateResponseObject, error) {
|
||||
err := h.trainingDateRepository.Delete(req.TrainingDateID)
|
||||
if err == training.ErrTrainingDateNotFound {
|
||||
return DeleteTrainingDate404ApplicationProblemPlusJSONResponse{
|
||||
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
||||
Status: fiber.StatusNotFound,
|
||||
Title: "Not Found: Training date not found",
|
||||
}}, nil
|
||||
} else if err != nil {
|
||||
return DeleteTrainingDate500ApplicationProblemPlusJSONResponse{
|
||||
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
||||
Status: fiber.StatusInternalServerError,
|
||||
Title: "Internal Server Error: Failed to delete training date",
|
||||
Detail: err.Error(),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
return DeleteTrainingDate204Response{}, nil
|
||||
}
|
||||
|
||||
func (h *APIHandlers) GetTrainingDate(ctx context.Context, req GetTrainingDateRequestObject) (GetTrainingDateResponseObject, error) {
|
||||
td, err := h.trainingDateRepository.FindByID(req.TrainingDateID)
|
||||
if err == training.ErrTrainingDateNotFound {
|
||||
return GetTrainingDate404ApplicationProblemPlusJSONResponse{
|
||||
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
||||
Status: fiber.StatusNotFound,
|
||||
Title: "Not Found: Training date not found",
|
||||
}}, nil
|
||||
} else if err != nil {
|
||||
return GetTrainingDate500ApplicationProblemPlusJSONResponse{
|
||||
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
||||
Status: fiber.StatusInternalServerError,
|
||||
Title: "Internal Server Error: Failed to get training date",
|
||||
Detail: err.Error(),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
return GetTrainingDate200JSONResponse{
|
||||
Id: td.ID,
|
||||
Date: td.Date,
|
||||
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
||||
Days: td.Days,
|
||||
IsOnline: td.IsOnline,
|
||||
Location: td.Location,
|
||||
Address: td.Address,
|
||||
Capacity: td.Capacity,
|
||||
Price: TrainingDatePrice{
|
||||
Amount: td.Price.Amount,
|
||||
Currency: td.Price.Currency,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *APIHandlers) UpdateTrainingDate(ctx context.Context, req UpdateTrainingDateRequestObject) (UpdateTrainingDateResponseObject, error) {
|
||||
price := training.TrainingDatePrice{
|
||||
Amount: req.Body.Price.Amount,
|
||||
Currency: req.Body.Price.Currency,
|
||||
}
|
||||
|
||||
startTime, err := time.Parse(time.RFC3339, "2000-01-01T"+req.Body.StartTime)
|
||||
if err != nil {
|
||||
return UpdateTrainingDate400ApplicationProblemPlusJSONResponse{
|
||||
InvalidInputErrorApplicationProblemPlusJSONResponse: InvalidInputErrorApplicationProblemPlusJSONResponse{
|
||||
Status: fiber.StatusBadRequest,
|
||||
Title: "Invalid Input: Invalid startTime",
|
||||
Detail: err.Error(),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
td := training.TrainingDate{
|
||||
ID: req.TrainingDateID,
|
||||
Date: req.Body.Date,
|
||||
StartTime: startTime,
|
||||
Days: req.Body.Days,
|
||||
IsOnline: req.Body.IsOnline,
|
||||
Location: req.Body.Location,
|
||||
Address: req.Body.Address,
|
||||
Capacity: req.Body.Capacity,
|
||||
Price: price,
|
||||
}
|
||||
|
||||
err = h.trainingDateRepository.Update(&td)
|
||||
if err != nil {
|
||||
return UpdateTrainingDate500ApplicationProblemPlusJSONResponse{
|
||||
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
||||
Status: fiber.StatusInternalServerError,
|
||||
Title: "Internal Server Error: Failed to update training date",
|
||||
Detail: err.Error(),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
return UpdateTrainingDate200JSONResponse{
|
||||
Id: td.ID,
|
||||
Date: td.Date,
|
||||
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
||||
Days: td.Days,
|
||||
IsOnline: td.IsOnline,
|
||||
Location: td.Location,
|
||||
Address: td.Address,
|
||||
Capacity: td.Capacity,
|
||||
Price: TrainingDatePrice{
|
||||
Amount: td.Price.Amount,
|
||||
Currency: td.Price.Currency,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue