From 1aa4adba650153e22cf549526e6f9ce16bcc8790 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 16 Aug 2022 15:54:15 -0500 Subject: [PATCH] Add endpoint to find Owner by ID --- src/Owner.hs | 5 +++++ src/ZGoBackend.hs | 16 ++++++++++++++++ test/Spec.hs | 7 ++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Owner.hs b/src/Owner.hs index b6d81d7..fbbeaa6 100644 --- a/src/Owner.hs +++ b/src/Owner.hs @@ -287,3 +287,8 @@ upsertOwner o = do -- | Function to get an Owner findOwner :: T.Text -> Action IO (Maybe Document) findOwner zaddy = findOne (select ["address" =: zaddy] "owners") + +-- | Function to get an Owner by id +findOwnerById :: T.Text -> Action IO (Maybe Document) +findOwnerById i = + findOne (select ["_id" =: (read (T.unpack i) :: ObjectId)] "owners") diff --git a/src/ZGoBackend.hs b/src/ZGoBackend.hs index 08d76c4..9c3fae2 100644 --- a/src/ZGoBackend.hs +++ b/src/ZGoBackend.hs @@ -586,6 +586,22 @@ routes pipe config = do [ "message" .= ("Owner found!" :: String) , "owner" .= toJSON (q :: Owner) ]) + get "/api/ownerid" $ do + id <- param "id" + owner <- liftIO $ run (findOwnerById id) + case owner of + Nothing -> status noContent204 + Just o -> do + let pOwner = cast' (Doc o) + case pOwner of + Nothing -> status internalServerError500 + Just q -> do + status ok200 + Web.Scotty.json + (object + [ "message" .= ("Owner found!" :: String) + , "owner" .= toJSON (q :: Owner) + ]) --Upsert owner to DB post "/api/owner" $ do o <- jsonData diff --git a/test/Spec.hs b/test/Spec.hs index 4b80bfe..b36350a 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -139,7 +139,7 @@ main = do getResponseStatus res `shouldBe` ok200 describe "Owner endpoint" $ do prop "add owner" testOwnerAdd - it "return owner" $ do + it "return owner by address" $ do req <- testGet "/api/owner" @@ -149,6 +149,11 @@ main = do ] res <- httpJSON req getResponseStatus (res :: Response A.Value) `shouldBe` ok200 + it "return owner by id" $ do + req <- + testGet "/api/ownerid" [("id", Just "627ad3492b05a76be3000001")] + res <- httpLBS req + getResponseStatus res `shouldBe` ok200 describe "Order endpoint" $ do prop "upsert order" testOrderAdd it "get order by session" $ do