997 lines
36 KiB
Go
997 lines
36 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/oapi-codegen/runtime/types"
|
|
"github.com/shopspring/decimal"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"gitlab.mareshq.com/hq/yggdrasil/internal/money"
|
|
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
|
)
|
|
|
|
type APIHandlers struct {
|
|
trainingRepository training.Repository
|
|
trainingDateRepository training.DateRepository
|
|
trainingDateAttendeeRepository training.AttendeeRepository
|
|
}
|
|
|
|
func NewAPIHandlers(trainingRepository training.Repository, trainingDateRepository training.DateRepository, trainingDateAttendeeRepository training.AttendeeRepository) *APIHandlers {
|
|
return &APIHandlers{
|
|
trainingRepository: trainingRepository,
|
|
trainingDateRepository: trainingDateRepository,
|
|
trainingDateAttendeeRepository: trainingDateAttendeeRepository,
|
|
}
|
|
}
|
|
|
|
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.String(),
|
|
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.Price, len(req.Body.Pricing))
|
|
for idx, p := range req.Body.Pricing {
|
|
amount, err := decimal.NewFromString(p.Amount)
|
|
if err != nil {
|
|
return CreateTraining400ApplicationProblemPlusJSONResponse{
|
|
InvalidInputErrorApplicationProblemPlusJSONResponse: InvalidInputErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusBadRequest,
|
|
Title: "Invalid Input: Invalid amount",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
pricing[idx] = training.Price{
|
|
Amount: amount,
|
|
Currency: p.Currency,
|
|
Type: training.PriceType(p.Type),
|
|
}
|
|
}
|
|
|
|
slug := ""
|
|
if req.Body.Slug != nil {
|
|
slug = *req.Body.Slug
|
|
}
|
|
|
|
t := training.Training{
|
|
Name: req.Body.Name,
|
|
Slug: slug,
|
|
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.String(),
|
|
Currency: p.Currency,
|
|
Type: TrainingPriceType(p.Type),
|
|
}
|
|
}
|
|
|
|
return CreateTraining201JSONResponse{
|
|
Id: t.ID,
|
|
Name: t.Name,
|
|
Slug: t.Slug,
|
|
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 errors.Is(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 errors.Is(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.String(),
|
|
Currency: p.Currency,
|
|
Type: TrainingPriceType(p.Type),
|
|
}
|
|
}
|
|
return GetTraining200JSONResponse{
|
|
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 {
|
|
amount, err := decimal.NewFromString(p.Amount)
|
|
if err != nil {
|
|
return UpdateTraining400ApplicationProblemPlusJSONResponse{
|
|
InvalidInputErrorApplicationProblemPlusJSONResponse: InvalidInputErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusBadRequest,
|
|
Title: "Invalid Input: Invalid amount",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
pricing[idx] = training.Price{
|
|
Amount: amount,
|
|
Currency: p.Currency,
|
|
Type: training.PriceType(p.Type),
|
|
}
|
|
}
|
|
|
|
slug := ""
|
|
if req.Body.Slug != nil {
|
|
slug = *req.Body.Slug
|
|
}
|
|
|
|
t := training.Training{
|
|
ID: req.TrainingID,
|
|
Name: req.Body.Name,
|
|
Slug: slug,
|
|
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,
|
|
Slug: t.Slug,
|
|
Days: t.Days,
|
|
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: types.Date{Time: td.Date},
|
|
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
|
Days: td.Days,
|
|
IsOnline: td.IsOnline,
|
|
Location: td.Location,
|
|
Address: td.Address,
|
|
Capacity: td.Capacity,
|
|
Price: Price{
|
|
Amount: td.PriceAmount.String(),
|
|
Currency: td.PriceCurrency,
|
|
},
|
|
}
|
|
}
|
|
|
|
return ListTrainingDates200JSONResponse(data), nil
|
|
}
|
|
|
|
func (h *APIHandlers) CreateTrainingDate(ctx context.Context, req CreateTrainingDateRequestObject) (CreateTrainingDateResponseObject, error) {
|
|
amount, err := decimal.NewFromString(req.Body.Price.Amount)
|
|
if err != nil {
|
|
return CreateTrainingDate400ApplicationProblemPlusJSONResponse{
|
|
InvalidInputErrorApplicationProblemPlusJSONResponse: InvalidInputErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusBadRequest,
|
|
Title: "Invalid Input: Invalid amount",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
price := money.Money{
|
|
Amount: amount,
|
|
Currency: req.Body.Price.Currency,
|
|
}
|
|
|
|
startTime, err := time.Parse(time.RFC3339, "2000-01-01T"+req.Body.StartTime+":00Z")
|
|
if err != nil {
|
|
return CreateTrainingDate400ApplicationProblemPlusJSONResponse{
|
|
InvalidInputErrorApplicationProblemPlusJSONResponse: InvalidInputErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusBadRequest,
|
|
Title: "Invalid Input: Invalid startTime",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
td := training.Date{
|
|
Date: req.Body.Date.Time,
|
|
StartTime: startTime,
|
|
Days: req.Body.Days,
|
|
IsOnline: req.Body.IsOnline,
|
|
Location: req.Body.Location,
|
|
Address: req.Body.Address,
|
|
Capacity: req.Body.Capacity,
|
|
PriceAmount: price.Amount,
|
|
PriceCurrency: price.Currency,
|
|
}
|
|
|
|
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: types.Date{Time: td.Date},
|
|
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
|
Days: td.Days,
|
|
IsOnline: td.IsOnline,
|
|
Location: td.Location,
|
|
Address: td.Address,
|
|
Capacity: td.Capacity,
|
|
Price: Price{
|
|
Amount: td.PriceAmount.String(),
|
|
Currency: td.PriceCurrency,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) DeleteTrainingDate(ctx context.Context, req DeleteTrainingDateRequestObject) (DeleteTrainingDateResponseObject, error) {
|
|
err := h.trainingDateRepository.Delete(req.TrainingDateID)
|
|
if errors.Is(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 errors.Is(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: types.Date{Time: td.Date},
|
|
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
|
Days: td.Days,
|
|
IsOnline: td.IsOnline,
|
|
Location: td.Location,
|
|
Address: td.Address,
|
|
Capacity: td.Capacity,
|
|
Price: Price{
|
|
Amount: td.PriceAmount.String(),
|
|
Currency: td.PriceCurrency,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) UpdateTrainingDate(ctx context.Context, req UpdateTrainingDateRequestObject) (UpdateTrainingDateResponseObject, error) {
|
|
amount, err := decimal.NewFromString(req.Body.Price.Amount)
|
|
if err != nil {
|
|
return UpdateTrainingDate400ApplicationProblemPlusJSONResponse{
|
|
InvalidInputErrorApplicationProblemPlusJSONResponse: InvalidInputErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusBadRequest,
|
|
Title: "Invalid Input: Invalid amount",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
price := money.Money{
|
|
Amount: amount,
|
|
Currency: req.Body.Price.Currency,
|
|
}
|
|
|
|
startTime, err := time.Parse(time.RFC3339, "2000-01-01T"+req.Body.StartTime+":00Z")
|
|
if err != nil {
|
|
return UpdateTrainingDate400ApplicationProblemPlusJSONResponse{
|
|
InvalidInputErrorApplicationProblemPlusJSONResponse: InvalidInputErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusBadRequest,
|
|
Title: "Invalid Input: Invalid startTime",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
td := training.Date{
|
|
ID: req.TrainingDateID,
|
|
Date: req.Body.Date.Time,
|
|
StartTime: startTime,
|
|
Days: req.Body.Days,
|
|
IsOnline: req.Body.IsOnline,
|
|
Location: req.Body.Location,
|
|
Address: req.Body.Address,
|
|
Capacity: req.Body.Capacity,
|
|
PriceAmount: price.Amount,
|
|
PriceCurrency: price.Currency,
|
|
}
|
|
|
|
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: types.Date{Time: td.Date},
|
|
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
|
Days: td.Days,
|
|
IsOnline: td.IsOnline,
|
|
Location: td.Location,
|
|
Address: td.Address,
|
|
Capacity: td.Capacity,
|
|
Price: Price{
|
|
Amount: td.PriceAmount.String(),
|
|
Currency: td.PriceCurrency,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) ListAllUpcomingTrainingDates(ctx context.Context, req ListAllUpcomingTrainingDatesRequestObject) (ListAllUpcomingTrainingDatesResponseObject, error) {
|
|
trainings, err := h.trainingRepository.FindAll()
|
|
if err != nil {
|
|
return ListAllUpcomingTrainingDates500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to list all upcoming training dates",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
response := make(ListAllUpcomingTrainingDatesResponse, len(trainings))
|
|
for i, t := range trainings {
|
|
trainingDates, err := h.trainingDateRepository.FindUpcomingByTrainingID(t.ID)
|
|
if err != nil {
|
|
return ListAllUpcomingTrainingDates500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to list all upcoming training dates",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
response[i].Dates = make([]TrainingDate, len(trainingDates))
|
|
response[i].TrainingID = t.ID
|
|
for j, td := range trainingDates {
|
|
response[i].Dates[j] = TrainingDate{
|
|
Id: td.ID,
|
|
Date: types.Date{Time: td.Date},
|
|
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
|
Days: td.Days,
|
|
IsOnline: td.IsOnline,
|
|
Location: td.Location,
|
|
Address: td.Address,
|
|
Capacity: td.Capacity,
|
|
Price: Price{
|
|
Amount: td.PriceAmount.String(),
|
|
Currency: td.PriceCurrency,
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
return ListAllUpcomingTrainingDates200JSONResponse(response), nil
|
|
}
|
|
|
|
func (h *APIHandlers) ListTrainingUpcomingDates(ctx context.Context, req ListTrainingUpcomingDatesRequestObject) (ListTrainingUpcomingDatesResponseObject, error) {
|
|
trainingDates, err := h.trainingDateRepository.FindUpcomingByTrainingID(req.TrainingID)
|
|
if err != nil {
|
|
return ListTrainingUpcomingDates500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to list all upcoming training dates",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
data := make([]TrainingDate, len(trainingDates))
|
|
for idx, td := range trainingDates {
|
|
data[idx] = TrainingDate{
|
|
Id: td.ID,
|
|
Date: types.Date{Time: td.Date},
|
|
StartTime: td.StartTime.Format(trainingDateStartTimeFormat),
|
|
Days: td.Days,
|
|
IsOnline: td.IsOnline,
|
|
Location: td.Location,
|
|
Address: td.Address,
|
|
Capacity: td.Capacity,
|
|
Price: Price{
|
|
Amount: td.PriceAmount.String(),
|
|
Currency: td.PriceCurrency,
|
|
},
|
|
}
|
|
}
|
|
|
|
return ListTrainingUpcomingDates200JSONResponse(data), nil
|
|
}
|
|
|
|
func (h *APIHandlers) ListTrainingDateAttendees(ctx context.Context, req ListTrainingDateAttendeesRequestObject) (ListTrainingDateAttendeesResponseObject, error) {
|
|
attendees, err := h.trainingDateAttendeeRepository.FindAllByTrainingDateID(req.TrainingDateID)
|
|
if err != nil {
|
|
return ListTrainingDateAttendees500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to list training date attendees",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
data := make([]TrainingDateAttendee, len(attendees))
|
|
for idx, a := range attendees {
|
|
data[idx] = TrainingDateAttendee{
|
|
Id: a.ID,
|
|
Name: a.Name,
|
|
Email: types.Email(a.Email),
|
|
Phone: a.Phone,
|
|
Company: a.Company,
|
|
Position: a.Position,
|
|
IsStudent: &a.IsStudent,
|
|
HasPaid: a.HasPaid,
|
|
HasAttended: a.HasAttended,
|
|
Bill: Price{
|
|
Amount: a.BillAmount.String(),
|
|
Currency: a.BillCurrency,
|
|
},
|
|
}
|
|
}
|
|
|
|
return ListTrainingDateAttendees200JSONResponse(data), nil
|
|
}
|
|
|
|
func (h *APIHandlers) CreateTrainingDateAttendee(ctx context.Context, req CreateTrainingDateAttendeeRequestObject) (CreateTrainingDateAttendeeResponseObject, error) {
|
|
_, err := h.trainingRepository.FindByID(req.TrainingID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingNotFound) {
|
|
return CreateTrainingDateAttendee404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training not found",
|
|
}}, nil
|
|
} else {
|
|
return CreateTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
td, err := h.trainingDateRepository.FindByID(req.TrainingDateID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingDateNotFound) {
|
|
return CreateTrainingDateAttendee404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date not found",
|
|
}}, nil
|
|
} else {
|
|
return CreateTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
ta := training.Attendee{
|
|
Name: req.Body.Name,
|
|
Email: string(req.Body.Email),
|
|
Phone: req.Body.Phone,
|
|
Company: req.Body.Company,
|
|
Position: req.Body.Position,
|
|
IsStudent: *req.Body.IsStudent,
|
|
HasPaid: false,
|
|
HasAttended: false,
|
|
BillAmount: td.PriceAmount,
|
|
BillCurrency: td.PriceCurrency,
|
|
}
|
|
|
|
err = h.trainingDateAttendeeRepository.Create(req.TrainingDateID, &ta)
|
|
if err != nil {
|
|
return CreateTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to create training date attendee",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
return CreateTrainingDateAttendee201JSONResponse{
|
|
Id: ta.ID,
|
|
Name: ta.Name,
|
|
Email: types.Email(ta.Email),
|
|
Phone: ta.Phone,
|
|
Company: ta.Company,
|
|
Position: ta.Position,
|
|
IsStudent: &ta.IsStudent,
|
|
HasAttended: ta.HasAttended,
|
|
HasPaid: ta.HasPaid,
|
|
Bill: Price{
|
|
Amount: ta.BillAmount.String(),
|
|
Currency: ta.BillCurrency,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) GetTrainingDateAttendeeCount(ctx context.Context, req GetTrainingDateAttendeeCountRequestObject) (GetTrainingDateAttendeeCountResponseObject, error) {
|
|
attendees, err := h.trainingDateAttendeeRepository.FindAllByTrainingDateID(req.TrainingDateID)
|
|
if err != nil {
|
|
return GetTrainingDateAttendeeCount500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date attendee count",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
return GetTrainingDateAttendeeCount200JSONResponse{
|
|
Count: int8(len(attendees)),
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) DeleteTrainingDateAttendee(ctx context.Context, req DeleteTrainingDateAttendeeRequestObject) (DeleteTrainingDateAttendeeResponseObject, error) {
|
|
err := h.trainingDateAttendeeRepository.Delete(req.TrainingDateAttendeeID)
|
|
if errors.Is(err, training.ErrTrainingDateAttendeeNotFound) {
|
|
return DeleteTrainingDateAttendee404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date attendee not found",
|
|
}}, nil
|
|
} else if err != nil {
|
|
return DeleteTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to delete training date attendee",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
return DeleteTrainingDateAttendee204Response{}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) GetTrainingDateAttendee(ctx context.Context, req GetTrainingDateAttendeeRequestObject) (GetTrainingDateAttendeeResponseObject, error) {
|
|
_, err := h.trainingRepository.FindByID(req.TrainingID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingNotFound) {
|
|
return GetTrainingDateAttendee404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training not found",
|
|
}}, nil
|
|
} else {
|
|
return GetTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
_, err = h.trainingDateRepository.FindByID(req.TrainingDateID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingDateNotFound) {
|
|
return GetTrainingDateAttendee404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date not found",
|
|
}}, nil
|
|
} else {
|
|
return GetTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
ta, err := h.trainingDateAttendeeRepository.FindByID(req.TrainingDateAttendeeID)
|
|
if errors.Is(err, training.ErrTrainingDateAttendeeNotFound) {
|
|
return GetTrainingDateAttendee404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date attendee not found",
|
|
}}, nil
|
|
} else if err != nil {
|
|
return GetTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date attendee",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
return GetTrainingDateAttendee200JSONResponse{
|
|
Id: ta.ID,
|
|
Name: ta.Name,
|
|
Email: types.Email(ta.Email),
|
|
Phone: ta.Phone,
|
|
Company: ta.Company,
|
|
Position: ta.Position,
|
|
IsStudent: &ta.IsStudent,
|
|
HasAttended: ta.HasAttended,
|
|
HasPaid: ta.HasPaid,
|
|
Bill: Price{
|
|
Amount: ta.BillAmount.String(),
|
|
Currency: ta.BillCurrency,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) UpdateTrainingDateAttendee(ctx context.Context, req UpdateTrainingDateAttendeeRequestObject) (UpdateTrainingDateAttendeeResponseObject, error) {
|
|
_, err := h.trainingRepository.FindByID(req.TrainingID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingNotFound) {
|
|
return UpdateTrainingDateAttendee404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training not found",
|
|
}}, nil
|
|
} else {
|
|
return UpdateTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
_, err = h.trainingDateRepository.FindByID(req.TrainingDateID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingDateNotFound) {
|
|
return UpdateTrainingDateAttendee404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date not found",
|
|
}}, nil
|
|
} else {
|
|
return UpdateTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
ta, err := h.trainingDateAttendeeRepository.FindByID(req.TrainingDateAttendeeID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingDateAttendeeNotFound) {
|
|
return UpdateTrainingDateAttendee404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date attendee not found",
|
|
}}, nil
|
|
} else {
|
|
return UpdateTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date attendee",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
ta.Name = req.Body.Name
|
|
ta.Email = string(req.Body.Email)
|
|
ta.Phone = req.Body.Phone
|
|
ta.Company = req.Body.Company
|
|
ta.Position = req.Body.Position
|
|
ta.IsStudent = *req.Body.IsStudent
|
|
|
|
err = h.trainingDateAttendeeRepository.Update(ta)
|
|
if err != nil {
|
|
return UpdateTrainingDateAttendee500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to update training date attendee",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
return UpdateTrainingDateAttendee200JSONResponse{
|
|
Id: ta.ID,
|
|
Name: ta.Name,
|
|
Email: types.Email(ta.Email),
|
|
Phone: ta.Phone,
|
|
Company: ta.Company,
|
|
Position: ta.Position,
|
|
IsStudent: &ta.IsStudent,
|
|
HasAttended: ta.HasAttended,
|
|
HasPaid: ta.HasPaid,
|
|
Bill: Price{
|
|
Amount: ta.BillAmount.String(),
|
|
Currency: ta.BillCurrency,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) UpdateTrainingDateAttendeePayment(ctx context.Context, req UpdateTrainingDateAttendeePaymentRequestObject) (UpdateTrainingDateAttendeePaymentResponseObject, error) {
|
|
_, err := h.trainingRepository.FindByID(req.TrainingID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingNotFound) {
|
|
return UpdateTrainingDateAttendeePayment404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training not found",
|
|
}}, nil
|
|
} else {
|
|
return UpdateTrainingDateAttendeePayment500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
_, err = h.trainingDateRepository.FindByID(req.TrainingDateID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingDateNotFound) {
|
|
return UpdateTrainingDateAttendeePayment404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date not found",
|
|
}}, nil
|
|
} else {
|
|
return UpdateTrainingDateAttendeePayment500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
ta, err := h.trainingDateAttendeeRepository.FindByID(req.TrainingDateAttendeeID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingDateAttendeeNotFound) {
|
|
return UpdateTrainingDateAttendeePayment404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date attendee not found",
|
|
}}, nil
|
|
} else {
|
|
return UpdateTrainingDateAttendeePayment500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date attendee",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
ta.HasPaid = req.Body.Paid
|
|
|
|
err = h.trainingDateAttendeeRepository.Update(ta)
|
|
if err != nil {
|
|
return UpdateTrainingDateAttendeePayment500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to update training date attendee",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
return UpdateTrainingDateAttendeePayment200JSONResponse{
|
|
Id: ta.ID,
|
|
Name: ta.Name,
|
|
Email: types.Email(ta.Email),
|
|
Phone: ta.Phone,
|
|
Company: ta.Company,
|
|
Position: ta.Position,
|
|
IsStudent: &ta.IsStudent,
|
|
HasAttended: ta.HasAttended,
|
|
HasPaid: ta.HasPaid,
|
|
Bill: Price{
|
|
Amount: ta.BillAmount.String(),
|
|
Currency: ta.BillCurrency,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (h *APIHandlers) UpdateTrainingDateAttendeeAttendance(ctx context.Context, req UpdateTrainingDateAttendeeAttendanceRequestObject) (UpdateTrainingDateAttendeeAttendanceResponseObject, error) {
|
|
_, err := h.trainingRepository.FindByID(req.TrainingID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingNotFound) {
|
|
return UpdateTrainingDateAttendeeAttendance404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training not found",
|
|
}}, nil
|
|
} else {
|
|
return UpdateTrainingDateAttendeeAttendance500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
_, err = h.trainingDateRepository.FindByID(req.TrainingDateID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingDateNotFound) {
|
|
return UpdateTrainingDateAttendeeAttendance404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date not found",
|
|
}}, nil
|
|
} else {
|
|
return UpdateTrainingDateAttendeeAttendance500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
ta, err := h.trainingDateAttendeeRepository.FindByID(req.TrainingDateAttendeeID)
|
|
if err != nil {
|
|
if errors.Is(err, training.ErrTrainingDateAttendeeNotFound) {
|
|
return UpdateTrainingDateAttendeeAttendance404ApplicationProblemPlusJSONResponse{
|
|
NotFoundErrorApplicationProblemPlusJSONResponse: NotFoundErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusNotFound,
|
|
Title: "Not Found: Training date attendee not found",
|
|
}}, nil
|
|
} else {
|
|
return UpdateTrainingDateAttendeeAttendance500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to get training date attendee",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
}
|
|
|
|
ta.HasAttended = req.Body.Attended
|
|
|
|
err = h.trainingDateAttendeeRepository.Update(ta)
|
|
if err != nil {
|
|
return UpdateTrainingDateAttendeeAttendance500ApplicationProblemPlusJSONResponse{
|
|
InternalErrorApplicationProblemPlusJSONResponse: InternalErrorApplicationProblemPlusJSONResponse{
|
|
Status: fiber.StatusInternalServerError,
|
|
Title: "Internal Server Error: Failed to update training date attendee",
|
|
Detail: err.Error(),
|
|
}}, nil
|
|
}
|
|
|
|
return UpdateTrainingDateAttendeeAttendance200JSONResponse{
|
|
Id: ta.ID,
|
|
Name: ta.Name,
|
|
Email: types.Email(ta.Email),
|
|
Phone: ta.Phone,
|
|
Company: ta.Company,
|
|
Position: ta.Position,
|
|
IsStudent: &ta.IsStudent,
|
|
HasAttended: ta.HasAttended,
|
|
HasPaid: ta.HasPaid,
|
|
Bill: Price{
|
|
Amount: ta.BillAmount.String(),
|
|
Currency: ta.BillCurrency,
|
|
},
|
|
}, nil
|
|
}
|