2017-05-04 18:46:45 +00:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
|
|
|
|
|
|
module App where
|
|
|
|
|
|
|
|
|
|
import Api
|
2017-05-06 12:34:13 +00:00
|
|
|
import Control.Monad
|
2017-05-04 18:46:45 +00:00
|
|
|
import Control.Monad.IO.Class
|
2017-05-06 12:34:13 +00:00
|
|
|
import Control.Monad.Logger (runStderrLoggingT)
|
2017-05-06 16:03:22 +00:00
|
|
|
import Control.Monad.Trans.Reader
|
2017-05-06 12:34:13 +00:00
|
|
|
import Data.Maybe
|
2017-05-04 18:46:45 +00:00
|
|
|
import Data.String.Conversions
|
|
|
|
|
import Data.Text
|
|
|
|
|
import Database.Persist
|
|
|
|
|
import Database.Persist.Postgresql
|
|
|
|
|
import Database.Persist.Sqlite
|
|
|
|
|
import Network.Wai
|
2017-05-06 12:34:13 +00:00
|
|
|
import Network.Wai.Handler.Warp as Warp
|
2017-05-04 18:46:45 +00:00
|
|
|
import Network.Wai.Middleware.Cors
|
2017-05-06 12:34:13 +00:00
|
|
|
import Network.Wai.Middleware.RequestLogger (logStdoutDev)
|
2017-05-04 18:46:45 +00:00
|
|
|
import Servant
|
2017-05-06 09:55:27 +00:00
|
|
|
import SwaggerGen
|
2017-05-04 18:46:45 +00:00
|
|
|
|
|
|
|
|
couponServer :: ConnectionPool -> Server CouponApi
|
|
|
|
|
couponServer pool =
|
|
|
|
|
couponAddH :<|> couponGetH :<|> couponDelH
|
|
|
|
|
where
|
|
|
|
|
couponAddH newCoupon = liftIO $ couponAdd newCoupon
|
|
|
|
|
couponGetH code = liftIO $ couponGet code
|
|
|
|
|
couponDelH code = liftIO $ couponDel code
|
|
|
|
|
|
2017-05-06 12:34:13 +00:00
|
|
|
couponAdd :: Coupon -> IO NoContent
|
2017-05-04 18:46:45 +00:00
|
|
|
couponAdd newCoupon = flip runSqlPersistMPool pool $ do
|
|
|
|
|
exists <- selectFirst [CouponCode ==. couponCode newCoupon] []
|
2017-05-06 12:34:13 +00:00
|
|
|
when (isNothing exists) $ void $ insert newCoupon
|
|
|
|
|
return NoContent
|
2017-05-04 18:46:45 +00:00
|
|
|
|
|
|
|
|
couponGet :: Text -> IO (Maybe Coupon)
|
|
|
|
|
couponGet code = flip runSqlPersistMPool pool $ do
|
|
|
|
|
mUser <- selectFirst [CouponCode ==. code] []
|
|
|
|
|
return $ entityVal <$> mUser
|
|
|
|
|
|
2017-05-06 12:34:13 +00:00
|
|
|
couponDel :: Text -> IO NoContent
|
2017-05-04 18:46:45 +00:00
|
|
|
couponDel code = flip runSqlPersistMPool pool $ do
|
|
|
|
|
deleteWhere [CouponCode ==. code]
|
2017-05-06 12:34:13 +00:00
|
|
|
return NoContent
|
2017-05-04 18:46:45 +00:00
|
|
|
|
2017-05-06 16:03:22 +00:00
|
|
|
-- computeBillCoupon :: Coupon -> BillCoupon -> CouponResult
|
|
|
|
|
-- computeBillCoupon c b = Applied 1000
|
|
|
|
|
|
2017-05-04 18:46:45 +00:00
|
|
|
billCouponServer :: ConnectionPool -> Server BillCouponApi
|
2017-05-06 16:03:22 +00:00
|
|
|
billCouponServer pool = liftIO.compute
|
|
|
|
|
where compute bill = runSqlPersistMPool (query bill) pool
|
|
|
|
|
query bill = do mcpn <- selectFirst [CouponCode ==. coupon bill] []
|
|
|
|
|
case mcpn of
|
|
|
|
|
Just cpn -> withCoupon (entityVal cpn) bill
|
|
|
|
|
Nothing -> return (Rejected "Coupon Not Found")
|
|
|
|
|
withCoupon cpn bill = case couponValue cpn of
|
|
|
|
|
ProductFlat c -> productFlat c bill
|
|
|
|
|
CartFlat c -> cartFlat c bill
|
|
|
|
|
CartPercent c -> cartPerCent c bill
|
|
|
|
|
|
|
|
|
|
productFlat :: (Monad m) => CouponForProduct -> BillCoupon -> m CouponResult
|
|
|
|
|
productFlat p bill = return (Rejected "Coupon ProductFlat Found")
|
|
|
|
|
|
|
|
|
|
cartFlat :: (Monad m) => Int -> BillCoupon -> m CouponResult
|
|
|
|
|
cartFlat p bill = return (Rejected "Coupon CartFlat Found")
|
|
|
|
|
|
|
|
|
|
cartPerCent :: (Monad m) => Int -> BillCoupon -> m CouponResult
|
|
|
|
|
cartPerCent p bill = return (Rejected "Coupon CartPercent Found")
|
2017-05-04 18:46:45 +00:00
|
|
|
|
2017-05-06 09:55:27 +00:00
|
|
|
swaggerServer :: Server SwaggerApi
|
|
|
|
|
swaggerServer = liftIO $ return $ swaggerDoc couponApi
|
2017-05-04 18:46:45 +00:00
|
|
|
|
|
|
|
|
server :: ConnectionPool -> Server ServerApi
|
2017-05-06 09:55:27 +00:00
|
|
|
server pool = couponServer pool :<|> billCouponServer pool :<|> swaggerServer
|
2017-05-04 18:46:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
app :: ConnectionPool -> Application
|
2017-05-06 12:34:13 +00:00
|
|
|
app pool = serve couponApi $ couponServer pool :<|> billCouponServer pool
|
2017-05-04 18:46:45 +00:00
|
|
|
|
2017-05-06 12:34:13 +00:00
|
|
|
appDebug :: ConnectionPool -> Application
|
|
|
|
|
appDebug pool = logStdoutDev $ cors (const $ Just policy) $ serve api $ server pool
|
|
|
|
|
where policy = simpleCorsResourcePolicy { corsRequestHeaders = ["Content-Type"] }
|
|
|
|
|
|
|
|
|
|
mkApp :: String -> IO Application
|
|
|
|
|
mkApp sqliteFile = do
|
2017-05-04 18:46:45 +00:00
|
|
|
pool <- runStderrLoggingT $ createPostgresqlPool (cs sqliteFile) 5
|
|
|
|
|
runSqlPool (runMigration migrateAll) pool
|
|
|
|
|
return $ app pool
|
|
|
|
|
|
2017-05-06 12:34:13 +00:00
|
|
|
mkDebugPgApp :: String -> IO Application
|
|
|
|
|
mkDebugPgApp sqliteFile = do
|
|
|
|
|
pool <- runStderrLoggingT $ createPostgresqlPool (cs sqliteFile) 5
|
|
|
|
|
runSqlPool (runMigration migrateAll) pool
|
|
|
|
|
return $ appDebug pool
|
|
|
|
|
|
|
|
|
|
mkSqliteApp :: String -> IO Application
|
|
|
|
|
mkSqliteApp sqliteFile = do
|
2017-05-04 18:46:45 +00:00
|
|
|
pool <- runStderrLoggingT $ createSqlitePool (cs sqliteFile) 5
|
|
|
|
|
runSqlPool (runMigration migrateAll) pool
|
2017-05-06 12:34:13 +00:00
|
|
|
return $ appDebug pool
|
2017-05-04 18:46:45 +00:00
|
|
|
|
|
|
|
|
run :: String -> IO ()
|
2017-05-06 12:34:13 +00:00
|
|
|
run dbConnStr = Warp.run 3000 =<< mkApp dbConnStr
|