Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Add conflict errors for duplicate registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
TakshPSingh committed Nov 4, 2021
1 parent b16a906 commit 49b6081
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions common/errors/ConflictError.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package errors

import "net/http"

// An error that occurs when an incoming request tries to create a resource that already exists.
func ConflictError(raw_error string, message string) ApiError {
return ApiError{Status: http.StatusConflict, Type: "CONFLICT_ERROR", Message: message, RawError: raw_error}
}
8 changes: 8 additions & 0 deletions services/registration/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func CreateCurrentUserRegistration(w http.ResponseWriter, r *http.Request) {
err = service.CreateUserRegistration(id, user_registration)

if err != nil {
if err.Error() == service.RegistrationAlreadyExists {
errors.WriteError(w, r, errors.ConflictError(err.Error(), "Could not create user registration."))
return
}
errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not create user registration."))
return
}
Expand Down Expand Up @@ -324,6 +328,10 @@ func CreateCurrentMentorRegistration(w http.ResponseWriter, r *http.Request) {
err = service.CreateMentorRegistration(id, mentor_registration)

if err != nil {
if err.Error() == service.RegistrationAlreadyExists {
errors.WriteError(w, r, errors.ConflictError(err.Error(), "Could not create mentor registration."))
return
}
errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not create mentor registration."))
return
}
Expand Down
6 changes: 4 additions & 2 deletions services/registration/service/registration_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var validate *validator.Validate

var db database.Database

const RegistrationAlreadyExists = "Registration already exists."

func Initialize() error {
if db != nil {
db.Close()
Expand Down Expand Up @@ -64,7 +66,7 @@ func CreateUserRegistration(id string, user_registration models.UserRegistration
if err != nil {
return err
}
return errors.New("Registration already exists.")
return errors.New(RegistrationAlreadyExists)
}

err = db.Insert("attendees", &user_registration)
Expand Down Expand Up @@ -189,7 +191,7 @@ func CreateMentorRegistration(id string, mentor_registration models.MentorRegist
if err != nil {
return err
}
return errors.New("Registration already exists")
return errors.New(RegistrationAlreadyExists)
}

err = db.Insert("mentors", &mentor_registration)
Expand Down

0 comments on commit 49b6081

Please sign in to comment.