Skip to content

Commit

Permalink
Implemented activities/emotions duplication protection in /api/days
Browse files Browse the repository at this point in the history
  • Loading branch information
kolayne committed Feb 8, 2021
1 parent 7e7f35d commit aeb1c6e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions database_scheme.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ CREATE TABLE "activities_and_emotions" (
"type_id" integer REFERENCES types_of_activities_and_emotions NOT NULL,
"day_id" integer REFERENCES days ON DELETE CASCADE NOT NULL,
"proportion" integer NOT NULL,
UNIQUE (type_id, day_id), -- A day can't contain the same activity/emotion twice
CONSTRAINT type_owned_by_correct_user_check CHECK (does_activity_or_emotion_belong_to_user_of_the_day(type_id,day_id))
);
9 changes: 7 additions & 2 deletions src/webhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,13 @@ func HandleAPIDays(w http.ResponseWriter, r *http.Request) {
return
}

// TODO: also implement and document the handling of the case when the request contained activity/emotion
// multiple times
if pgErr.Constraint == "activities_and_emotions_type_id_day_id_key" {
writeJSON(w,
map[string]interface{}{"ok": false, "error": "Activity/emotion type id can't be mentioned more " +
"than once", "error_type": "duplicated_type"},
http.StatusBadRequest)
return
}
}
panicIfError(err)
}
Expand Down

0 comments on commit aeb1c6e

Please sign in to comment.