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:
parent
1b22041506
commit
0a0092a205
@ -50,28 +50,16 @@ getCategory catId =
|
|||||||
|
|
||||||
-- | Create a new category, given the title and the grandparent (aka group).
|
-- | 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
|
-- Returns the ID of the created category.
|
||||||
-- category with this title exists already).
|
|
||||||
createCategory :: Text -> Text -> Guider (Uid Category)
|
createCategory :: Text -> Text -> Guider (Uid Category)
|
||||||
createCategory title' group' =
|
createCategory title' group' =
|
||||||
logHandler "createCategory" [attr "title" title', attr "group" group'] $ do
|
logHandler "createCategory" [attr "title" title', attr "group" group'] $ do
|
||||||
when (T.null title') $ throwError err400{errReasonPhrase = "Title not provided"}
|
when (T.null title') $ throwError err400{errReasonPhrase = "Title not provided"}
|
||||||
when (T.null group') $ throwError err400{errReasonPhrase = "Group not provided"}
|
when (T.null group') $ throwError err400{errReasonPhrase = "Group not provided"}
|
||||||
-- If the category exists already, don't create it
|
catId <- randomShortUid
|
||||||
cats <- view categories <$> dbQuery GetGlobalState
|
time <- liftIO getCurrentTime
|
||||||
let isDuplicate cat = T.toCaseFold (cat^.title) == T.toCaseFold title'
|
addEdit . fst =<< dbUpdate (AddCategory catId title' group' time)
|
||||||
&& T.toCaseFold (cat^.group_) == T.toCaseFold group'
|
return catId
|
||||||
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
|
|
||||||
|
|
||||||
-- | Edit category's note.
|
-- | Edit category's note.
|
||||||
setCategoryNotes :: Uid Category -> CTextEdit -> Guider NoContent
|
setCategoryNotes :: Uid Category -> CTextEdit -> Guider NoContent
|
||||||
@ -111,7 +99,7 @@ deleteCategory catId =
|
|||||||
-- Items
|
-- Items
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
|
|
||||||
-- | Get item by item id
|
-- | Get item by item ID.
|
||||||
getItem :: Uid Item -> Guider CItemFull
|
getItem :: Uid Item -> Guider CItemFull
|
||||||
getItem itemId =
|
getItem itemId =
|
||||||
logHandler "getItem" [attr "itemId" itemId] $ do
|
logHandler "getItem" [attr "itemId" itemId] $ do
|
||||||
@ -119,8 +107,7 @@ getItem itemId =
|
|||||||
|
|
||||||
-- | Create a new item, given the name.
|
-- | Create a new item, given the name.
|
||||||
--
|
--
|
||||||
-- Returns the ID of the created item. Unlike 'createCategory', allows items
|
-- Returns the ID of the created item.
|
||||||
-- with duplicated names.
|
|
||||||
createItem :: Uid Category -> Text -> Guider (Uid Item)
|
createItem :: Uid Category -> Text -> Guider (Uid Item)
|
||||||
createItem catId name' =
|
createItem catId name' =
|
||||||
logHandler "createItem" [attr "catId" catId, attr "name" name'] $ do
|
logHandler "createItem" [attr "catId" catId, attr "name" name'] $ do
|
||||||
|
@ -104,9 +104,7 @@ data CategorySite route = CategorySite
|
|||||||
|
|
||||||
, _createCategory :: route :-
|
, _createCategory :: route :-
|
||||||
Summary "Create a new category"
|
Summary "Create a new category"
|
||||||
:> Description "Returns the ID of the created category.\n\n\
|
:> Description "Returns the ID of the created category."
|
||||||
\If a category with the same title already exists \
|
|
||||||
\in the group, returns its ID instead."
|
|
||||||
:> ErrorResponse 400 "'title' not provided"
|
:> ErrorResponse 400 "'title' not provided"
|
||||||
:> ErrorResponse 400 "'group' not provided"
|
:> ErrorResponse 400 "'group' not provided"
|
||||||
:> "category"
|
:> "category"
|
||||||
|
@ -242,19 +242,12 @@ addMethods = do
|
|||||||
-- New category
|
-- New category
|
||||||
Spock.post (addRoute <//> "category") $ do
|
Spock.post (addRoute <//> "category") $ do
|
||||||
title' <- param' "content"
|
title' <- param' "content"
|
||||||
-- If the category exists already, don't create it
|
catId <- randomShortUid
|
||||||
cats <- view categories <$> dbQuery GetGlobalState
|
time <- liftIO getCurrentTime
|
||||||
let hasSameTitle cat = T.toCaseFold (cat^.title) == T.toCaseFold title'
|
(edit, newCategory) <- dbUpdate (AddCategory catId title' "Miscellaneous" time)
|
||||||
category <- case find hasSameTitle cats of
|
addEdit edit
|
||||||
Just c -> return c
|
-- Return the URL of the new category
|
||||||
Nothing -> do
|
Spock.text ("/haskell/" <> categorySlug newCategory)
|
||||||
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)
|
|
||||||
|
|
||||||
-- New item in a category
|
-- New item in a category
|
||||||
Spock.post (addRoute <//> categoryVar <//> "item") $ \catId -> do
|
Spock.post (addRoute <//> categoryVar <//> "item") $ \catId -> do
|
||||||
|
Loading…
Reference in New Issue
Block a user