Skip to content

Commit

Permalink
Made some changes to model to work with Google Books API
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondboswel committed Nov 11, 2017
1 parent 753a93a commit eb55294
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
15 changes: 13 additions & 2 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@ use Mix.Config
config :amnesia_api, AmnesiaApiWeb.Endpoint,
load_from_system_env: true,
url: [host: "example.com", port: 80],
cache_static_manifest: "priv/static/cache_manifest.json"
server: true,
cache_static_manifest: "priv/static/cache_manifest.json",
secret_key_base: "${SECRET_KEY_BASE}"
# secret_key_base: "Di1NydQzZbRT8/7KjGbq2cnhHwuLyJfo/iNX5DKLu1WI+Czl7IJXdyLPzW8ej+jw"

# Do not print debug messages in production
config :logger, level: :info


config :amnesia_api, AmnesiaApi.Repo,
adapter: Ecto.Adapters.MySQL,
url: "${DATABASE_URL}",
database: "",
ssl: true,
pool_size: 10

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
Expand Down Expand Up @@ -61,4 +72,4 @@ config :logger, level: :info

# Finally import the config/prod.secret.exs
# which should be versioned separately.
import_config "prod.secret.exs"
# import_config "prod.secret.exs"
9 changes: 6 additions & 3 deletions lib/amnesia_api/amnesia/book.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ defmodule AmnesiaApi.Amnesia.Book do
field :subtitle, :string
field :title, :string
field :summary, :string
many_to_many :user, AmnesiaApi.Accounts.User, join_through: "user_books"
field :google_id, :string
field :cover_url, :string
belongs_to :user, AmnesiaApi.Accounts.User
many_to_many :authors, AmnesiaApi.Amnesia.Author, join_through: "book_authors"
many_to_many :sections, AmnesiaApi.Amnesia.Section, join_through: "book_sections"

Expand All @@ -18,7 +20,8 @@ defmodule AmnesiaApi.Amnesia.Book do
@doc false
def changeset(%Book{} = book, attrs) do
book
|> cast(attrs, [:title, :subtitle, :summary])
|> validate_required([:title, :subtitle])
|> cast(attrs, [:title, :subtitle, :summary, :user_id, :google_id, :cover_url])
|> validate_required([:title, :google_id])
|> foreign_key_constraint(:user_id)
end
end
62 changes: 35 additions & 27 deletions lib/amnesia_api_web/controllers/book_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ defmodule AmnesiaApiWeb.BookController do
render(conn, "index.json", books: books)
end

def index(conn, %{"user_id" => user_id}) do
books = AmnesiaApi.Repo.all(from b in AmnesiaApi.Amnesia.Book, where: b.user_id == ^user_id)
|> Repo.preload(:authors)
|> Repo.preload(:sections)
Logger.debug "Books: #{inspect books}"
render(conn, "index.json", books: books)
end

def index(conn, _params) do
books = AmnesiaApi.Repo.all(from b in AmnesiaApi.Amnesia.Book)
|> Repo.preload(:authors)
Expand All @@ -28,35 +36,35 @@ defmodule AmnesiaApiWeb.BookController do
end

# Need to add or link authors if provided
def create(conn, %{"book" => %{"authors" => authors, "title" => title, "subtitle" => subtitle}} ) do
new_authors = Enum.filter(authors, fn(author) -> !Map.has_key?(author, "id") end)
existing_authors = Enum.filter(authors, fn(author) -> Map.has_key?(author, "id") end)
Enum.each(new_authors, fn(a) ->
case AmnesiaApi.Repo.insert(AmnesiaApi.Amnesia.Author.changeset(%Author{}, a)) do
{:ok, author } ->
Logger.info "author: #{inspect author}"
existing_authors = [author | existing_authors]
Logger.debug "existing_authors: #{inspect existing_authors}"
{:error, changeset} -> Logger.error "Could not add author: #{inspect changeset}"
end
end)
Logger.debug "existing_authors: #{inspect existing_authors}"
case Amnesia.create_book(%{title: title, subtitle: subtitle}) do
{:ok, book} ->
book = AmnesiaApi.Repo.preload(book, :sections)
Logger.warn "Book: #{inspect book}"
Logger.debug "Existing authors: #{inspect existing_authors}"
res = Enum.each(existing_authors, fn(author) -> AmnesiaApi.Repo.insert(%BookAuthors{book_id: book.id, author_id: author.id}) end)
Logger.info "Add book author result: #{inspect res}"
conn
|> put_status(:created)
|> put_resp_header("location", book_path(conn, :show, book))
|> render("show.json", book: book)
# def create(conn, %{"book" => %{"authors" => authors, "title" => title, "subtitle" => subtitle}} ) do
# new_authors = Enum.filter(authors, fn(author) -> !Map.has_key?(author, "id") end)
# existing_authors = Enum.filter(authors, fn(author) -> Map.has_key?(author, "id") end)
# Enum.each(new_authors, fn(a) ->
# case AmnesiaApi.Repo.insert(AmnesiaApi.Amnesia.Author.changeset(%Author{}, a)) do
# {:ok, author } ->
# Logger.info "author: #{inspect author}"
# existing_authors = [author | existing_authors]
# Logger.debug "existing_authors: #{inspect existing_authors}"
# {:error, changeset} -> Logger.error "Could not add author: #{inspect changeset}"
# end
# end)
# Logger.debug "existing_authors: #{inspect existing_authors}"
# case Amnesia.create_book(%{title: title, subtitle: subtitle}) do
# {:ok, book} ->
# book = AmnesiaApi.Repo.preload(book, :sections)
# Logger.warn "Book: #{inspect book}"
# Logger.debug "Existing authors: #{inspect existing_authors}"
# res = Enum.each(existing_authors, fn(author) -> AmnesiaApi.Repo.insert(%BookAuthors{book_id: book.id, author_id: author.id}) end)
# Logger.info "Add book author result: #{inspect res}"
# conn
# |> put_status(:created)
# |> put_resp_header("location", book_path(conn, :show, book))
# |> render("show.json", book: book)

{:error, changeset} -> Logger.error "Could not add book: #{inspect changeset}"
end
# {:error, changeset} -> Logger.error "Could not add book: #{inspect changeset}"
# end

end
# end

def create(conn, %{"book" => book_params}) do
with {:ok, %Book{} = book} <- Amnesia.create_book(book_params) do
Expand Down
2 changes: 2 additions & 0 deletions lib/amnesia_api_web/views/book_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule AmnesiaApiWeb.BookView do
title: book.title,
subtitle: book.subtitle,
summary: book.summary,
google_id: book.google_id,
cover_url: book.cover_url,
sections: AmnesiaApiWeb.SectionView.render("index.json", %{sections: book.sections})
}
end
Expand Down
5 changes: 5 additions & 0 deletions priv/repo/migrations/20170810193457_create_books.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ defmodule AmnesiaApi.Repo.Migrations.CreateBooks do
add :title, :string
add :subtitle, :string
add :summary, :string
add :google_id, :string
add :cover_url, :string
add :user_id, references(:users, on_delete: :nothing)
timestamps()
end

create index(:books, [:user_id])

end
end

0 comments on commit eb55294

Please sign in to comment.