From 0a0092a205b3588699bf0c98724d2bf903b06229 Mon Sep 17 00:00:00 2001 From: Artyom Kazak Date: Wed, 26 Jun 2019 12:42:28 +0300 Subject: [PATCH] [backend] Don't check for category title duplication (#301) --- back/src/Guide/Api/Methods.hs | 27 +++++++-------------------- back/src/Guide/Api/Types.hs | 4 +--- back/src/Guide/Handlers.hs | 19 ++++++------------- 3 files changed, 14 insertions(+), 36 deletions(-) diff --git a/back/src/Guide/Api/Methods.hs b/back/src/Guide/Api/Methods.hs index d4ad69c..2210221 100644 --- a/back/src/Guide/Api/Methods.hs +++ b/back/src/Guide/Api/Methods.hs @@ -50,28 +50,16 @@ getCategory catId = -- | Create a new category, given the title and the grandparent (aka group). -- --- Returns the ID of the created category (or of the existing one if the --- category with this title exists already). +-- Returns the ID of the created category. createCategory :: Text -> Text -> Guider (Uid Category) createCategory title' group' = logHandler "createCategory" [attr "title" title', attr "group" group'] $ do when (T.null title') $ throwError err400{errReasonPhrase = "Title not provided"} when (T.null group') $ throwError err400{errReasonPhrase = "Group not provided"} - -- If the category exists already, don't create it - cats <- view categories <$> dbQuery GetGlobalState - let isDuplicate cat = T.toCaseFold (cat^.title) == T.toCaseFold title' - && T.toCaseFold (cat^.group_) == T.toCaseFold group' - case find isDuplicate cats of - Just c -> do - logDebug $ format - "Found a category with the same title and group (id {}), \ - \will not create a new one" (c^.uid) - return (c^.uid) - Nothing -> do - catId <- randomShortUid - time <- liftIO getCurrentTime - addEdit . fst =<< dbUpdate (AddCategory catId title' group' time) - return catId + catId <- randomShortUid + time <- liftIO getCurrentTime + addEdit . fst =<< dbUpdate (AddCategory catId title' group' time) + return catId -- | Edit category's note. setCategoryNotes :: Uid Category -> CTextEdit -> Guider NoContent @@ -111,7 +99,7 @@ deleteCategory catId = -- Items ---------------------------------------------------------------------------- --- | Get item by item id +-- | Get item by item ID. getItem :: Uid Item -> Guider CItemFull getItem itemId = logHandler "getItem" [attr "itemId" itemId] $ do @@ -119,8 +107,7 @@ getItem itemId = -- | Create a new item, given the name. -- --- Returns the ID of the created item. Unlike 'createCategory', allows items --- with duplicated names. +-- Returns the ID of the created item. createItem :: Uid Category -> Text -> Guider (Uid Item) createItem catId name' = logHandler "createItem" [attr "catId" catId, attr "name" name'] $ do diff --git a/back/src/Guide/Api/Types.hs b/back/src/Guide/Api/Types.hs index 4ecb1f6..12aa77c 100644 --- a/back/src/Guide/Api/Types.hs +++ b/back/src/Guide/Api/Types.hs @@ -104,9 +104,7 @@ data CategorySite route = CategorySite , _createCategory :: route :- Summary "Create a new category" - :> Description "Returns the ID of the created category.\n\n\ - \If a category with the same title already exists \ - \in the group, returns its ID instead." + :> Description "Returns the ID of the created category." :> ErrorResponse 400 "'title' not provided" :> ErrorResponse 400 "'group' not provided" :> "category" diff --git a/back/src/Guide/Handlers.hs b/back/src/Guide/Handlers.hs index 9852fd0..ca5cdc7 100644 --- a/back/src/Guide/Handlers.hs +++ b/back/src/Guide/Handlers.hs @@ -242,19 +242,12 @@ addMethods = do -- New category Spock.post (addRoute "category") $ do title' <- param' "content" - -- If the category exists already, don't create it - cats <- view categories <$> dbQuery GetGlobalState - let hasSameTitle cat = T.toCaseFold (cat^.title) == T.toCaseFold title' - category <- case find hasSameTitle cats of - Just c -> return c - Nothing -> do - catId <- randomShortUid - time <- liftIO getCurrentTime - (edit, newCategory) <- dbUpdate (AddCategory catId title' "Miscellaneous" time) - addEdit edit - return newCategory - -- And now send the URL of the new (or old) category - Spock.text ("/haskell/" <> categorySlug category) + catId <- randomShortUid + time <- liftIO getCurrentTime + (edit, newCategory) <- dbUpdate (AddCategory catId title' "Miscellaneous" time) + addEdit edit + -- Return the URL of the new category + Spock.text ("/haskell/" <> categorySlug newCategory) -- New item in a category Spock.post (addRoute categoryVar "item") $ \catId -> do