diff options
author | Kierán Meinhardt <kmein@posteo.de> | 2024-10-11 16:53:42 +0200 |
---|---|---|
committer | Kierán Meinhardt <kmein@posteo.de> | 2024-10-11 16:53:42 +0200 |
commit | 08d3a9f867bd92dafa3dc5ccc61254d25993130e (patch) | |
tree | 8a4336f91d55cf82581f80c59b867c16913d338c | |
parent | 4e2bca263be212f5795b845a68cf218ff237d8ab (diff) |
autogenerate collection item IDs
-rw-r--r-- | backend/backend.cabal | 1 | ||||
-rw-r--r-- | backend/lib/ACMS/API/REST/Collection.hs | 8 | ||||
-rw-r--r-- | cli/app/Main.hs | 62 | ||||
-rw-r--r-- | docs/get-started-cli.md | 6 |
4 files changed, 42 insertions, 35 deletions
diff --git a/backend/backend.cabal b/backend/backend.cabal index 4909bdf..f92dd46 100644 --- a/backend/backend.cabal +++ b/backend/backend.cabal @@ -41,6 +41,7 @@ library tagged, text, utf8-string, + uuid, wai, warp diff --git a/backend/lib/ACMS/API/REST/Collection.hs b/backend/lib/ACMS/API/REST/Collection.hs index bfbd637..ed0ae9a 100644 --- a/backend/lib/ACMS/API/REST/Collection.hs +++ b/backend/lib/ACMS/API/REST/Collection.hs @@ -8,6 +8,8 @@ import Data.Function ((&)) import Data.Text qualified as T import Network.HTTP.Simple import Text.Printf (printf) +import Data.UUID qualified as U +import Data.UUID.V4 qualified as U type CollectionName = T.Text @@ -37,8 +39,10 @@ update c i o = & httpLBS >>= A.throwDecode . getResponseBody -create :: T.Text -> T.Text -> A.Object -> IO () -create c i o = +create :: T.Text -> A.Object -> IO () +create c o = do + uuid <- U.nextRandom + let i = U.toText uuid <> ".json" "http://localhost:8081" & setRequestMethod "POST" & setRequestBodyLBS diff --git a/cli/app/Main.hs b/cli/app/Main.hs index b832f14..fb35b42 100644 --- a/cli/app/Main.hs +++ b/cli/app/Main.hs @@ -25,10 +25,7 @@ data Args = Args args :: O.Parser Args args = Args <$> cmd_ -data Cmd = Collection - { operation :: CollectionCmd - , filePath :: CollectionPath - } +data Cmd = Collection CollectionCmd cmd_ :: O.Parser Cmd cmd_ = @@ -37,16 +34,23 @@ cmd_ = O.progDesc "Manage content collections" ] -data CollectionCmd = CollectionAdd | CollectionView | CollectionEdit | CollectionDelete +data CollectionCmd + = CollectionAdd CollectionName + | CollectionView CollectionPath + | CollectionEdit CollectionPath + | CollectionDelete CollectionPath + +newtype CollectionName = CollectionName T.Text + deriving (Read) data CollectionPath = CollectionPath - { collectionName :: T.Text, + { collectionName :: CollectionName, fileName :: T.Text } instance Read CollectionPath where readPrec = R.lift do - (T.pack -> collectionName) <- R.munch (/= '/') + (CollectionName . T.pack -> collectionName) <- R.munch (/= '/') _ <- R.string "/" (T.pack -> fileName) <- do fileName <- R.munch (liftA2 (&&) (/= '.') (/= '/')) @@ -55,51 +59,49 @@ instance Read CollectionPath where pure CollectionPath {..} instance Show CollectionPath where - show (CollectionPath {collectionName, fileName}) = - show (collectionName <> "/" <> fileName) + show (CollectionPath {collectionName = CollectionName cn, fileName}) = + show (cn <> "/" <> fileName) collectionCmd :: O.Parser Cmd collectionCmd = do - operation <- O.hsubparser $ mconcat $ - [ O.command "add" . O.info (pure CollectionAdd) $ + fmap Collection . O.hsubparser . mconcat $ + [ O.command "add" . O.info (CollectionAdd <$> collectionNameArg) $ O.progDesc "Add an entity" - , O.command "view" . O.info (pure CollectionView) $ + , O.command "view" . O.info (CollectionView <$> collectionPathArg) $ O.progDesc "View an entity" - , O.command "edit" . O.info (pure CollectionEdit) $ + , O.command "edit" . O.info (CollectionEdit <$> collectionPathArg) $ O.progDesc "Edit an entity" - , O.command "delete" . O.info (pure CollectionDelete) $ + , O.command "delete" . O.info (CollectionDelete <$> collectionPathArg) $ O.progDesc "Delete an entity" ] - filePath <- collectionPathArg - pure $ Collection {..} collectionPathArg :: O.Parser CollectionPath collectionPathArg = O.argument O.auto (O.metavar "COLLECTION_PATH") +collectionNameArg :: O.Parser CollectionName +collectionNameArg = + CollectionName . T.pack <$> O.strArgument (O.metavar "COLLECTION_NAME") + main :: IO () main = O.execParser (O.info (args <**> O.helper) O.idm) >>= \case Args - { cmd = - Collection - { operation = operation - , filePath = CollectionPath {collectionName, fileName} - } - } -> case operation of - CollectionAdd -> + { cmd = Collection cmd + } -> case cmd of + CollectionAdd (CollectionName cn) -> do print - =<< ACMS.API.REST.Collection.create collectionName fileName + =<< ACMS.API.REST.Collection.create cn =<< J.throwDecode =<< LB.getContents - CollectionView -> + CollectionView CollectionPath {collectionName = CollectionName cn, fileName} -> print - =<< ACMS.API.REST.Collection.read collectionName fileName - CollectionDelete -> + =<< ACMS.API.REST.Collection.read cn fileName + CollectionDelete CollectionPath {collectionName = CollectionName cn, fileName}-> print - =<< ACMS.API.REST.Collection.delete collectionName fileName - CollectionEdit -> + =<< ACMS.API.REST.Collection.delete cn fileName + CollectionEdit CollectionPath {collectionName = CollectionName cn, fileName}-> print - =<< ACMS.API.REST.Collection.update collectionName fileName + =<< ACMS.API.REST.Collection.update cn fileName =<< J.throwDecode =<< LB.getContents diff --git a/docs/get-started-cli.md b/docs/get-started-cli.md index ddf5102..93806c4 100644 --- a/docs/get-started-cli.md +++ b/docs/get-started-cli.md @@ -13,7 +13,7 @@ export ACMS_CONTENT=$PWD/content ## Create a restaurant collection type ```console -acms collection insert restaurant/1.json <<'EOF' +acms collection add restaurant <<'EOF' { "name": "Biscotte Restaurant", "description": "Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers." @@ -24,7 +24,7 @@ EOF ## Create a category collection type ```console -acms collection insert category/1.json <<'EOF' +acms collection add category <<'EOF' { "name": "French Food", "restaurant": "1.json" @@ -33,7 +33,7 @@ EOF ``` ```console -acms collection insert category/2.json <<'EOF' +acms collection add category <<'EOF' { "name": "Brunch", "restaurant": "1.json" |