module Server exposing ( signIn , createPayment , editPayment , deletePayment , createIncome , editIncome , 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 as HttpUtils 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 = HttpUtils.request "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)) ] |> HttpUtils.jsonRequest "POST" "/payment" |> flip Task.andThen (HttpUtils.decodeHttpValue <| "id" := paymentIdDecoder) editPayment : PaymentId -> String -> Int -> Date -> Frequency -> Task Http.Error () editPayment paymentId name cost date frequency = Json.object [ ("id", Json.int paymentId) , ("name", Json.string name) , ("cost", Json.int cost) , ("date", Json.string (DateFormat.isoDateString date)) , ("frequency", Json.string (toString frequency)) ] |> HttpUtils.jsonRequest "PUT" "/payment" |> Task.map (always ()) deletePayment : PaymentId -> Task Http.Error () deletePayment paymentId = HttpUtils.request "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)) ] |> HttpUtils.jsonRequest "POST" "/income" |> flip Task.andThen (HttpUtils.decodeHttpValue <| "id" := incomeIdDecoder) editIncome : IncomeId -> Int -> Date -> Task Http.Error () editIncome incomeId amount date = Json.object [ ("id", Json.int incomeId) , ("amount", Json.int amount) , ("date", Json.string (DateFormat.isoDateString date)) ] |> HttpUtils.jsonRequest "PUT" "/income" |> Task.map (always ()) deleteIncome : IncomeId -> Task Http.Error () deleteIncome incomeId = HttpUtils.request "DELETE" ("/income?id=" ++ (toString incomeId)) |> Task.map (always ()) signOut : Task Http.Error () signOut = HttpUtils.request "POST" "/signOut" |> Task.map (always ())