-
Notifications
You must be signed in to change notification settings - Fork 3
/
Albums.fs
70 lines (59 loc) · 1.76 KB
/
Albums.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
module FreyaMusicStore.Albums
open System
open System.Globalization
open System.IO
open Arachne.Http
open Chiron
open Freya.Core
open Freya.Router
open Freya.Machine
open Freya.Machine.Extensions.Http
open Freya.Lenses.Http
open Microsoft.AspNet.Identity
let isMalformed =
freya {
let! meth = Freya.getLens Request.Method_
match meth with
| POST -> let! album = readAlbum in return album.IsNone
| _ -> return false
}
let createAlbum =
freya {
let! album = readAlbum
let album = album.Value
let ctx = Db.getContext()
let album =
Db.createAlbum(
album.ArtistId,
album.GenreId,
album.Price,
album.Title,
album.AlbumArtUrl)
ctx
let details = Db.getAlbumDetails album.AlbumId ctx
return Album.AlbumDetails.fromDb details.Value
} |> Freya.memo
let post =
freya {
let! _ = createAlbum
return ()
}
let onCreated _ =
freya {
let! album = createAlbum
do! Freya.setLensPartial
Response.Headers.Location_
(Location.Parse (String.Format(Uris.endpoint + Uris.album, album.AlbumId)))
return! writeHtml ("album", album)
}
let fetch : Freya<Db.DbContext -> _> =
Db.getAlbumsDetails >> Array.map Album.AlbumDetails.fromDb |> Freya.init
let pipe =
freyaMachine {
methodsSupported ( Freya.init [ GET; POST ] )
malformed isMalformed
including (protectAuthenticated [ GET; POST ] (Freya.init Uris.albums))
including (protectAdmin [ GET; POST ])
including (res fetch "albums")
doPost post
handleCreated onCreated} |> FreyaMachine.toPipeline