mirror of
https://github.com/aelve/guide.git
synced 2024-11-27 00:14:03 +03:00
[backend] Don't check for category title duplication (#301)
This commit is contained in:
parent
1b22041506
commit
0a0092a205
@ -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
|
||||
|
@ -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"
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user