-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathInventoryItem.fs
43 lines (36 loc) · 1.4 KB
/
InventoryItem.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
[<RequireQualifiedAccess>]
module InventoryItem
type State = {
isActive : bool;
}
with static member Zero = { isActive = false }
type Command =
| Create of string
| Deactivate
| Rename of string
| CheckInItems of int
| RemoveItems of int
type Event =
| Created of string
| Deactivated
| Renamed of string
| ItemsCheckedIn of int
| ItemsRemoved of int
let apply item = function
| Created _ -> { item with State.isActive = true; }
| Deactivated _ -> { item with State.isActive = false; }
| Renamed _ -> item
| ItemsCheckedIn _ -> item
| ItemsRemoved _ -> item
open Validator
module private Assert =
let validName name = notNull ["The name must not be null."] name <* notEmptyString ["The name must not be empty"] name
let validCount count = validator (fun c -> c > 0) ["The item count must be positive."] count
let inactive state = validator (fun i -> i.isActive = false) ["The item is already deactivated."] state
let exec state =
function
| Create name -> Assert.validName name <?> Created(name)
| Deactivate -> Assert.inactive state <?> Deactivated
| Rename name -> Assert.validName name <?> Renamed(name)
| CheckInItems count -> Assert.validCount count <?> ItemsCheckedIn(count)
| RemoveItems count -> Assert.validCount count <?> ItemsRemoved(count)