我想在Maru中定義一個自定義類型並使用它來解析JSON post實體,並使用它進一步執行sql語句。但我不確定如何繼續。在Elixir中定義一個自定義的Maru類型並使用它來獲取Json POST實體
這裏是我的自定義類型
defmodule Maru.Types.Volume do
use Maru.Type
@type length :: Float
@type breadth :: Float
@type height :: Float
end
這種類型在路由器
defmodule My.Router.Box do
use Maru.Router
alias My.Repo.Box, as: :DB
namespace :select_volume do
params do
requires :volume, type: Volume
post do
volume = DB.getBoxWithRequiredVolume(params)
conn |> put_status(200) |> json(volume)
end
end
然後在這裏使用SQL查詢
defmodule My.Repo.Box do
import Ecto.Query
require Logger
def getBoxWithRequiredVolume(params) do
volume = params[:volume]
query = from box in My.Box,
select: (
%{id: box.id}
),
where: (box.length == ^volume[:length] and box.breadth == ^volume[:breadth] and box.height == ^volume[:height])
query |> My.Repo.all
end
end
這是使用REST查詢的實體:
http://localhost:8880/select_volume
{
"volume":{
"length": 20,
"breadth": 5,
"height": 5
}
}
錯誤:
%Maru.Exceptions.InvalidFormatter{param: :volume, reason: :illegal,
value: %{"breadth" => 5, "height" => 5, "length" => 20}}
你的問題是什麼? – mudasobwa
上面的代碼不起作用。我無法使用捲來分析實體 –
這仍然不是問題。另外,你是否收到任何錯誤?怎麼了?請根據本網站的規則重新提出您的問題,否則將被關閉。 – mudasobwa