-
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.
- Loading branch information
1 parent
93973a9
commit dc73d4e
Showing
12 changed files
with
447 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule AmnesiaApi.Amnesia.UserBook do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
alias AmnesiaApi.Amnesia.UserBook | ||
|
||
|
||
schema "user_books" do | ||
field :user_id, :id | ||
field :book_id, :id | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(%UserBook{} = user_book, attrs) do | ||
user_book | ||
|> cast(attrs, []) | ||
|> validate_required([]) | ||
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,51 @@ | ||
defmodule AmnesiaApiWeb.UserBookController do | ||
use AmnesiaApiWeb, :controller | ||
|
||
alias AmnesiaApi.Amnesia | ||
alias AmnesiaApi.Amnesia.UserBook | ||
alias AmnesiaApi.Repo | ||
|
||
action_fallback AmnesiaApiWeb.FallbackController | ||
|
||
def index(conn, _params) do | ||
user_books = Amnesia.list_user_books() | ||
render(conn, "index.json", user_books: user_books) | ||
end | ||
|
||
def create(conn, %{"user_book" => %{"user_id" => user_id, "book_id" => book_id}}) do | ||
user_book = Repo.get_by(UserBook, [book_id: book_id, user_id: user_id]) | ||
if user_book != nil do | ||
conn | ||
|> put_status(:created) | ||
|> put_resp_header("location", user_book_path(conn, :show, user_book)) | ||
|> render("show.json", user_book: user_book) | ||
else | ||
with {:ok, %UserBook{} = user_book} <- Amnesia.create_user_book(user_book_params) do | ||
conn | ||
|> put_status(:created) | ||
|> put_resp_header("location", user_book_path(conn, :show, user_book)) | ||
|> render("show.json", user_book: user_book) | ||
end | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
user_book = Amnesia.get_user_book!(id) | ||
render(conn, "show.json", user_book: user_book) | ||
end | ||
|
||
def update(conn, %{"id" => id, "user_book" => user_book_params}) do | ||
user_book = Amnesia.get_user_book!(id) | ||
|
||
with {:ok, %UserBook{} = user_book} <- Amnesia.update_user_book(user_book, user_book_params) do | ||
render(conn, "show.json", user_book: user_book) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
user_book = Amnesia.get_user_book!(id) | ||
with {:ok, %UserBook{}} <- Amnesia.delete_user_book(user_book) do | ||
send_resp(conn, :no_content, "") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule AmnesiaApiWeb.UserBookView do | ||
use AmnesiaApiWeb, :view | ||
alias AmnesiaApiWeb.UserBookView | ||
|
||
def render("index.json", %{user_books: user_books}) do | ||
%{data: render_many(user_books, UserBookView, "user_book.json")} | ||
end | ||
|
||
def render("show.json", %{user_book: user_book}) do | ||
%{data: render_one(user_book, UserBookView, "user_book.json")} | ||
end | ||
|
||
def render("user_book.json", %{user_book: user_book}) do | ||
%{id: user_book.id} | ||
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,15 @@ | ||
defmodule AmnesiaApi.Repo.Migrations.CreateUserBook do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:user_books) do | ||
add :user_id, references(:users, on_delete: :nothing) | ||
add :book_id, references(:books, on_delete: :nothing) | ||
|
||
timestamps() | ||
end | ||
|
||
create index(:user_books, [:user_id]) | ||
create index(:user_books, [:book_id]) | ||
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,15 @@ | ||
defmodule AmnesiaApi.Repo.Migrations.CreateUserBooks do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:user_books) do | ||
add :user_id, references(:users, on_delete: :nothing) | ||
add :book_id, references(:books, on_delete: :nothing) | ||
|
||
timestamps() | ||
end | ||
|
||
create index(:user_books, [:user_id]) | ||
create index(:user_books, [:book_id]) | ||
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
77 changes: 77 additions & 0 deletions
77
test/amnesia_api_web/controllers/user_book_controller_test.exs
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,77 @@ | ||
defmodule AmnesiaApiWeb.UserBookControllerTest do | ||
use AmnesiaApiWeb.ConnCase | ||
|
||
alias AmnesiaApi.Amnesia | ||
alias AmnesiaApi.Amnesia.UserBook | ||
|
||
@create_attrs %{} | ||
@update_attrs %{} | ||
@invalid_attrs %{} | ||
|
||
def fixture(:user_book) do | ||
{:ok, user_book} = Amnesia.create_user_book(@create_attrs) | ||
user_book | ||
end | ||
|
||
setup %{conn: conn} do | ||
{:ok, conn: put_req_header(conn, "accept", "application/json")} | ||
end | ||
|
||
describe "index" do | ||
test "lists all user_books", %{conn: conn} do | ||
conn = get conn, user_book_path(conn, :index) | ||
assert json_response(conn, 200)["data"] == [] | ||
end | ||
end | ||
|
||
describe "create user_book" do | ||
test "renders user_book when data is valid", %{conn: conn} do | ||
conn = post conn, user_book_path(conn, :create), user_book: @create_attrs | ||
assert %{"id" => id} = json_response(conn, 201)["data"] | ||
|
||
conn = get conn, user_book_path(conn, :show, id) | ||
assert json_response(conn, 200)["data"] == %{ | ||
"id" => id} | ||
end | ||
|
||
test "renders errors when data is invalid", %{conn: conn} do | ||
conn = post conn, user_book_path(conn, :create), user_book: @invalid_attrs | ||
assert json_response(conn, 422)["errors"] != %{} | ||
end | ||
end | ||
|
||
describe "update user_book" do | ||
setup [:create_user_book] | ||
|
||
test "renders user_book when data is valid", %{conn: conn, user_book: %UserBook{id: id} = user_book} do | ||
conn = put conn, user_book_path(conn, :update, user_book), user_book: @update_attrs | ||
assert %{"id" => ^id} = json_response(conn, 200)["data"] | ||
|
||
conn = get conn, user_book_path(conn, :show, id) | ||
assert json_response(conn, 200)["data"] == %{ | ||
"id" => id} | ||
end | ||
|
||
test "renders errors when data is invalid", %{conn: conn, user_book: user_book} do | ||
conn = put conn, user_book_path(conn, :update, user_book), user_book: @invalid_attrs | ||
assert json_response(conn, 422)["errors"] != %{} | ||
end | ||
end | ||
|
||
describe "delete user_book" do | ||
setup [:create_user_book] | ||
|
||
test "deletes chosen user_book", %{conn: conn, user_book: user_book} do | ||
conn = delete conn, user_book_path(conn, :delete, user_book) | ||
assert response(conn, 204) | ||
assert_error_sent 404, fn -> | ||
get conn, user_book_path(conn, :show, user_book) | ||
end | ||
end | ||
end | ||
|
||
defp create_user_book(_) do | ||
user_book = fixture(:user_book) | ||
{:ok, user_book: user_book} | ||
end | ||
end |
Oops, something went wrong.