aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Server.elm
blob: c0175489d9b4fb795d71ab1810e0ee02beb54a19 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module Server exposing
  ( signIn
  , createPayment
  , editPayment
  , deletePayment
  , createIncome
  , editIncome
  , deleteIncome
  , signOut
  )

import Task as Task exposing (Task)
import Http
import Date
import Json.Decode exposing ((:=))
import Json.Encode as Json
import Date exposing (Date)

import Date.Extra.Format as DateFormat

import Utils.Http as HttpUtils

import Model.Payment exposing (..)
import Model.Income exposing (incomesDecoder, incomeIdDecoder, IncomeId)
import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder)
import Model.Init exposing (Init)

signIn : String -> Task Http.Error ()
signIn email =
  HttpUtils.request "POST" ("/signIn?email=" ++ email)
    |> Task.map (always ())

createPayment : String -> Int -> Date -> Frequency -> Task Http.Error PaymentId
createPayment name cost date frequency =
  Json.object
    [ ("name", Json.string name)
    , ("cost", Json.int cost)
    , ("date", Json.string (DateFormat.isoDateString date))
    , ("frequency", Json.string (toString frequency))
    ]
    |> HttpUtils.jsonRequest "POST" "/payment"
    |> flip Task.andThen (HttpUtils.decodeHttpValue <| "id" := paymentIdDecoder)

editPayment : PaymentId -> String -> Int -> Date -> Frequency -> Task Http.Error ()
editPayment paymentId name cost date frequency =
  Json.object
    [ ("id", Json.int paymentId)
    , ("name", Json.string name)
    , ("cost", Json.int cost)
    , ("date", Json.string (DateFormat.isoDateString date))
    , ("frequency", Json.string (toString frequency))
    ]
    |> HttpUtils.jsonRequest "PUT" "/payment"
    |> Task.map (always ())

deletePayment : PaymentId -> Task Http.Error ()
deletePayment paymentId =
  HttpUtils.request "DELETE" ("/payment?id=" ++ (toString paymentId))
    |> Task.map (always ())

createIncome : Int -> Date -> Task Http.Error IncomeId
createIncome amount date =
  Json.object
    [ ("amount", Json.int amount)
    , ("date", Json.string (DateFormat.isoDateString date))
    ]
    |> HttpUtils.jsonRequest "POST" "/income"
    |> flip Task.andThen (HttpUtils.decodeHttpValue <| "id" := incomeIdDecoder)

editIncome : IncomeId -> Int -> Date -> Task Http.Error ()
editIncome incomeId amount date =
  Json.object
    [ ("id", Json.int incomeId)
    , ("amount", Json.int amount)
    , ("date", Json.string (DateFormat.isoDateString date))
    ]
    |> HttpUtils.jsonRequest "PUT" "/income"
    |> Task.map (always ())

deleteIncome : IncomeId -> Task Http.Error ()
deleteIncome incomeId =
  HttpUtils.request "DELETE" ("/income?id=" ++ (toString incomeId))
    |> Task.map (always ())

signOut : Task Http.Error ()
signOut =
  HttpUtils.request "POST" "/signOut"
    |> Task.map (always ())