From a419067635e08a8bbaa9a1eeefe075eecaa7d835 Mon Sep 17 00:00:00 2001 From: ImSoZRious <30285202+ImSoZRious@users.noreply.github.com> Date: Thu, 11 Jan 2024 00:04:03 +0700 Subject: [PATCH] fix: non-registerable event --- apperror/error.go | 1 + internal/evtreg/evtreg.repository.go | 2 +- internal/evtreg/evtreg.service.go | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/apperror/error.go b/apperror/error.go index b83e8b6..e49a5b8 100644 --- a/apperror/error.go +++ b/apperror/error.go @@ -27,4 +27,5 @@ var ( ScheduleNotFound = &AppError{"schedule-not-found", http.StatusNotFound} DuplicatePeriod = &AppError{"duplicate-period", http.StatusConflict} ScheduleFull = &AppError{"schedule-full", http.StatusBadRequest} + NonRegisterableEvent = &AppError{"non-registerable-event", http.StatusBadRequest} ) diff --git a/internal/evtreg/evtreg.repository.go b/internal/evtreg/evtreg.repository.go index 86928dc..70bdf14 100644 --- a/internal/evtreg/evtreg.repository.go +++ b/internal/evtreg/evtreg.repository.go @@ -28,7 +28,7 @@ func (r *repositoryImpl) GetUserWithEventRegistrationByEmail(user *model.User, e } func (r *repositoryImpl) GetScheduleById(schedule *model.Schedule, scheduleId int) error { - return r.db.Model(schedule).Where("id = ?", scheduleId).First(schedule).Error + return r.db.Model(schedule).Preload("Event").Where("id = ?", scheduleId).First(schedule).Error } func (r *repositoryImpl) RegisterEvent(evtreg *model.EventRegistration) error { diff --git a/internal/evtreg/evtreg.service.go b/internal/evtreg/evtreg.service.go index 1a3b6c1..849416d 100644 --- a/internal/evtreg/evtreg.service.go +++ b/internal/evtreg/evtreg.service.go @@ -50,6 +50,10 @@ func (h *serviceImpl) RegisterEvent(ctx context.Context, userEmail string, sched return apperror.InternalError } + if !schedule.Event.RequireRegistration { + return apperror.NonRegisterableEvent + } + for _, regEvt := range user.RegisteredEvents { if regEvt.Schedule.Period == schedule.Period { return apperror.DuplicatePeriod