module Model.Payment ( perPage , Payments , Payment , PaymentId , PaymentWithId , paymentsDecoder , addPayment , removePayment ) where import Date exposing (..) import Json.Decode as Json exposing ((:=)) import Dict exposing (..) perPage : Int perPage = 10 type alias Payments = Dict PaymentId Payment type alias PaymentWithId = (PaymentId, Payment) type alias Payment = { creation : Date , name : String , cost : Int , userName : String } type alias PaymentId = String paymentsDecoder : Json.Decoder Payments paymentsDecoder = Json.map Dict.fromList (Json.list paymentWithIdDecoder) paymentWithIdDecoder : Json.Decoder (PaymentId, Payment) paymentWithIdDecoder = paymentDecoder `Json.andThen` (\payment -> Json.map (\id -> (id, payment)) ("id" := Json.string)) paymentDecoder : Json.Decoder Payment paymentDecoder = Json.object4 Payment ("creation" := dateDecoder) ("name" := Json.string) ("cost" := Json.int) ("userName" := Json.string) paymentIdDecoder : Json.Decoder PaymentId paymentIdDecoder = Json.string dateDecoder : Json.Decoder Date dateDecoder = Json.customDecoder Json.string Date.fromString addPayment : Payments -> (PaymentId, Payment) -> Payments addPayment payments (paymentId, payment) = Dict.insert paymentId payment payments removePayment : Payments -> PaymentId -> Payments removePayment payments paymentId = Dict.remove paymentId payments