diff --git a/database_scheme.sql b/database_scheme.sql index a00a149..acafd36 100644 --- a/database_scheme.sql +++ b/database_scheme.sql @@ -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)) ); diff --git a/src/webhandlers.go b/src/webhandlers.go index 7bb6200..4159d75 100644 --- a/src/webhandlers.go +++ b/src/webhandlers.go @@ -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) }