blob: b83273f21c6a4df6ef0e4a1929fdc5c1740f311d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  | 
{-# LANGUAGE OverloadedStrings #-}
module Application
  ( getIndexAction
  , getUsersAction
  , getPaymentsAction
  , addUserAction
  , deleteUserAction
  , insertPaymentAction
  , signIn
  , checkConnection
  , signOut
  ) where
import Web.Scotty
import Network.HTTP.Types.Status (badRequest400)
import Database.Persist
import Control.Monad.IO.Class (liftIO)
import Data.Text (Text)
import qualified Data.Text as T
import Data.String (fromString)
import qualified Data.Text.Lazy as TL
import qualified LoginSession
import qualified Secure
import Model.Database (runDb)
import Model.User
import Model.Payment
import View.Page (page)
getIndexAction :: ActionM ()
getIndexAction =
  Secure.loggedAction (\_ ->
    html $ page
  )
getUsersAction :: ActionM ()
getUsersAction = do
  users <- liftIO $ runDb getUsers
  html . fromString . show $ users
getPaymentsAction :: ActionM ()
getPaymentsAction = do
  payments <- liftIO $ runDb getPayments
  json payments
addUserAction :: Text -> Text -> ActionM ()
addUserAction email name = do
  _ <- liftIO . runDb $ insertUser email name
  html "Ok"
deleteUserAction :: Text -> ActionM ()
deleteUserAction email = do
  _ <- liftIO . runDb $ deleteUser email
  html "Ok"
insertPaymentAction :: Text -> Text -> Int -> ActionM ()
insertPaymentAction email name cost = do
  maybeUser <- liftIO . runDb $ getUser email
  case maybeUser of
    Just user -> do
      _ <- liftIO . runDb $ insertPayment (entityKey user) name cost
      return ()
    Nothing -> do
      status badRequest400
      html "Not found"
signIn :: Text -> ActionM ()
signIn login = do
  LoginSession.put login
  html "Ok"
checkConnection :: ActionM ()
checkConnection = do
  maybeLogin <- LoginSession.get
  case maybeLogin of
    Just login ->
      html . TL.fromStrict $
        T.intercalate
          " "
          [ "You are connected with the following login:"
          , login
          ]
    Nothing -> do
      status badRequest400
      html "You are not connected"
signOut :: ActionM ()
signOut = do
  LoginSession.delete
  html "Ok"
 
  |