Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
396 changes: 394 additions & 2 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ config :busi_api, BusiApi.Repo,
# watchers to your application. For example, we use it
# with webpack to recompile .js and .css sources.
config :busi_api, BusiApiWeb.Endpoint,
http: [port: 4000],
http: [port: 4001],
debug_errors: true,
code_reloader: true,
check_origin: false,
Expand Down
51 changes: 47 additions & 4 deletions lib/busi_api/directory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ defmodule BusiApi.Directory do
[%Business{}, ...]

"""
def list_businesses do
Repo.all(Business)
end
# def list_businesses do
# query = from b in Business,
# join: c in assoc(b, :colaborators),
# preload: [colaborators: c]
# Repo.all(query)
# end

def list_businesses, do: Repo.all(Business) |> Repo.preload([:colaborators])


@doc """
Gets a single business.
Expand All @@ -35,7 +41,7 @@ defmodule BusiApi.Directory do
** (Ecto.NoResultsError)

"""
def get_business!(id), do: Repo.get!(Business, id)
def get_business!(id), do: Repo.get!(Business, id) |> Repo.preload([:colaborators])

@doc """
Creates a business.
Expand Down Expand Up @@ -101,4 +107,41 @@ defmodule BusiApi.Directory do
def change_business(%Business{} = business, attrs \\ %{}) do
Business.changeset(business, attrs)
end

@spec get_businesess_collaborators(any, any) :: any
def get_businesess_collaborators(id,year) do
query = from b in Business,
where: b.id == ^id and b.year == ^year,
join: c in Colaborator,
on: c.business_id == b.id,
select: c
Repo.all(query)
end

alias BusiApi.Directory.Colaborator

def list_collaborators, do: Repo.all(Colaborator) |> Repo.preload([:business])

def get_collaborator!(id), do: Repo.get!(Colaborator, id) |> Repo.preload([:business])

def create_collaborator(attrs \\ %{}) do
%Colaborator{}
|> Colaborator.changeset(attrs)
|> Repo.insert()
end

def update_collaborator(%Colaborator{} = colaborator, attrs) do
colaborator
|> Colaborator.changeset(attrs)
|> Repo.update()
end

def delete_collaborator(%Colaborator{} = colaborator) do
Repo.delete(colaborator)
end

def change_collaborator(%Colaborator{} = colaborator, attrs \\ %{}) do
Colaborator.changeset(colaborator, attrs)
end

end
12 changes: 6 additions & 6 deletions lib/busi_api/directory/business.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ defmodule BusiApi.Directory.Business do

schema "businesses" do
field :description, :string
field :name, :string
field :tag, :string

field :name, :string
field :tag, :string
field :year, :integer
has_many :colaborators, BusiApi.Directory.Colaborator
timestamps()
end

@doc false
def changeset(business, attrs) do
business
|> cast(attrs, [:name, :description, :tag])
|> validate_required([:name, :description, :tag])
|> cast(attrs, [:name, :description, :tag, :year])
|> validate_required([:name, :description, :tag, :year])
end
end
19 changes: 19 additions & 0 deletions lib/busi_api/directory/colaborator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule BusiApi.Directory.Colaborator do
use Ecto.Schema
import Ecto.Changeset

schema "colaborators" do
field :email, :string
field :lastname, :string
field :name, :string
belongs_to :business, BusiApi.Directory.Business
timestamps()
end

def changeset(colaborator, attrs) do
colaborator
|> cast(attrs, [:name, :lastname, :email, :business_id])
|> validate_required([:name, :lastname, :email, :business_id])
|> assoc_constraint(:business)
end
end
5 changes: 3 additions & 2 deletions lib/busi_api_web/controllers/business_controller.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
defmodule BusiApiWeb.BusinessController do
use BusiApiWeb, :controller

alias BusiApi.Directory
alias BusiApi.Directory.Business
#alias BusiApi.Directory.Colaborator

action_fallback BusiApiWeb.FallbackController

def index(conn, _params) do
businesses = Directory.list_businesses()
|>IO.inspect()
render(conn, "index.json", businesses: businesses)
end

Expand All @@ -16,7 +17,7 @@ defmodule BusiApiWeb.BusinessController do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.business_path(conn, :show, business))
|> render("show.json", business: business)
|> render("business.json", business: business)
end
end

Expand Down
49 changes: 49 additions & 0 deletions lib/busi_api_web/controllers/colaborator_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule BusiApiWeb.ColaboratorController do
use BusiApiWeb, :controller
alias BusiApi.Directory
alias BusiApi.Directory.Colaborator

action_fallback BusiApiWeb.FallbackController

def index(conn, _params) do
colaborators = Directory.list_collaborators()
|>IO.inspect()
render(conn, "index.json", colaborators: colaborators)
end

def create(conn, %{"colaborator" => colaborator_params}) do
with {:ok, %Colaborator{} = colaborator} <- Directory.create_collaborator(colaborator_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.colaborator_path(conn, :show, colaborator))
|> render("colaborator.json", colaborator: colaborator)
end
end

def show(conn, %{"id" => id}) do
colaborator = Directory.get_collaborator!(id)
render(conn, "show.json", colaborator: colaborator)
end

