-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEditAlbum.fs
69 lines (58 loc) · 1.66 KB
/
EditAlbum.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
module FreyaMusicStore.EditAlbum
open System
open Arachne.Http
open Arachne.Uri.Template
open Freya.Core
open Freya.Core.Operators
open Freya.Machine
open Freya.Machine.Extensions.Http
open Freya.Machine.Router
open Freya.Router
type IdAndName =
{ Id : int
Name : string }
type EditAlbum =
{ Album : Album.Album
Genres : IdAndName []
Artists : IdAndName [] }
let id =
freya {
let! id = Freya.getLensPartial (Route.Atom_ "0")
match Int32.TryParse id.Value with
| true, id -> return Some id
| _ -> return None
}
let isMalformed =
freya {
let! id = id
return Option.isNone id
}
let doesExist =
freya {
let! id = id
let ctx = Db.getContext()
return Db.getAlbum id.Value ctx |> Option.isSome
}
let ok _ =
freya {
let! id = id
let ctx = Db.getContext()
let album = Db.getAlbum id.Value ctx |> Option.get |> Album.Album.fromDb
let genres = Db.getGenres ctx |> Array.map (fun g -> { Id = g.GenreId; Name = g.Name })
let artists = Db.getArtists ctx |> Array.map (fun a -> { Id = a.ArtistId; Name = a.Name })
return! writeHtml ("editAlbum", { Album = album; Genres = genres; Artists = artists } )
}
let uri =
freya {
let! id = id
return String.Format(Uris.editAlbum, id.Value)
}
let pipe =
freyaMachine {
methodsSupported ( freya { return [ GET ] } )
malformed isMalformed
including common
including (protectAuthenticated [ GET ] uri)
including (protectAdmin [ GET ])
exists doesExist
handleOk ok } |> FreyaMachine.toPipeline