-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
117 lines (100 loc) · 3.39 KB
/
Main.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
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Lens ((^.),_1)
import Data.Time (getCurrentTime,utctDay,UTCTime(..),secondsToDiffTime,addDays)
import Opaleye (constant,pgStrictText)
import OpalLib.Accession
import OpalLib.Book
import OpalLib.Ids
import OpalLib.Loan
import OpalLib.Pagination
import OpalLib.Search
import OpalLib.Util
main :: IO ()
main = do
eg_allBooks
eg_findLyah
eg_projection
eg_allProgrammingBooks
eg_allProgrammingAccessions
eg_borrow
eg_return
eg_overdue
eg_accessionCount
eg_accessionCountKeywords
eg_pagination
eg_search
eg_searchPaginated
lyahIsbn :: Isbn
lyahIsbn = Isbn 9781593272838
eg_allBooks :: IO ()
eg_allBooks = opaleyeExample "All Books" bookQuery booksAll
eg_findLyah :: IO ()
eg_findLyah = opaleyeExample "Find by ISBN"
(findBookByIsbnQ (constant lyahIsbn))
(findBookByIsbn lyahIsbn)
eg_projection :: IO ()
eg_projection = opaleyeExample "Projection" bookTitlesQuery bookTitles
eg_allProgrammingBooks :: IO ()
eg_allProgrammingBooks = opaleyeExample "All Programming Books"
(booksWithKeywordQuery (pgStrictText "Programming"))
(booksWithKeyword "Programming")
eg_allProgrammingAccessions :: IO ()
eg_allProgrammingAccessions = opaleyeExample "All Programming Accessions"
(accessionsWithKeywordQuery (pgStrictText "Programming"))
(accessionsWithKeyword "Programming")
eg_borrow :: IO ()
eg_borrow = do
printTitle "Borrow Book (Insert Loan)"
b <- getCurrentTime
let d = UTCTime (addDays 28 $ utctDay b) (secondsToDiffTime (60*60*17))
printOpaleye $ do
lId <- borrow (AccessionId 1) (PersonId 1) b d
findLoanByLoanId lId
printEnding
eg_return :: IO ()
eg_return = do
printTitle "Return Book (Update Loan)"
r <- getCurrentTime
ls <- runOpaleye loansOutstanding
case ls of
Right (fl:_) -> printOpaleye $ do
let lId = fl^._1.loanId
loanReturn lId r
findLoanByLoanId lId
_ -> putStrLn "ERROR: NO OUTSTANDING LOANS"
printEnding
eg_overdue :: IO ()
eg_overdue = opaleyeExample "Overdue Books" loansOverdueQuery loansOverdue
eg_accessionCount :: IO ()
eg_accessionCount = opaleyeExample "Accession Count"
(accessionCountForBookQuery $ constant lyahIsbn)
(accessionCountForBook lyahIsbn)
eg_accessionCountKeywords :: IO ()
eg_accessionCountKeywords = opaleyeExample "Accession Keyword Grouping"
accessionCountsForKeywordQuery
accessionCountsForKeyword
-- NOTE: The query in the example output
eg_pagination :: IO ()
eg_pagination = do
printTitle "Paginated Programming Books"
printOpaleye $ booksWithKeywordPaginated (Pagination 1 5) "Programming"
printOpaleye $ booksWithKeywordPaginated (Pagination 2 5) "Programming"
printOpaleye $ booksWithKeywordPaginated (Pagination 3 5) "Programming"
printEnding
eg_search :: IO ()
eg_search = do
let s1 = Search (Just "Great Good") [] False
opaleyeExample "Search Title" (searchQuery s1) (search s1)
let s2 = Search Nothing ["Management"] False
opaleyeExample "Search Keyword" (searchQuery s2) (search s2)
let s3 = Search Nothing ["Management"] True
opaleyeExample "Search Keyword" (searchQuery s3) (search s3)
eg_searchPaginated :: IO ()
eg_searchPaginated = do
let s1 = Search Nothing ["Programming"] False
printTitle "Search Programming Paginated"
printOpaleye $ searchPaginated (Pagination 1 5) s1
printOpaleye $ searchPaginated (Pagination 2 5) s1
printOpaleye $ searchPaginated (Pagination 3 5) s1
printEnding