1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-27 10:10:50 +03:00

[backend] Don't check for category title duplication (#301)

This commit is contained in:
Artyom Kazak 2019-06-26 12:42:28 +03:00 committed by GitHub
parent 1b22041506
commit 0a0092a205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 36 deletions

View File

@ -50,24 +50,12 @@ 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)
@ -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

View File

@ -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"

View File

@ -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)
-- Return the URL of the new category
Spock.text ("/haskell/" <> categorySlug newCategory)
-- New item in a category
Spock.post (addRoute <//> categoryVar <//> "item") $ \catId -> do