fixing things and adding beverage update

This commit is contained in:
nek0 2019-07-18 16:58:22 +02:00
parent 9e925816e4
commit 920bed1fa3
4 changed files with 58 additions and 12 deletions

View file

@ -35,6 +35,8 @@ type UserAPI =
( "list" :> Get '[JSON] [Beverage] ( "list" :> Get '[JSON] [Beverage]
:<|> "new" :> AuthProtect "header-auth" :> ReqBody '[JSON] BeverageSubmit :<|> "new" :> AuthProtect "header-auth" :> ReqBody '[JSON] BeverageSubmit
:> Post '[JSON] Int :> Post '[JSON] Int
:<|> "update" :> AuthProtect "header-auth"
:> Capture "id" Int :> ReqBody '[JSON] BeverageSubmit :> Post '[JSON] ()
) )
:<|> "auth" :> :<|> "auth" :>
( "get" :> ReqBody '[JSON] Int :> Post '[JSON] AuthInfo ( "get" :> ReqBody '[JSON] Int :> Post '[JSON] AuthInfo

View file

@ -116,7 +116,7 @@ users =
userGetUpdate :: Maybe Int -> Int -> MateHandler UserDetails userGetUpdate :: Maybe Int -> Int -> MateHandler UserDetails
userGetUpdate Nothing _ = userGetUpdate Nothing _ =
throwError $ err403 throwError $ err403
{ errBody = "No Authorization present" { errBody = "No Authentication present"
} }
userGetUpdate (Just aid) id = userGetUpdate (Just aid) id =
if aid == id if aid == id
@ -127,13 +127,13 @@ users =
userDetailsSelect conn id userDetailsSelect conn id
else else
throwError $ err403 throwError $ err403
{ errBody = "Wrong Authorization present" { errBody = "Wrong Authentication present"
} }
userPostUpdate :: Maybe Int -> Int -> UserDetailsSubmit -> MateHandler () userPostUpdate :: Maybe Int -> Int -> UserDetailsSubmit -> MateHandler ()
userPostUpdate Nothing _ _ = use359c65b0e68b6607a03d39f908ca26827ab97fb6e21096rPostUpdate Nothing _ _ =
throwError $ err403 throwError $ err403
{ errBody = "No Authorization present" { errBody = "No Authentication present"
} }
userPostUpdate (Just aid) id uds = userPostUpdate (Just aid) id uds =
if aid == id if aid == id
@ -143,12 +143,13 @@ users =
void $ liftIO $ runUpdate_ conn (updateUserDetails id uds (utctDay now)) void $ liftIO $ runUpdate_ conn (updateUserDetails id uds (utctDay now))
else else
throwError $ err403 throwError $ err403
{ errBody = "Wrong Authorization present" { errBody = "Wrong Authentication present"
} }
beverages = beverages =
list :<|> list :<|>
new new :<|>
update
where where
list :: MateHandler [Beverage] list :: MateHandler [Beverage]
list = do list = do
@ -156,9 +157,18 @@ beverages =
beverageSelect conn beverageSelect conn
new :: Maybe Int -> BeverageSubmit -> MateHandler Int new :: Maybe Int -> BeverageSubmit -> MateHandler Int
new _ bevsub = do new (Just _) bevsub = do
conn <- rsConnection <$> ask conn <- rsConnection <$> ask
head <$> (liftIO $ runInsert_ conn (insertBeverage bevsub)) head <$> (liftIO $ runInsert_ conn (insertBeverage bevsub))
new Nothing _ =
throwError $ err403
update :: Maybe Int -> Int -> BeverageSubmit -> MateHandler ()
update (Just _) bid bevsub = do
conn <- rsConnection <$> ask
void $ liftIO $ runUpdate_ conn (updateBeverage bid bevsub)
update Nothing _ _ =
throwError $ err403
auth = auth =
authGet :<|> authGet :<|>

View file

@ -9,6 +9,8 @@ import Data.Profunctor.Product (p13)
import Data.Aeson import Data.Aeson
import Data.Aeson.Types import Data.Aeson.Types
import Data.Int (Int64)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Control.Arrow ((<<<)) import Control.Arrow ((<<<))
@ -98,14 +100,15 @@ beverageSelect conn = do
] ]
mapM mapM
(\(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13) -> return $ (\(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13) -> return $
Beverage i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13 Beverage i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13
) )
bevs bevs
insertBeverage insertBeverage
:: BeverageSubmit :: BeverageSubmit
-> Insert [Int] -> Insert [Int]
insertBeverage (BeverageSubmit ident price ml sup max apc ppc artnr) = Insert insertBeverage (BeverageSubmit ident price ml ava sup max apc ppc artnr) = Insert
{ iTable = beverageTable { iTable = beverageTable
, iRows = , iRows =
[ [
@ -115,7 +118,7 @@ insertBeverage (BeverageSubmit ident price ml sup max apc ppc artnr) = Insert
, C.constant (0 :: Int) , C.constant (0 :: Int)
, C.constant (0 :: Int) , C.constant (0 :: Int)
, C.constant ml , C.constant ml
, C.constant (Nothing :: Maybe Int) , C.constant ava
, C.constant sup , C.constant sup
, C.constant max , C.constant max
, C.constant (0 :: Int) , C.constant (0 :: Int)
@ -127,3 +130,33 @@ insertBeverage (BeverageSubmit ident price ml sup max apc ppc artnr) = Insert
, iReturning = rReturning (\(id, _, _, _, _, _, _, _, _, _, _, _, _) -> id) , iReturning = rReturning (\(id, _, _, _, _, _, _, _, _, _, _, _, _) -> id)
, iOnConflict = Nothing , iOnConflict = Nothing
} }
updateBeverage
:: Int
-> BeverageSubmit
-> Update Int64
updateBeverage sid (BeverageSubmit ident price ml ava sup max apc ppc artnr) = Update
{ uTable = beverageTable
, uUpdateWith = updateEasy (\(id_, _, _, amo, van, _, _, _, _, tot, _, _, _) ->
( id_
, C.constant ident
, C.constant price
, amo
, van
, C.constant ml
, C.constant ava
, C.constant sup
, C.constant max
, tot
, C.constant apc
, C.constant ppc
, C.constant artnr
)
)
, uWhere =
(\(id_, _, _, _, _, _, _, _, _, _, _, _, _) ->
id_ .== C.constant sid
)
, uReturning = rCount
}

View file

@ -8,7 +8,8 @@ import Data.Aeson
import qualified Data.Text as T import qualified Data.Text as T
data Beverage = Beverage data Beverage = Beverage
{ beverageIdent :: T.Text { beverageId :: Int
, beverageIdent :: T.Text
, beveragePrice :: Int , beveragePrice :: Int
, beverageAmount :: Int , beverageAmount :: Int
, beverageVanish :: Int , beverageVanish :: Int
@ -34,7 +35,7 @@ data BeverageSubmit = BeverageSubmit
-- , beverageSubmitAmount :: Int -- , beverageSubmitAmount :: Int
-- , beverageSubmitVanish :: Int -- , beverageSubmitVanish :: Int
, beverageSubmitMl :: Int , beverageSubmitMl :: Int
-- , beverageSubmitAvatar :: Maybe Int , beverageSubmitAvatar :: Maybe Int
, beverageSubmitSupplier :: Maybe Int , beverageSubmitSupplier :: Maybe Int
, beverageSubmitMaxAmount :: Int , beverageSubmitMaxAmount :: Int
-- , beverageSubmitTotalBought :: Int -- , beverageSubmitTotalBought :: Int