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

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

import Utils.Http exposing (..)

import Model.Payment exposing (..)
import Model.Payer exposing (Payers, payersDecoder)
import Model.User exposing (Users, usersDecoder, UserId, userIdDecoder)
import Model.Init exposing (Init)

init : Task Http.Error Init
init =
  Task.map Init (Http.get usersDecoder "/users")
    `Task.andMap` (Http.get ("id" := userIdDecoder) "/whoAmI")
    `Task.andMap` (Http.get paymentsDecoder "/payments")
    `Task.andMap` (Http.get paymentsDecoder "/monthlyPayments")
    `Task.andMap` (Http.get ("number" := Json.int) "/payments/count")
    `Task.andMap` (Http.get payersDecoder "/payers")

signIn : String -> Task Http.Error Init
signIn assertion =
  post ("/signIn?assertion=" ++ assertion)
    |> flip Task.andThen (always init)

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

deletePayment : Payment -> PaymentFrequency -> Task Http.Error ()
deletePayment payment frequency =
  post ("payment/delete?id=" ++ (toString payment.id))
    |> Task.map (always ())

setIncome : Time -> Int -> Task Http.Error ()
setIncome currentTime amount =
  post ("/income?amount=" ++ (toString amount))
    |> Task.map (always ())

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