From 3b681e32f6de19298d424d01e473ffe5e0aaa874 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Wed, 8 Aug 2018 18:22:41 +0900 Subject: [PATCH] Add backend for creating a category --- src/forum.nim | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/forum.nim b/src/forum.nim index fc6e81a..ac67c51 100644 --- a/src/forum.nim +++ b/src/forum.nim @@ -665,6 +665,12 @@ proc executeLike(c: TForumData, postId: int) = # Save the like. exec(db, crud(crCreate, "like", "author", "post"), c.userid, postId) +proc executeNewCategory(c: TForumData, name, color, description: string): int64 = + if name.len == 0: + raise newForumError("Category name must not be empty!", @["name"]) + + result = insertID(db, crud(crCreate, "category", "name", "color", "description"), name, color, description) + proc executeUnlike(c: TForumData, postId: int) = # Verify the post and like exists for the current user. const likeQuery = sql""" @@ -1051,6 +1057,21 @@ routes: except ForumError as exc: resp Http400, $(%exc.data), "application/json" + post "/createCategory": + createTFD() + let formData = request.formData + + let name = formData["name"].body + let color = formData["color"].body.replace("#", "") + let description = formData["description"].body + + try: + let id = executeNewCategory(c, name, color, description) + let category = Category(id: id.int, name: name, color: color, description: description) + resp Http200, $(%category), "application/json" + except ForumError as exc: + resp Http400, $(%exc.data), "application/json" + get "/status.json": createTFD()