added login-layout and changed default layout to different stuff. Also included Balance

This commit is contained in:
Stefan Dresselhaus
2015-08-18 12:28:15 +02:00
parent ae16513293
commit a71c8f3b26
12 changed files with 155 additions and 41 deletions

View File

@ -2,39 +2,40 @@ module Handler.Home where
import Import
-- This is a handler function for the GET request method on the HomeR
-- resource pattern. All of your resource patterns are defined in
-- config/routes
--
-- The majority of the code you will write in Yesod lives in these handler
-- functions. You can spread them across multiple files if you are so
-- inclined, or create a single monolithic file.
loginOrElse :: ((Key User, User) -> Handler Html) -> Handler Html -> Handler Html
loginOrElse cont contElse = do
maid <- maybeAuthId
muid <- case maid of
Just uid -> fmap ((,) uid) <$> runDB (get uid)
Nothing -> return Nothing
case muid of
Nothing -> contElse
Just (uid,u) -> cont (uid,u)
getHomeR :: Handler Html
getHomeR = do
maid <- maybeAuthId
muser <- case maid of
Just uid -> runDB $ get uid
Nothing -> return $ Nothing
loginOrElse getLoggedIn getNotLoggedIn
getLoggedIn :: (Key User, User) -> Handler Html
getLoggedIn (uid, user) = do
loginLayout user $ [whamlet|
<h1>Welcome back, #{userName user}.
<p>Current Balance: #{prettyISK $ userBalanceCents user} ISK.
<p>Current Stock Worth: ...
<p>Current total Worth: ...
<p>Profit in the last 7 days: ...
|]
getNotLoggedIn :: Handler Html
getNotLoggedIn = do
defaultLayout $ do
setTitle "NEAT"
[whamlet|
<h1>
$maybe u <- muser
Welcome back #{userName u}
$nothing
Welcome to NEAT.
<div>
$maybe u <- maid
<p>
Data: #{show u}<br>
<a href=@{AuthR LogoutR}>Logout
<br>
<a href=@{WalletR}>Wallet
<br>
<a href=@{SettingsR}>Settings
$nothing
<p>
<a href=@{AuthR LoginR}>Login
<h1>Welcome to NEAT.
<div>Here we should present features, images and other stuff to get people hooked.
|]
{-
(formWidget, formEnctype) <- generateFormPost sampleForm
@ -64,7 +65,7 @@ postHomeR = do
<button>Submit
<a href=@{RegisterR}>Register Account
|]
case result of
case result of
FormSuccess (u,pw) -> do
login <- runDB $ selectFirst [UserIdent ==. u, UserPassword ==. (Just pw)] []
case login of

View File

@ -11,7 +11,7 @@ import qualified Data.Text.Lazy as T
import qualified Data.ByteString.Lazy.Char8 as B
getSettingsR :: Handler Html
getSettingsR = loginOrDo $ (\(uid,_) -> do
getSettingsR = loginOrDo $ (\(uid,user) -> do
apiKey <- runDB $ getBy $ UniqueApiUser uid
(formWidget, formEnctype) <- generateFormPost $ renderBootstrap3 authFormLayout (authKeyForm (entityVal <$> apiKey) uid)
man <- getHttpManager <$> ask
@ -19,12 +19,12 @@ getSettingsR = loginOrDo $ (\(uid,_) -> do
Just (Entity _ key) -> liftIO $ checkApiKey key man
Nothing -> return False
insertionWidget <- return Nothing :: Handler (Maybe Widget)
defaultLayout $(widgetFile "settings")
loginLayout user $(widgetFile "settings")
)
postSettingsR :: Handler Html
postSettingsR = loginOrDo $ (\(uid,_) -> do
postSettingsR = loginOrDo $ (\(uid,user) -> do
apiKey <- runDB $ getBy $ UniqueApiUser uid
((result,formWidget),formEnctype) <- runFormPost $ renderBootstrap3 authFormLayout (authKeyForm (entityVal <$> apiKey) uid)
(success, msg) <- case result of
@ -46,7 +46,7 @@ $if success
$else
<div class="alert alert-danger" role="alert">^{msg}
|] :: Handler (Maybe Widget)
defaultLayout $(widgetFile "settings")
loginLayout user $(widgetFile "settings")
)
checkApiKey :: Api -> Manager -> IO Bool

View File

@ -81,7 +81,7 @@ getStockR = loginOrDo (\(uid,user) -> do
order by t.type_name asc"
(items :: [Stock]) <- runDB $ rawSql sql [toPersistValue uid]
let items' = convertStock <$> items
defaultLayout $ [whamlet|
loginLayout user $ [whamlet|
<div .panel .panel-default>
<div .panel-heading>Current Stock:
<table .table .table-condensed .small .table-bordered>

View File

@ -7,8 +7,10 @@ import qualified Eve.Api.Char.WalletTransactions as WT
import qualified Eve.Api.Types as T
import qualified Eve.Api.Char.Standings as ST
import qualified Eve.Api.Char.Skills as SK
import qualified Eve.Api.Char.AccountBalance as BA
import Database.Persist.Sql
import Data.Time.Clock
import Control.Lens.Operators
accountingId :: Int64
accountingId = 16622
@ -80,6 +82,15 @@ getUpdateR = loginOrDo (\(uid,user) -> do
mapM_ (\(Entity eid t) -> replace eid t) trans
let updateProblemSql = "update transaction t set problematic=true where t.trans_is_sell and t.in_stock < 0"
runDB $ rawExecute updateProblemSql []
--update Balance
when (userBalanceTimeout user < now) $
do
balance <- liftIO $ BA.getAccountBalance man apidata
case balance of
T.QueryResult time' balance' -> runDB $ do
update uid [UserBalanceCents =. fromIntegral (balance' ^. BA.centbalance)]
update uid [UserBalanceTimeout =. time']
_ -> return ()
redirect WalletR
)

View File

@ -61,7 +61,7 @@ getWalletDetailsR hrs days = loginOrDo (\(uid,user) -> do
order by \
extract(day from (now() at time zone 'utc')-date(date_time at time zone 'utc')) asc"
(profits :: [Profit]) <- runDB $ rawSql profitquery [toPersistValue uid, toPersistValue days]
defaultLayout $ [whamlet|
loginLayout user $ [whamlet|
<div .panel .panel-default>
<div .panel-heading>Transactions in the last #{hrs} hours:
<div .btn-group .btn-group-justified role="group">