module Model.User exposing ( Users , usersDecoder , User , userDecoder , UserId , userIdDecoder , getUserName ) import Json.Decode as Decode exposing (Decoder) import Dict exposing (Dict) type alias Users = Dict UserId User type alias UserId = Int type alias User = { name : String , email : String } usersDecoder : Decoder Users usersDecoder = Decode.map Dict.fromList (Decode.list userWithIdDecoder) userWithIdDecoder : Decode.Decoder (UserId, User) userWithIdDecoder = Decode.map2 (,) (Decode.field "id" userIdDecoder) userDecoder userIdDecoder : Decoder UserId userIdDecoder = Decode.int userDecoder : Decoder User userDecoder = Decode.map2 User (Decode.field "name" Decode.string) (Decode.field "email" Decode.string) getUserName : Users -> UserId -> Maybe String getUserName users userId = Dict.get userId users |> Maybe.map .name