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 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) init : Task Http.Error Init init = Task.map Init (Http.get usersDecoder "/api/users") `Task.andMap` (Http.get ("id" := userIdDecoder) "/api/whoAmI") `Task.andMap` (Http.get paymentsDecoder "/api/payments") `Task.andMap` (Http.get paymentsDecoder "/api/monthlyPayments") `Task.andMap` (Http.get incomesDecoder "/api/incomes") signIn : String -> Task Http.Error () signIn email = post ("/api/signIn?email=" ++ email) |> Task.map (always ()) addPayment : String -> String -> PaymentFrequency -> 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 : Payment -> PaymentFrequency -> Task Http.Error () deletePayment payment frequency = post ("/api/payment/delete?id=" ++ (toString payment.id)) |> Task.map (always ()) setIncome : Int -> Task Http.Error IncomeId setIncome amount = post ("/api/income?amount=" ++ (toString amount)) |> flip Task.andThen (decodeHttpValue <| "id" := incomeIdDecoder) signOut : Task Http.Error () signOut = post "/api/signOut" |> Task.map (always ())