module Server exposing ( signIn , addPayment , deletePayment , addIncome , deleteIncome , signOut ) import Task as Task exposing (Task) import Http import Date import Json.Decode exposing ((:=)) import Json.Encode as Json import Time exposing (Time) 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 ()) addPayment : String -> Int -> Frequency -> Task Http.Error PaymentId addPayment name cost frequency = post ("/payment/add?name=" ++ name ++ "&cost=" ++ (toString cost) ++ "&frequency=" ++ (toString frequency)) |> flip Task.andThen (decodeHttpValue <| "id" := paymentIdDecoder) deletePayment : PaymentId -> Task Http.Error () deletePayment paymentId = delete ("/payment?id=" ++ (toString paymentId)) |> Task.map (always ()) addIncome : Time -> Int -> Task Http.Error IncomeId addIncome time amount = Json.object [ ("day", Json.string (DateFormat.isoDateString (Date.fromTime time))) , ("amount", Json.int amount) ] |> 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 ())