module Model.User exposing ( Users , usersDecoder , User , userDecoder , UserId , userIdDecoder , getUserName ) 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 userIdDecoder : Json.Decoder UserId userIdDecoder = Json.int userDecoder : Json.Decoder User userDecoder = Json.object2 User ("name" := Json.string) ("email" := Json.string) getUserName : Users -> UserId -> Maybe String getUserName users userId = Dict.get userId users |> Maybe.map .name