WIP
This commit is contained in:
84
lib/customer_service_web/controllers/customer.ex
Normal file
84
lib/customer_service_web/controllers/customer.ex
Normal file
@@ -0,0 +1,84 @@
|
||||
defmodule CustomerServiceWeb.Customer do
|
||||
use CustomerServiceWeb, :controller
|
||||
|
||||
alias CustomerServiceWeb.Schemas.CreateCustomerRequest
|
||||
alias CustomerServiceWeb.Schemas.CustomerResponse
|
||||
alias CustomerService.Commands.CreateCustomer
|
||||
alias CustomerService.CommandedApp
|
||||
use OpenApiSpex.ControllerSpecs
|
||||
|
||||
tags ["Customers"]
|
||||
|
||||
operation :create,
|
||||
summary: "Create customer",
|
||||
request_body: {"Customer data", "application/json", CreateCustomerRequest},
|
||||
responses: [
|
||||
ok: {"Customer created", "application/json", CustomerResponse}
|
||||
]
|
||||
|
||||
def create(conn, params) do
|
||||
customer_id = Ecto.UUID.generate()
|
||||
|
||||
command =
|
||||
%CreateCustomer{
|
||||
id: customer_id,
|
||||
first_name: params["first_name"],
|
||||
last_name: params["last_name"],
|
||||
birth_date: Date.from_iso8601!(params["birth_date"]),
|
||||
gender: params["gender"],
|
||||
email: params["email"],
|
||||
phone: params["phone"]
|
||||
}
|
||||
|
||||
case CommandedApp.dispatch(command, consistency: :strong) do
|
||||
:ok ->
|
||||
json(conn, %{id: customer_id})
|
||||
|
||||
{:error, reason} ->
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> json(%{error: inspect(reason)})
|
||||
end
|
||||
end
|
||||
|
||||
operation :show,
|
||||
summary: "Get customer",
|
||||
parameters: [
|
||||
id: [in: :path, type: :string, description: "Customer ID"]
|
||||
],
|
||||
responses: [
|
||||
ok: {"Customer", "application/json", CustomerResponse},
|
||||
not_found: {"Not found", "application/json", nil}
|
||||
]
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
case CustomerService.Repo.get(CustomerService.Projections.Customer, id) do
|
||||
nil ->
|
||||
send_resp(conn, 404, "")
|
||||
|
||||
customer ->
|
||||
json(conn, customer)
|
||||
end
|
||||
end
|
||||
|
||||
operation :index,
|
||||
summary: "List customers",
|
||||
responses: [
|
||||
ok:
|
||||
{"Customer list", "application/json",
|
||||
%OpenApiSpex.Schema{
|
||||
type: :array,
|
||||
items: CustomerResponse
|
||||
}}
|
||||
]
|
||||
|
||||
def index(conn, _) do
|
||||
case CustomerService.Repo.all(CustomerService.Projections.Customer) do
|
||||
nil ->
|
||||
send_resp(conn, 404, "")
|
||||
|
||||
customer ->
|
||||
json(conn, customer)
|
||||
end
|
||||
end
|
||||
end
|
||||
21
lib/customer_service_web/controllers/error_json.ex
Normal file
21
lib/customer_service_web/controllers/error_json.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule CustomerServiceWeb.ErrorJSON do
|
||||
@moduledoc """
|
||||
This module is invoked by your endpoint in case of errors on JSON requests.
|
||||
|
||||
See config/config.exs.
|
||||
"""
|
||||
|
||||
# If you want to customize a particular status code,
|
||||
# you may add your own clauses, such as:
|
||||
#
|
||||
# def render("500.json", _assigns) do
|
||||
# %{errors: %{detail: "Internal Server Error"}}
|
||||
# end
|
||||
|
||||
# By default, Phoenix returns the status message from
|
||||
# the template name. For example, "404.json" becomes
|
||||
# "Not Found".
|
||||
def render(template, _assigns) do
|
||||
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user