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]
:<|> "new" :> AuthProtect "header-auth" :> ReqBody '[JSON] BeverageSubmit
:> Post '[JSON] Int
:<|> "update" :> AuthProtect "header-auth"
:> Capture "id" Int :> ReqBody '[JSON] BeverageSubmit :> Post '[JSON] ()
)
:<|> "auth" :>
( "get" :> ReqBody '[JSON] Int :> Post '[JSON] AuthInfo

View file

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

View file

@ -9,6 +9,8 @@ import Data.Profunctor.Product (p13)
import Data.Aeson
import Data.Aeson.Types
import Data.Int (Int64)
import Control.Monad.IO.Class (liftIO)
import Control.Arrow ((<<<))
@ -98,14 +100,15 @@ beverageSelect conn = do
]
mapM
(\(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
insertBeverage
:: BeverageSubmit
-> 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
, 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 ml
, C.constant (Nothing :: Maybe Int)
, C.constant ava
, C.constant sup
, C.constant max
, C.constant (0 :: Int)
@ -127,3 +130,33 @@ insertBeverage (BeverageSubmit ident price ml sup max apc ppc artnr) = Insert
, iReturning = rReturning (\(id, _, _, _, _, _, _, _, _, _, _, _, _) -> id)
, 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
data Beverage = Beverage
{ beverageIdent :: T.Text
{ beverageId :: Int
, beverageIdent :: T.Text
, beveragePrice :: Int
, beverageAmount :: Int
, beverageVanish :: Int
@ -34,7 +35,7 @@ data BeverageSubmit = BeverageSubmit
-- , beverageSubmitAmount :: Int
-- , beverageSubmitVanish :: Int
, beverageSubmitMl :: Int
-- , beverageSubmitAvatar :: Maybe Int
, beverageSubmitAvatar :: Maybe Int
, beverageSubmitSupplier :: Maybe Int
, beverageSubmitMaxAmount :: Int
-- , beverageSubmitTotalBought :: Int