From cfca18262c1ff48dcb683ddab7d03cf8e55573ff Mon Sep 17 00:00:00 2001 From: Joris Date: Fri, 24 Mar 2017 09:21:04 +0000 Subject: Features/categories --- src/client/elm/Server.elm | 138 +++++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 56 deletions(-) (limited to 'src/client/elm/Server.elm') diff --git a/src/client/elm/Server.elm b/src/client/elm/Server.elm index c017548..7f25876 100644 --- a/src/client/elm/Server.elm +++ b/src/client/elm/Server.elm @@ -6,14 +6,17 @@ module Server exposing , createIncome , editIncome , deleteIncome + , createCategory + , editCategory + , deleteCategory , signOut ) import Task as Task exposing (Task) -import Http +import Http exposing (Error) import Date -import Json.Decode exposing ((:=)) -import Json.Encode as Json +import Json.Decode as Decode +import Json.Encode as Encode import Date exposing (Date) import Date.Extra.Format as DateFormat @@ -21,68 +24,91 @@ import Date.Extra.Format as DateFormat import Utils.Http as HttpUtils import Model.Payment exposing (..) -import Model.Income exposing (incomesDecoder, incomeIdDecoder, IncomeId) +import Model.Income exposing (incomeIdDecoder, IncomeId) +import Model.Category exposing (categoryIdDecoder, CategoryId) 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 ()) +signIn : String -> (Result Error String -> msg) -> Cmd msg +signIn email = HttpUtils.request "POST" ("/signIn?email=" ++ email) Http.expectString -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) +createPayment : String -> Int -> Date -> CategoryId -> Frequency -> (Result Error PaymentId -> msg) -> Cmd msg +createPayment name cost date categoryId frequency handleResult = + let json = + Encode.object + [ ("name", Encode.string name) + , ("cost", Encode.int cost) + , ("date", Encode.string (DateFormat.isoDateString date)) + , ("category", Encode.int categoryId) + , ("frequency", Encode.string (toString frequency)) + ] + expect = Http.expectJson (Decode.field "id" paymentIdDecoder) + in HttpUtils.jsonRequest "POST" "/payment" expect handleResult json -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 ()) +editPayment : PaymentId -> String -> Int -> Date -> CategoryId -> Frequency -> (Result Error String -> msg) -> Cmd msg +editPayment paymentId name cost date categoryId frequency handleResult = + let json = + Encode.object + [ ("id", Encode.int paymentId) + , ("name", Encode.string name) + , ("cost", Encode.int cost) + , ("date", Encode.string (DateFormat.isoDateString date)) + , ("category", Encode.int categoryId) + , ("frequency", Encode.string (toString frequency)) + ] + in HttpUtils.jsonRequest "PUT" "/payment" Http.expectString handleResult json -deletePayment : PaymentId -> Task Http.Error () +deletePayment : PaymentId -> (Result Error String -> msg) -> Cmd msg deletePayment paymentId = - HttpUtils.request "DELETE" ("/payment?id=" ++ (toString paymentId)) - |> Task.map (always ()) + HttpUtils.request "DELETE" ("/payment?id=" ++ (toString paymentId)) Http.expectString -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) +createIncome : Int -> Date -> (Result Error IncomeId -> msg) -> Cmd msg +createIncome amount date handleResult = + let json = + Encode.object + [ ("amount", Encode.int amount) + , ("date", Encode.string (DateFormat.isoDateString date)) + ] + expect = Http.expectJson (Decode.field "id" incomeIdDecoder) + in HttpUtils.jsonRequest "POST" "/income" expect handleResult json -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 ()) +editIncome : IncomeId -> Int -> Date -> (Result Error String -> msg) -> Cmd msg +editIncome incomeId amount date handleResult = + let json = + Encode.object + [ ("id", Encode.int incomeId) + , ("amount", Encode.int amount) + , ("date", Encode.string (DateFormat.isoDateString date)) + ] + in HttpUtils.jsonRequest "PUT" "/income" Http.expectString handleResult json -deleteIncome : IncomeId -> Task Http.Error () +deleteIncome : IncomeId -> (Result Error String -> msg) -> Cmd msg deleteIncome incomeId = - HttpUtils.request "DELETE" ("/income?id=" ++ (toString incomeId)) - |> Task.map (always ()) + HttpUtils.request "DELETE" ("/income?id=" ++ (toString incomeId)) Http.expectString -signOut : Task Http.Error () -signOut = - HttpUtils.request "POST" "/signOut" - |> Task.map (always ()) +createCategory : String -> String -> (Result Error CategoryId -> msg) -> Cmd msg +createCategory name color handleResult = + let json = + Encode.object + [ ("name", Encode.string name) + , ("color", Encode.string color) + ] + expect = Http.expectJson (Decode.field "id" categoryIdDecoder) + in HttpUtils.jsonRequest "POST" "/category" expect handleResult json + +editCategory : CategoryId -> String -> String -> (Result Error String -> msg) -> Cmd msg +editCategory categoryId name color handleResult = + let json = + Encode.object + [ ("id", Encode.int categoryId) + , ("name", Encode.string name) + , ("color", Encode.string color) + ] + in HttpUtils.jsonRequest "PUT" "/category" Http.expectString handleResult json + +deleteCategory : CategoryId -> (Result Error String -> msg) -> Cmd msg +deleteCategory categoryId = + HttpUtils.request "DELETE" ("/category?id=" ++ (toString categoryId)) Http.expectString + +signOut : (Result Error String -> msg) -> Cmd msg +signOut = HttpUtils.request "POST" "/signOut" Http.expectString -- cgit v1.2.3