-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDb.fs
156 lines (132 loc) · 4.88 KB
/
Db.fs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
module SuaveMusicStore.Db
open FSharp.Data.Sql
open System.Runtime.InteropServices
[<Literal>]
let ConnectionString =
"Server=127.0.0.1;" +
"Database=suavemusicstore;" +
"User Id=suave;" +
"Password=1234;"
type Sql =
SqlDataProvider<
ConnectionString = ConnectionString,
DatabaseVendor = Common.DatabaseProviderTypes.POSTGRESQL,
CaseSensitivityChange = Common.CaseSensitivityChange.ORIGINAL>
type DbContext = Sql.dataContext
type Album = DbContext.``public.albumsEntity``
type Genre = DbContext.``public.genresEntity``
type AlbumsDetails = DbContext.``public.albumdetailsEntity``
type Artist = DbContext.``public.artistsEntity``
type User = DbContext.``public.usersEntity``
type Cart = DbContext.``public.cartsEntity``
type CartDetails = DbContext.``public.cartdetailsEntity``
let getContext() = Sql.GetDataContext()
let getGenres (ctx: DbContext): Genre list =
ctx.Public.Genres |> Seq.toList
let getAlbumsForGenre genreName (ctx: DbContext): Album list =
query {
for album in ctx.Public.Albums do
join genre in ctx.Public.Genres on (album.Genreid = genre.Genreid)
where (genre.Name = genreName)
select album
} |> Seq.toList
let getAlbumDetails id (ctx: DbContext): AlbumsDetails option =
query {
for album in ctx.Public.Albumdetails do
where (album.Albumid = id)
select album
} |> Seq.tryHead
let getAlbumsDetails (ctx: DbContext): AlbumsDetails list =
ctx.Public.Albumdetails
|> Seq.toList
|> List.sortBy (fun a -> a.Artist)
let getAlbum id (ctx: DbContext): Album option =
query {
for album in ctx.Public.Albums do
where (album.Albumid = id)
select album
} |> Seq.tryHead
let createAlbum (artistId, genreId, price, title) (ctx: DbContext) =
ctx.Public.Albums.Create(artistId, genreId, price, title) |> ignore
ctx.SubmitUpdates()
let updateAlbum (album: Album) (artistId, genreId, price, title) (ctx: DbContext) =
album.Artistid <- artistId
album.Genreid <- genreId
album.Price <- price
album.Title <- title
ctx.SubmitUpdates()
let deleteAlbum (album: Album) (ctx: DbContext) =
album.Delete()
ctx.SubmitUpdates()
let getArtists (ctx: DbContext): Artist list =
ctx.Public.Artists |> Seq.toList
let validateUser (username, password) (ctx: DbContext): User option =
query {
for user in ctx.Public.Users do
where (user.Username = username && user.Password = password)
select user
} |> Seq.tryHead
let getCart cartId albumId (ctx: DbContext): Cart option =
query {
for cart in ctx.Public.Carts do
where (cart.Cartid = cartId && cart.Albumid = albumId)
select cart
} |> Seq.tryHead
let addToCart cartId albumId (ctx: DbContext) =
match getCart cartId albumId ctx with
| Some cart ->
cart.Count <- cart.Count + 1
| None ->
ctx.Public.Carts.Create(albumId, cartId, 1, System.DateTime.UtcNow) |> ignore
ctx.SubmitUpdates()
let removeFromCart (cart: Cart) albumId (ctx: DbContext) =
cart.Count <- cart.Count - 1
if cart.Count = 0 then cart.Delete()
ctx.SubmitUpdates()
let getCartsDetails cartId (ctx: DbContext): CartDetails list =
query {
for cart in ctx.Public.Cartdetails do
where (cart.Cartid = cartId)
select cart
} |> Seq.toList
let getCarts cartId (ctx: DbContext): Cart list =
query {
for cart in ctx.Public.Carts do
where (cart.Cartid = cartId)
select cart
} |> Seq.toList
let upgradeCarts (cartId: string, username: string) (ctx: DbContext) =
for cart in getCarts cartId ctx do
match getCart username cart.Albumid ctx with
| Some existing ->
existing.Count <- existing.Count + cart.Count
cart.Delete()
| None ->
cart.Cartid <- username
ctx.SubmitUpdates()
let getUser username (ctx: DbContext): User option =
query {
for user in ctx.Public.Users do
where (user.Username = username)
select user
} |> Seq.tryHead
let newUser (username, password, email) (ctx: DbContext) =
let role = "user"
let user = ctx.Public.Users.Create(email, password, role, username)
ctx.SubmitUpdates()
user
let placeOrder (username: string) (ctx: DbContext) =
let carts = (username, ctx) ||> getCartsDetails
let total = carts |> List.sumBy (fun c -> (decimal) c.Count * c.Price)
let order = ctx.Public.Orders.Create(System.DateTime.UtcNow, total)
order.Username <- username
ctx.SubmitUpdates()
for cart in carts do
ctx.Public.Orderdetails.Create(
cart.Albumid,
order.Orderid,
cart.Count,
cart.Price) |> ignore
(cart.Cartid, cart.Albumid, ctx) |||> getCart
|> Option.iter (fun cart -> cart.Delete())
ctx.SubmitUpdates()