aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Server.elm
blob: 9522d17cec854b6ffefaefd3b24e2dac3e8830db (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
module Server exposing
  ( signIn
  , createPayment
  , deletePayment
  , createIncome
  , 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 exposing (..)

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 =
  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))
    ]
    |> Json.encode 0
    |> Http.string
    |> postWithBody "/payment"
    |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder)

deletePayment : PaymentId -> Task Http.Error ()
deletePayment paymentId =
  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))
    ]
    |> Json.encode 0
    |> Http.string
    |> postWithBody "/income"
    |> flip Task.andThen (decodeHttpValue <| "id" := incomeIdDecoder)

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

signOut : Task Http.Error ()
signOut =
  post "/signOut"
    |> Task.map (always ())