blob: 1412913c9ad5958ae7ca34b0d629c9be6089a59c (
plain)
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
|
module Model.User
( Users
, usersDecoder
, User
, userDecoder
, UserId
, userIdDecoder
, getUserName
) where
import Json.Decode as Json exposing ((:=))
import Dict exposing (Dict)
type alias Users = Dict UserId User
type alias UserId = Int
type alias User =
{ name : String
, email : String
}
usersDecoder : Json.Decoder Users
usersDecoder = Json.map Dict.fromList (Json.list userWithIdDecoder)
userWithIdDecoder : Json.Decoder (UserId, User)
userWithIdDecoder =
Json.object2 (,)
("id" := userIdDecoder)
userDecoder
userDecoder : Json.Decoder User
userDecoder =
Json.object2 User
("name" := Json.string)
("email" := Json.string)
userIdDecoder : Json.Decoder UserId
userIdDecoder = Json.int
getUserName : Users -> UserId -> Maybe String
getUserName users userId =
Dict.get userId users
|> Maybe.map .name
|