-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoundation.hs
137 lines (114 loc) · 4.4 KB
/
Foundation.hs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{-# LANGUAGE OverloadedStrings, TypeFamilies, QuasiQuotes,
TemplateHaskell, GADTs, FlexibleContexts,
MultiParamTypeClasses, DeriveDataTypeable, EmptyDataDecls,
GeneralizedNewtypeDeriving, ViewPatterns, FlexibleInstances, DeriveGeneric #-}
module Foundation where
import Yesod
import Yesod.Static
import Data.Text
import Control.Applicative()
import GHC.Generics
import Yesod.Auth
import Data.Default (def)
import Network.HTTP.Client.Conduit (Manager)
import Yesod.Auth.BrowserId
import Yesod.Auth.GoogleEmail2
import Database.Persist.Postgresql
( ConnectionPool, SqlBackend, runSqlPool)
-- static
staticFiles "static"
-- importanto o getStatic para coisas estaticas e connPool para conexao do banco
data App = App {getStatic :: Static, connPool :: ConnectionPool, httpManager :: Manager}
-- criacao do banco
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Usuario
loginId LoginId
nome Text Maybe
email Text
senha Text
UniqueUsuario email
deriving Generic Show Read
Login
email Text
senha Text
UniqueLogin email
deriving Generic Show Read
Favoritos
usuarioId UsuarioId
nomefavoritos Text
tempo Text
rendimento Text
url Text
urlimg Text
urlfonte Text
nomefonte Text
deriving Generic Show Read
|]
-- arquivo routes
mkYesodData "App" $(parseRoutesFile "routes")
newLayout :: Text -> Widget -> Handler Html
newLayout title widget = do
pc <- widgetToPageContent widget
withUrlRenderer
[hamlet|
$doctype 5
<html>
<head>
<title>#{title}
<meta charset=utf-8>
<style>body { font-family: verdana }
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/png" href=@{StaticR img_miniLogo_png}/>
<link href="https://fonts.googleapis.com/css?family=Dosis" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<link href=@{StaticR css_estilo_css} rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<body>
<article>
^{pageBody pc}
|]
-- Formulario
type Form a = Html -> MForm Handler (FormResult a, Widget)
semlogin :: Text
semlogin = "Sem login"
comlogin :: Text
comlogin = "Com login"
-- Formulario
instance RenderMessage App FormMessage where
renderMessage _ _ = defaultFormMessage
instance Yesod App where
approot = ApprootStatic "https://projeto-final-c-renataalberti.c9users.io"
instance YesodAuth App where
type AuthId App = Text
getAuthId = return . Just . credsIdent
loginDest _ = LooginR
logoutDest _ = LooginR
authPlugins _ = [authBrowserId def, authGoogleEmail semlogin comlogin]
authHttpManager = httpManager
maybeAuthId = lookupSession "_USER"
passwordConfirmField :: Field Handler Text
passwordConfirmField = Field
{ fieldParse = \rawVals _fileVals ->
case rawVals of
[a, b]
| a == b -> return $ Right $ Just a
| otherwise -> return $ Left "Passwords don't match"
[] -> return $ Right Nothing
_ -> return $ Left "You must enter two values"
, fieldView = \idAttr nameAttr otherAttrs eResult isReq ->
[whamlet|
<input id=#{idAttr} name=#{nameAttr} *{otherAttrs} type=password>
<div>Confirm:
<input id=#{idAttr}-confirm name=#{nameAttr} *{otherAttrs} type=password>
|]
, fieldEnctype = UrlEncoded
}
-- banco
instance YesodPersist App where
type YesodPersistBackend App = SqlBackend
runDB f = do
master <- getYesod
let pool = connPool master
runSqlPool f pool