aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Server.elm
blob: 7f258760275b398ca3bac888dbef81d45dfcd002 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
module Server exposing
  ( signIn
  , createPayment
  , editPayment
  , deletePayment
  , createIncome
  , editIncome
  , deleteIncome
  , createCategory
  , editCategory
  , deleteCategory
  , signOut
  )

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

import Date.Extra.Format as DateFormat

import Utils.Http as HttpUtils

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

signIn : String -> (Result Error String -> msg) -> Cmd msg
signIn email = HttpUtils.request "POST" ("/signIn?email=" ++ email) Http.expectString

createPayment : String -> Int -> Date -> CategoryId -> Frequency -> (Result Error PaymentId -> msg) -> Cmd msg
createPayment name cost date categoryId frequency handleResult =
  let json =
        Encode.object
          [ ("name", Encode.string name)
          , ("cost", Encode.int cost)
          , ("date", Encode.string (DateFormat.isoDateString date))
          , ("category", Encode.int categoryId)
          , ("frequency", Encode.string (toString frequency))
          ]
      expect = Http.expectJson (Decode.field "id" paymentIdDecoder)
  in  HttpUtils.jsonRequest "POST" "/payment" expect handleResult json

editPayment : PaymentId -> String -> Int -> Date -> CategoryId -> Frequency -> (Result Error String -> msg) -> Cmd msg
editPayment paymentId name cost date categoryId frequency handleResult =
  let json =
        Encode.object
          [ ("id", Encode.int paymentId)
          , ("name", Encode.string name)
          , ("cost", Encode.int cost)
          , ("date", Encode.string (DateFormat.isoDateString date))
          , ("category", Encode.int categoryId)
          , ("frequency", Encode.string (toString frequency))
          ]
  in  HttpUtils.jsonRequest "PUT" "/payment" Http.expectString handleResult json

deletePayment : PaymentId -> (Result Error String -> msg) -> Cmd msg
deletePayment paymentId =
  HttpUtils.request "DELETE" ("/payment?id=" ++ (toString paymentId)) Http.expectString

createIncome : Int -> Date -> (Result Error IncomeId -> msg) -> Cmd msg
createIncome amount date handleResult =
  let json =
        Encode.object
          [ ("amount", Encode.int amount)
          , ("date", Encode.string (DateFormat.isoDateString date))
          ]
      expect = Http.expectJson (Decode.field "id" incomeIdDecoder)
  in  HttpUtils.jsonRequest "POST" "/income" expect handleResult json

editIncome : IncomeId -> Int -> Date -> (Result Error String -> msg) -> Cmd msg
editIncome incomeId amount date handleResult =
  let json =
        Encode.object
          [ ("id", Encode.int incomeId)
          , ("amount", Encode.int amount)
          , ("date", Encode.string (DateFormat.isoDateString date))
          ]
  in  HttpUtils.jsonRequest "PUT" "/income" Http.expectString handleResult json

deleteIncome : IncomeId -> (Result Error String -> msg) -> Cmd msg
deleteIncome incomeId =
  HttpUtils.request "DELETE" ("/income?id=" ++ (toString incomeId)) Http.expectString

createCategory : String -> String -> (Result Error CategoryId -> msg) -> Cmd msg
createCategory name color handleResult =
  let json =
        Encode.object
          [ ("name", Encode.string name)
          , ("color", Encode.string color)
          ]
      expect = Http.expectJson (Decode.field "id" categoryIdDecoder)
  in  HttpUtils.jsonRequest "POST" "/category" expect handleResult json

editCategory : CategoryId -> String -> String -> (Result Error String -> msg) -> Cmd msg
editCategory categoryId name color handleResult =
  let json =
        Encode.object
          [ ("id", Encode.int categoryId)
          , ("name", Encode.string name)
          , ("color", Encode.string color)
          ]
  in  HttpUtils.jsonRequest "PUT" "/category" Http.expectString handleResult json

deleteCategory : CategoryId -> (Result Error String -> msg) -> Cmd msg
deleteCategory categoryId =
  HttpUtils.request "DELETE" ("/category?id=" ++ (toString categoryId)) Http.expectString

signOut : (Result Error String -> msg) -> Cmd msg
signOut = HttpUtils.request "POST" "/signOut" Http.expectString