-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(Users): POST user endpoint * feat(specs): Created specs schemas for POST user input and response
- Loading branch information
1 parent
a16cc9e
commit b93aba0
Showing
13 changed files
with
252 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule BookMyGigs.Users do | ||
@moduledoc """ | ||
The users context | ||
""" | ||
|
||
alias BookMyGigs.Accounts | ||
alias BookMyGigs.Users.Storage | ||
|
||
defmodule User do | ||
@moduledoc """ | ||
Module defining the context struct for a user. | ||
""" | ||
|
||
@derive Jason.Encoder | ||
|
||
defstruct [:id, :account_id, :username, :first_name, :last_name, :birthday] | ||
|
||
@type t :: %__MODULE__{ | ||
id: String.t(), | ||
account_id: Accounts.Account.id(), | ||
username: String.t(), | ||
first_name: String.t(), | ||
last_name: String.t(), | ||
birthday: Date.t() | ||
} | ||
end | ||
|
||
def create_user(%{"user" => user_params}, account_id) do | ||
Storage.create_user(user_params, account_id) | ||
end | ||
|
||
def to_context_struct(%Storage.User{} = index_db) do | ||
struct(User, Map.from_struct(index_db)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule BookMyGigs.Users.Storage do | ||
@moduledoc """ | ||
Module providing functionalities to interact with the users table. | ||
""" | ||
|
||
alias BookMyGigs.Accounts.Storage.Account | ||
alias BookMyGigs.Users | ||
alias BookMyGigs.Users.Storage | ||
alias BookMyGigs.Utils | ||
alias BookMyGigs.Repo | ||
|
||
def create_user(user_params, account_id) do | ||
params = %{ | ||
account_id: account_id, | ||
username: user_params["username"], | ||
first_name: user_params["first_name"], | ||
last_name: user_params["last_name"], | ||
birthday: Utils.DateUtils.parse_date(user_params["birthday"]) | ||
} | ||
|
||
case Repo.get(Account, account_id) do | ||
nil -> | ||
{:error, :account_not_found} | ||
|
||
_account -> | ||
changeset = Storage.User.changeset(%Storage.User{}, params) | ||
|
||
case Repo.insert(changeset) do | ||
{:ok, user} -> | ||
Users.to_context_struct(user) | ||
|
||
{:error, changeset} -> | ||
{:error, changeset} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule BookMyGigsWeb.Users.Schemas.CreateUserInput do | ||
@moduledoc """ | ||
Specs describing the response when creating an user. | ||
""" | ||
|
||
alias OpenApiSpex.Schema | ||
|
||
require OpenApiSpex | ||
|
||
OpenApiSpex.schema(%{ | ||
title: "Create User Input", | ||
description: "Valid input values to create an user", | ||
type: :object, | ||
properties: %{ | ||
user: %Schema{ | ||
type: :object, | ||
properties: %{ | ||
username: %Schema{ | ||
type: :string | ||
}, | ||
first_name: %Schema{ | ||
type: :string | ||
}, | ||
last_name: %Schema{ | ||
type: :string | ||
}, | ||
birthday: %Schema{ | ||
type: :string | ||
} | ||
} | ||
} | ||
}, | ||
example: %{ | ||
"user" => %{ | ||
"username" => "Gabdu20", | ||
"first_name" => "Gabriel", | ||
"last_name" => "Parizet", | ||
"birthday" => "04/07/1994" | ||
} | ||
} | ||
}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule BookMyGigsWeb.Users.Schemas.UserResponse do | ||
@moduledoc """ | ||
Specs describing the response when creating a user. | ||
""" | ||
|
||
alias OpenApiSpex.Schema | ||
|
||
require OpenApiSpex | ||
|
||
OpenApiSpex.schema(%{ | ||
title: "User response", | ||
description: "Schema describing the response when creating a user", | ||
type: :object, | ||
properties: %{ | ||
id: %Schema{ | ||
type: :string, | ||
format: :uuid | ||
}, | ||
account_id: %Schema{ | ||
type: :string, | ||
format: :uuid | ||
}, | ||
username: %Schema{ | ||
type: :string | ||
}, | ||
first_name: %Schema{ | ||
type: :string | ||
}, | ||
last_name: %Schema{ | ||
type: :string | ||
}, | ||
birthday: %Schema{ | ||
type: :string, | ||
format: :date | ||
} | ||
}, | ||
example: %{ | ||
"id" => "b57ed6b1-02a7-4cf3-89bb-34ac1f424b79", | ||
"account_id" => "1e531b65-44dc-44d8-a772-0f2353133444", | ||
"username" => "MyUsername", | ||
"first_name" => "Gabriel", | ||
"last_name" => "Parizet", | ||
"birthday" => "1994-04-20" | ||
} | ||
}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule BookMyGigsWeb.UsersController do | ||
@moduledoc """ | ||
The Users Controller | ||
""" | ||
use BookMyGigsWeb, :controller | ||
use OpenApiSpex.ControllerSpecs | ||
|
||
alias BookMyGigs.Users | ||
alias BookMyGigsWeb.Users.Schemas | ||
alias OpenApiSpex.Schema | ||
|
||
operation(:create, | ||
summary: "Create an user", | ||
parameters: [ | ||
account_id: [ | ||
in: :path, | ||
description: "User id", | ||
schema: %Schema{type: :string, format: :uuid}, | ||
example: "61492a85-3946-4b62-8887-2952af807c26" | ||
] | ||
], | ||
request_body: {"Create user input", "application/json", Schemas.CreateUserInput}, | ||
responses: [ | ||
ok: {"User response", "application/json", Schemas.UserResponse}, | ||
bad_request: "Invalid input values" | ||
], | ||
ok: "User successfully created" | ||
) | ||
|
||
def create(conn, params) do | ||
account_id = conn.private[:guardian_default_resource].id | ||
|
||
user = | ||
params | ||
|> Users.create_user(account_id) | ||
|> Jason.encode!() | ||
|
||
conn | ||
|> put_resp_content_type("application/json") | ||
|> send_resp(200, user) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters