167 lines
5 KiB
Go
167 lines
5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"github.com/shopspring/decimal"
|
|
"gitlab.mareshq.com/hq/backoffice/backoffice-api/internal/training"
|
|
)
|
|
|
|
func (s *Server) CreateTraining(ctx context.Context, req CreateTrainingRequestObject) (CreateTrainingResponseObject, error) {
|
|
|
|
pricing := make([]training.Price, len(req.Body.Training.Pricing))
|
|
for i, p := range req.Body.Training.Pricing {
|
|
amount, err := decimal.NewFromString(p.Amount)
|
|
if err != nil {
|
|
return CreateTraining400ApplicationProblemPlusJSONResponse{
|
|
InvalidInputErrorApplicationProblemPlusJSONResponse{
|
|
Title: "Invalid amount",
|
|
Detail: err.Error(),
|
|
Instance: s.hostname,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
pricing[i] = training.Price{
|
|
Currency: p.Currency,
|
|
Amount: amount,
|
|
Type: training.PriceType(p.Type),
|
|
}
|
|
}
|
|
|
|
newTr := training.NewTraining(req.Body.Training.Name, req.Body.Training.Days, pricing)
|
|
|
|
err := s.trainingRepository.Create(ctx, newTr)
|
|
if err != nil {
|
|
// returning response and nil as error
|
|
// since we suppress the error in code here and return an error response instead
|
|
return CreateTraining500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse{
|
|
Title: "Internal Server Error",
|
|
Detail: err.Error(),
|
|
Instance: s.hostname,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
tr := trainingToAPITraining(newTr)
|
|
return CreateTraining201JSONResponse{
|
|
CreateTrainingResponseJSONResponse(tr),
|
|
}, nil
|
|
}
|
|
|
|
func (s *Server) GetTrainingByID(ctx context.Context, req GetTrainingByIDRequestObject) (GetTrainingByIDResponseObject, error) {
|
|
tr, err := s.trainingRepository.FindByID(ctx, req.TrainingID)
|
|
if err != nil {
|
|
return GetTrainingByID404ApplicationProblemPlusJSONResponse{
|
|
// returning response and nil as error
|
|
// since we suppress the error in code here and return an error response instead
|
|
NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Title: "Not Found Training by ID",
|
|
Detail: err.Error(),
|
|
Instance: s.hostname,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
trAPI := trainingToAPITraining(tr)
|
|
return GetTrainingByID200JSONResponse{
|
|
GetTrainingResponseJSONResponse(trAPI),
|
|
}, nil
|
|
}
|
|
|
|
func (s *Server) ListTrainings(ctx context.Context, _ ListTrainingsRequestObject) (ListTrainingsResponseObject, error) {
|
|
trs, err := s.trainingRepository.FindAll(ctx)
|
|
if err != nil {
|
|
return ListTrainings500ApplicationProblemPlusJSONResponse{
|
|
// returning response and nil as error
|
|
// since we suppress the error in code here and return an error response instead
|
|
InternalErrorApplicationProblemPlusJSONResponse{
|
|
Title: "Internal Server Error",
|
|
Detail: err.Error(),
|
|
Instance: s.hostname,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
var trAPIs []Training
|
|
for _, tr := range trs {
|
|
trAPIs = append(trAPIs, trainingToAPITraining(&tr))
|
|
}
|
|
|
|
return ListTrainings200JSONResponse{
|
|
ListTrainingsResponseJSONResponse{
|
|
Trainings: &trAPIs,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (s *Server) PublishTraining(ctx context.Context, req PublishTrainingRequestObject) (PublishTrainingResponseObject, error) {
|
|
err := s.trainingRepository.Publish(ctx, req.TrainingID)
|
|
if err != nil {
|
|
return PublishTraining500ApplicationProblemPlusJSONResponse{
|
|
// returning response and nil as error
|
|
// since we suppress the error in code here and return an error response instead
|
|
InternalErrorApplicationProblemPlusJSONResponse{
|
|
Title: "Internal Server Error",
|
|
Detail: err.Error(),
|
|
Instance: s.hostname,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
return PublishTraining204Response{}, nil
|
|
}
|
|
|
|
func (s *Server) UnpublishTraining(ctx context.Context, req UnpublishTrainingRequestObject) (UnpublishTrainingResponseObject, error) {
|
|
err := s.trainingRepository.Unpublish(ctx, req.TrainingID)
|
|
if err != nil {
|
|
return UnpublishTraining500ApplicationProblemPlusJSONResponse{
|
|
// returning response and nil as error
|
|
// since we suppress the error in code here and return an error response instead
|
|
InternalErrorApplicationProblemPlusJSONResponse{
|
|
Title: "Internal Server Error",
|
|
Detail: err.Error(),
|
|
Instance: s.hostname,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
return UnpublishTraining204Response{}, nil
|
|
}
|
|
|
|
func (s *Server) RetireTraining(ctx context.Context, req RetireTrainingRequestObject) (RetireTrainingResponseObject, error) {
|
|
err := s.trainingRepository.Retire(ctx, req.TrainingID)
|
|
if err != nil {
|
|
return RetireTraining500ApplicationProblemPlusJSONResponse{
|
|
// returning response and nil as error
|
|
// since we suppress the error in code here and return an error response instead
|
|
InternalErrorApplicationProblemPlusJSONResponse{
|
|
Title: "Internal Server Error",
|
|
Detail: err.Error(),
|
|
Instance: s.hostname,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
return RetireTraining204Response{}, nil
|
|
}
|
|
|
|
func trainingToAPITraining(t *training.Training) Training {
|
|
tPricing := t.Pricing()
|
|
pricing := make([]TrainingPrice, len(tPricing))
|
|
for i, p := range tPricing {
|
|
pricing[i] = TrainingPrice{
|
|
Currency: p.Currency,
|
|
Amount: p.Amount.String(),
|
|
Type: TrainingPriceType(p.Type),
|
|
}
|
|
}
|
|
return Training{
|
|
Id: t.ID(),
|
|
Name: t.Name(),
|
|
Days: t.Days(),
|
|
Published: t.Published(),
|
|
Retired: t.Retired(),
|
|
Pricing: pricing,
|
|
}
|
|
}
|