Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions documentation/docs/reference/services/Event.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,15 @@ Response format:

```

PUT /event/code/{id}/
POST /event/code/
----------------------------

Updates a struct that contains information about the event code (generated upon event creation) and expiration time.
Upserts a struct that contains information about the event code (generated upon event creation) and expiration time.

Request format:
```
{
"id": "52fdfc072182654f163f5f0f9a621d72",
"code": "new_code",
"expiration": 1521388800
}
Expand Down
12 changes: 6 additions & 6 deletions gateway/services/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ var EventRoutes = arbor.RouteCollection{
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(GetEventCode).ServeHTTP,
},
arbor.Route{
"UpdateEventCode",
"PUT",
"/event/code/{id}/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(PutEventCode).ServeHTTP,
"UpsertEventCode",
"POST",
"/event/code/",
alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole, models.StaffRole}), middleware.IdentificationMiddleware).ThenFunc(PostEventCode).ServeHTTP,
},
arbor.Route{
"Checkin",
Expand Down Expand Up @@ -129,8 +129,8 @@ func GetEventCode(w http.ResponseWriter, r *http.Request) {
arbor.GET(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r)
}

func PutEventCode(w http.ResponseWriter, r *http.Request) {
arbor.PUT(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r)
func PostEventCode(w http.ResponseWriter, r *http.Request) {
arbor.POST(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r)
}

func Checkin(w http.ResponseWriter, r *http.Request) {
Expand Down
21 changes: 6 additions & 15 deletions services/event/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func SetupController(route *mux.Route) {
metrics.RegisterHandler("/", UpdateEvent, "PUT", router)
metrics.RegisterHandler("/", GetAllEvents, "GET", router)
metrics.RegisterHandler("/code/{id}/", GetEventCode, "GET", router)
metrics.RegisterHandler("/code/{id}/", UpdateEventCode, "PUT", router)
metrics.RegisterHandler("/code/", UpsertEventCode, "POST", router)

metrics.RegisterHandler("/checkin/", Checkin, "POST", router)

Expand Down Expand Up @@ -177,36 +177,27 @@ func GetEventCode(w http.ResponseWriter, r *http.Request) {
}

/*
Endpoint to update an event code and end time
Endpoint to upsert an event code and end time
*/
func UpdateEventCode(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]

if id == "" {
errors.WriteError(w, r, errors.MalformedRequestError("Must provide event id in request url.", "Must provide event id in request url."))
return
}

func UpsertEventCode(w http.ResponseWriter, r *http.Request) {
var eventCode models.EventCode
json.NewDecoder(r.Body).Decode(&eventCode)

eventCode.ID = id

err := service.UpdateEventCode(id, eventCode)
err := service.UpsertEventCode(eventCode.Code, eventCode)

if err != nil {
errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not update the code and timestamp of the event."))
return
}

updated_event, err := service.GetEventCode(id)
updated_code, err := service.GetEventCode(eventCode.ID)

if err != nil {
errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get updated event code and timestamp details."))
return
}

json.NewEncoder(w).Encode(updated_event)
json.NewEncoder(w).Encode(updated_code)
}

/*
Expand Down
7 changes: 4 additions & 3 deletions services/event/service/event_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,14 +515,15 @@ func GetEventCode(id string) (*models.EventCode, error) {
}

/*
Updates the event code and end time with the given id
Upserts the event code and end time with the given id
If no matching code is not found, then it is a new code and add it
*/
func UpdateEventCode(id string, eventCode models.EventCode) error {
func UpsertEventCode(id string, eventCode models.EventCode) error {
selector := database.QuerySelector{
"id": id,
}

err := db.Update("eventcodes", selector, &eventCode)
_, err := db.Upsert("eventcodes", selector, &eventCode)

return err
}