aboutsummaryrefslogtreecommitdiff
path: root/src/client/Model/Payment.elm
blob: 02dcf7e03224bf20b0bea7df4e7f6abea2302ee0 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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