def update(conn, %{"id" => id, "colaborator" => colaborator_params}) do
colaborator = Directory.get_collaborator!(id)
with {:ok, %Colaborator{} = colaborator} <- Directory.update_collaborator(colaborator, colaborator_params) do
render(conn, "show.json", colaborator: colaborator)
end
end

def delete(conn, %{"id" => id}) do
colaborator = Directory.get_collaborator!(id)
with {:ok, %Colaborator{}} <- Directory.delete_collaborator(colaborator) do
send_resp(conn, :no_content, "")
end
end

def get_businesess_collaborators(conn, %{"id" => id, "year" => year}) do
colaborators = Directory.get_businesess_collaborators(id, year)
|>IO.inspect()
render(conn, "index.json", colaborators: colaborators)
end


end
3 changes: 3 additions & 0 deletions lib/busi_api_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ defmodule BusiApiWeb.Router do
scope "/api", BusiApiWeb do
pipe_through :api
resources "/businesses", BusinessController, except: [:new, :edit]
resources "/collaborators", ColaboratorController, except: [:new, :edit]
get "/get_collaborators_businesess_by_year/:id/:year", ColaboratorController, :get_businesess_collaborators
end


# Enables LiveDashboard only for development
#
# If you want to use the LiveDashboard in production, you should put
Expand Down
22 changes: 19 additions & 3 deletions lib/busi_api_web/views/business_view.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
defmodule BusiApiWeb.BusinessView do
use BusiApiWeb, :view
alias BusiApiWeb.BusinessView
alias BusiApiWeb.ColaboratorView

def render("index.json", %{businesses: businesses}) do
%{data: render_many(businesses, BusinessView, "business.json")}
%{data: render_many(businesses, BusinessView, "business_assoc.json")}
end

def render("show.json", %{business: business}) do
%{data: render_one(business, BusinessView, "business.json")}
%{data: render_one(business, BusinessView, "business_assoc.json")}
end

def render("business_assoc.json", %{business: business}) do
%{
id: business.id,
name: business.name,
description: business.description,
tag: business.tag,
date: NaiveDateTime.to_string(business.inserted_at),
year: business.year,
collaborators:
%{
data: render_many(business.colaborators, ColaboratorView, "colaborator.json")
}
}
end
def render("business.json", %{business: business}) do
%{
id: business.id,
name: business.name,
description: business.description,
tag: business.tag,
date: NaiveDateTime.to_string(business.inserted_at)
date: NaiveDateTime.to_string(business.inserted_at),
year: business.year
}
end
end
37 changes: 37 additions & 0 deletions lib/busi_api_web/views/colaborator_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule BusiApiWeb.ColaboratorView do
use BusiApiWeb, :view
alias BusiApiWeb.ColaboratorView

def render("index.json", %{colaborators: colaborators}) do
%{data: render_many(colaborators, ColaboratorView, "colaborator_assoc.json")}
end

def render("show.json", %{colaborator: colaborator}) do
%{data: render_one(colaborator, ColaboratorView, "colaborator_assoc.json")}
end

def render("colaborator_assoc.json", %{colaborator: colaborator}) do
%{
id: colaborator.id,
name: colaborator.name,
lastname: colaborator.lastname,
email: colaborator.email,
business:
%{
id: colaborator.business.id,
name: colaborator.business.name,
description: colaborator.business.description,
tag: colaborator.business.tag,
year: colaborator.business.year
}
}
end
def render("colaborator.json", %{colaborator: colaborator}) do
%{
id: colaborator.id,
name: colaborator.name,
lastname: colaborator.lastname,
email: colaborator.email
}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule BusiApi.Repo.Migrations.AddYearToBusinesses do
use Ecto.Migration

def change do
alter table(:businesses) do
add :year, :integer
end
end
end
14 changes: 14 additions & 0 deletions priv/repo/migrations/20200723192526_create_colaborators.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule BusiApi.Repo.Migrations.CreateColaborators do
use Ecto.Migration

def change do
create table(:colaborators) do
add :name, :string
add :lastname, :string
add :email, :string
add :business_id, references(:businesses)
timestamps()
end

end
end
12 changes: 9 additions & 3 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# and so on) as they will fail if something goes wrong.
alias BusiApi.Repo
alias BusiApi.Directory.Business
Repo.insert! %Business{name: "Company 1", description: "Short description ...", tag: "IT, Software"}
Repo.insert! %Business{name: "Company 2", description: "Short description ...", tag: "Marketing"}
Repo.insert! %Business{name: "Company 3", description: "Short description ...", tag: "Accounting"}

Repo.insert! %Business{name: "Company 1", description: "Short description ...", tag: "IT, Software", year: 2000 }
Repo.insert! %Business{name: "Company 2", description: "Short description ...", tag: "Marketing", year: 2000 }
Repo.insert! %Business{name: "Company 3", description: "Short description ...", tag: "Accounting", year: 2000 }

alias BusiApi.Directory.Colaborator
Repo.insert! %Colaborator{name: "Jhon", lastname: "Doe", email: "some@gmail.com", business_id: 1 }
Repo.insert! %Colaborator{name: "Jane", lastname: "Dow", email: "some@gmail.com", business_id: 2 }
Repo.insert! %Colaborator{name: "Some", lastname: "Other", email: "some@gmail.com", business_id: 3 }