aboutsummaryrefslogtreecommitdiff
path: root/src/client/Model/Payment.elm
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/Model/Payment.elm')
-rw-r--r--src/client/Model/Payment.elm26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/client/Model/Payment.elm b/src/client/Model/Payment.elm
index fa59943..e773be3 100644
--- a/src/client/Model/Payment.elm
+++ b/src/client/Model/Payment.elm
@@ -2,17 +2,22 @@ module Model.Payment
( Payments
, Payment
, PaymentId
+ , PaymentWithId
, paymentsDecoder
+ , addPayment
+ , removePayment
) where
import Date exposing (..)
import Json.Decode as Json exposing ((:=))
+import Dict exposing (..)
-type alias Payments = List Payment
+type alias Payments = Dict PaymentId Payment
+
+type alias PaymentWithId = (PaymentId, Payment)
type alias Payment =
- { id : PaymentId
- , creation : Date
+ { creation : Date
, name : String
, cost : Int
, userName : String
@@ -21,12 +26,15 @@ type alias Payment =
type alias PaymentId = String
paymentsDecoder : Json.Decoder Payments
-paymentsDecoder = Json.list paymentDecoder
+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.object5 Payment
- ("id" := paymentIdDecoder)
+ Json.object4 Payment
("creation" := dateDecoder)
("name" := Json.string)
("cost" := Json.int)
@@ -37,3 +45,9 @@ 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