moves last server API logic inside its own module
This commit is contained in:
75
src/Api.elm
75
src/Api.elm
@@ -186,35 +186,52 @@ undoLastAction id = Http.request
|
||||
, tracker = Nothing
|
||||
}
|
||||
|
||||
sendRequest : ViewMode -> String -> E.Value -> Cmd Msg
|
||||
sendRequest mode id payload =
|
||||
let
|
||||
(endpoint, method) = case mode of
|
||||
Modes.Add ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||
, "POST"
|
||||
)
|
||||
Modes.Buy ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||
, "PUT"
|
||||
)
|
||||
Modes.Sell ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||
, "DELETE"
|
||||
)
|
||||
Modes.Grab ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
|
||||
, "POST")
|
||||
in
|
||||
Http.request
|
||||
{ method = method
|
||||
, headers = []
|
||||
, url = endpoint
|
||||
, body = Http.jsonBody payload
|
||||
, expect = Http.expectJson GotActionResult apiResponseDecoder
|
||||
, timeout = Nothing
|
||||
, tracker = Nothing
|
||||
}
|
||||
buildPayload : ViewMode -> List Item -> E.Value
|
||||
buildPayload mode items =
|
||||
case mode of
|
||||
Modes.Buy -> E.object
|
||||
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
|
||||
, ("global_mod", E.null )
|
||||
]
|
||||
Modes.Sell -> E.object
|
||||
[ ( "items", items |> E.list (\i -> E.list identity [E.int i.id, E.null]))
|
||||
, ("global_mod", E.null )
|
||||
]
|
||||
Modes.Grab -> E.object
|
||||
[ ( "items", items |> E.list (\i -> E.int i.id))
|
||||
, ("global_mod", E.null )
|
||||
]
|
||||
Modes.Add -> E.object
|
||||
[ ( "items", items |> E.list (\i -> E.int i.id))
|
||||
, ("global_mod", E.null )
|
||||
]
|
||||
|
||||
sendRequest : ViewMode -> String -> List Item -> Cmd Msg
|
||||
sendRequest mode id items =
|
||||
let
|
||||
(endpoint, method) = case mode of
|
||||
Modes.Add ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||
, "POST" )
|
||||
Modes.Buy ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||
, "PUT" )
|
||||
Modes.Sell ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/loot"
|
||||
, "DELETE" )
|
||||
Modes.Grab ->
|
||||
( "http://localhost:8088/api/players/" ++ id ++ "/claims"
|
||||
, "POST" )
|
||||
in
|
||||
Http.request
|
||||
{ method = method
|
||||
, headers = []
|
||||
, url = endpoint
|
||||
, body = Http.jsonBody <| buildPayload mode items
|
||||
, expect = Http.expectJson GotActionResult apiResponseDecoder
|
||||
, timeout = Nothing
|
||||
, tracker = Nothing
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user