aboutsummaryrefslogtreecommitdiff
path: root/src/client/Model/User.elm
blob: b0d62a60b5c84c027c2b21273e6a21ab14ea83f5 (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
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 =
  userDecoder `Json.andThen` (\user -> Json.map (\id -> (id, user)) ("id" := userIdDecoder))

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