This commit is contained in:
Haim Kortovich
2026-03-05 11:35:01 -05:00
commit 072dbf6e66
43 changed files with 1400 additions and 0 deletions

View 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