aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Server.elm
blob: 36adb333fc07062ee0dea4d5922473757df105d8 (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
module Server
  ( signIn
  , addPayment
  , deletePayment
  , addIncome
  , deleteIncome
  , signOut
  ) where

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

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 ("/api/signIn?email=" ++ email)
    |> Task.map (always ())

addPayment : String -> String -> Frequency -> Task Http.Error PaymentId
addPayment name cost frequency =
  post ("/api/payment/add?name=" ++ name ++ "&cost=" ++ cost ++ "&frequency=" ++ (toString frequency))
    |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder)

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

addIncome : Date -> Int -> Task Http.Error IncomeId
addIncome creation amount =
  post ("/api/income?creation=" ++ (toString << Date.toTime <| creation) ++ "&amount=" ++ (toString amount))
    |> flip Task.andThen (decodeHttpValue <| "id" := incomeIdDecoder)

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

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