-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.fs
50 lines (39 loc) · 1.71 KB
/
Program.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
type UnvalidatedUserId = UnvalidatedUserId of int
type UnvalidatedBookId = UnvalidatedBookId of int
type ValidUserId = ValidUserId of int
type ValidBookId = ValidBookId of int
type BorrowBook =
{ BookId: UnvalidatedBookId
UserId: UnvalidatedUserId }
type BookBorrowed =
{ ValidUserId: ValidUserId
ValidBookId: ValidBookId }
type BookNotCurrentlyAvailable =
{ ValidUserId: ValidUserId
ValidBookId: ValidBookId }
type BookNotFound = { BookId: UnvalidatedBookId }
type UserNotFound = { UserId: UnvalidatedUserId }
type Borrowed =
| BookBorrowed of BookBorrowed
| BookNotCurrentlyAvailable of BookNotCurrentlyAvailable
| BookNotFound of BookNotFound
| UserNotFound of UserNotFound
type ValidateUserId = UnvalidatedUserId -> ValidUserId option
type ValidateBookId = UnvalidatedBookId -> ValidBookId option
type MarkBookBorrowed = ValidUserId -> ValidBookId -> Borrowed option
type BorrowBookFlow =
ValidateUserId -> ValidateBookId -> MarkBookBorrowed -> BorrowBook -> Borrowed
let borrowBookFlow: BorrowBookFlow =
fun validateUserId validateBookId markBookBorrowed borrowBook ->
let validUserId = validateUserId borrowBook.UserId
let validBookId = validateBookId borrowBook.BookId
match (validUserId, validBookId) with
| None, _ -> UserNotFound { UserId = borrowBook.UserId }
| _, None -> BookNotFound { BookId = borrowBook.BookId }
| Some (userId), Some (bookId) ->
match markBookBorrowed userId bookId with
| Some (borrowed) -> borrowed
| None ->
BookNotCurrentlyAvailable
{ ValidUserId = validUserId.Value
ValidBookId = validBookId.Value }