-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
3 changed files
with
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
defmodule BookStore.Books do | ||
import Ecto.Query, warn: false | ||
|
||
alias BookStore.Books.Book | ||
alias BookStore.{BookDynamicSupervisor, BookRegistry, Repo} | ||
|
||
# All the OTP/actor model based calls | ||
def actor_all do | ||
BookDynamicSupervisor.all_book_pids() | ||
|> Enum.reduce([], fn pid, acc -> | ||
case actor_read(pid) do | ||
%Book{} = book -> [book | acc] | ||
_ -> acc | ||
end | ||
end) | ||
end | ||
|
||
def actor_read(book_pid) when is_pid(book_pid) do | ||
book_pid | ||
|> GenServer.call(:read) | ||
|> case do | ||
%Book{} = book -> book | ||
_ -> {:error, :not_found} | ||
end | ||
end | ||
|
||
def actor_read(book_id) do | ||
book_id | ||
|> BookRegistry.lookup_book() | ||
|> case do | ||
{:ok, pid} -> GenServer.call(pid, :read) | ||
error -> error | ||
end | ||
end | ||
|
||
def actor_order(book_id) do | ||
book_id | ||
|> BookRegistry.lookup_book() | ||
|> case do | ||
{:ok, pid} -> GenServer.call(pid, :order_copy) | ||
error -> error | ||
end | ||
end | ||
|
||
# Database only calls | ||
def all do | ||
Repo.all(Book) | ||
end | ||
|
||
def read(id) do | ||
case Repo.get(Book, id) do | ||
%Book{} = book -> | ||
book | ||
|
||
_ -> | ||
{:error, :not_found} | ||
end | ||
end | ||
|
||
def order(book_id) do | ||
{_, result} = | ||
Repo.transaction(fn -> | ||
case Repo.get(Book, book_id) do | ||
%Book{quantity: 0} -> | ||
:no_copies_available | ||
|
||
%Book{quantity: quantity} = book -> | ||
update_book(book, %{quantity: quantity - 1}) | ||
:ok | ||
|
||
thing -> | ||
IO.inspect(thing) | ||
{:error, :not_found} | ||
end | ||
end) | ||
|
||
result | ||
end | ||
|
||
def update_book(%Book{} = book, attrs) do | ||
book | ||
|> Book.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
def create(attrs \\ %{}) do | ||
%Book{} | ||
|> Book.changeset(attrs) | ||
|> Repo.insert() | ||
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,50 @@ | ||
defmodule BookStoreWeb.BookController do | ||
use BookStoreWeb, :controller | ||
|
||
alias BookStore.Books | ||
alias BookStore.Books.Book | ||
|
||
def index(conn, _params) do | ||
# books = Books.actor_all() | ||
books = Books.all() | ||
|
||
conn | ||
|> json(books) | ||
end | ||
|
||
def show(conn, %{"id" => book_id}) do | ||
# book = Books.actor_read(book_id) | ||
book = Books.read(book_id) | ||
|
||
case book do | ||
%Book{} = book -> | ||
conn | ||
|> json(book) | ||
|
||
_ -> | ||
conn | ||
|> put_status(404) | ||
|> json(%{error: "Not found"}) | ||
end | ||
end | ||
|
||
def order(conn, %{"book_id" => book_id}) do | ||
# status = Books.actor_order(book_id) | ||
status = Books.order(book_id) | ||
|
||
case status do | ||
:ok -> | ||
conn | ||
|> put_status(201) | ||
|> json(%{status: "Order placed"}) | ||
|
||
:no_copies_available -> | ||
json(conn, %{status: "Not enough copies on hand to complete order"}) | ||
|
||
{:error, :not_found} -> | ||
conn | ||
|> put_status(404) | ||
|> json(%{error: "Not found"}) | ||
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