From 89a8e7973f5a1e1583ea68542a17adeef7deec1f Mon Sep 17 00:00:00 2001 From: Maria Gabriela Moreno Exposito Date: Thu, 23 Jul 2020 15:10:16 -0400 Subject: [PATCH 1/6] Added new field Year to business model Added Migration of the field Year Added field Year to Json response --- config/dev.exs | 2 +- lib/busi_api/directory/business.ex | 7 ++++--- lib/busi_api_web/views/business_view.ex | 3 ++- .../migrations/20200723184303_add_year_to_businesses.exs | 9 +++++++++ priv/repo/seeds.exs | 6 +++--- 5 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 priv/repo/migrations/20200723184303_add_year_to_businesses.exs diff --git a/config/dev.exs b/config/dev.exs index 7b78fb5..8af2cdc 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -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, diff --git a/lib/busi_api/directory/business.ex b/lib/busi_api/directory/business.ex index b2c0032..8eb1c3b 100644 --- a/lib/busi_api/directory/business.ex +++ b/lib/busi_api/directory/business.ex @@ -4,9 +4,10 @@ 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, Business.Colaborators timestamps() end diff --git a/lib/busi_api_web/views/business_view.ex b/lib/busi_api_web/views/business_view.ex index 1bf0bd9..e56098b 100644 --- a/lib/busi_api_web/views/business_view.ex +++ b/lib/busi_api_web/views/business_view.ex @@ -16,7 +16,8 @@ defmodule BusiApiWeb.BusinessView do 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 diff --git a/priv/repo/migrations/20200723184303_add_year_to_businesses.exs b/priv/repo/migrations/20200723184303_add_year_to_businesses.exs new file mode 100644 index 0000000..d7203d0 --- /dev/null +++ b/priv/repo/migrations/20200723184303_add_year_to_businesses.exs @@ -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 diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 8343511..282c592 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -11,6 +11,6 @@ # 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"} \ No newline at end of file +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 } \ No newline at end of file From 6c7ff0ce9ed89b9ded2c55423f8078eeec1d625d Mon Sep 17 00:00:00 2001 From: Maria Gabriela Moreno Exposito Date: Thu, 23 Jul 2020 17:57:49 -0400 Subject: [PATCH 2/6] Colaboratos Schema Added - Added Migration and schema - Updated the Seeds - Added Controller and View - Updated the route - Added Service to get all collaborators from a business by id and year (of the business) - Updated the has_many relation on the businesses schema. --- lib/busi_api/directory.ex | 20 ++++++++++++++++++ lib/busi_api/directory/business.ex | 2 +- lib/busi_api/directory/colaborator.ex | 19 +++++++++++++++++ .../controllers/business_controller.ex | 6 ++++++ .../controllers/colaborator_controller.ex | 12 +++++++++++ lib/busi_api_web/router.ex | 2 ++ lib/busi_api_web/views/colaborator_view.ex | 21 +++++++++++++++++++ .../20200723192526_create_colaborators.exs | 14 +++++++++++++ priv/repo/seeds.exs | 8 ++++++- 9 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 lib/busi_api/directory/colaborator.ex create mode 100644 lib/busi_api_web/controllers/colaborator_controller.ex create mode 100644 lib/busi_api_web/views/colaborator_view.ex create mode 100644 priv/repo/migrations/20200723192526_create_colaborators.exs diff --git a/lib/busi_api/directory.ex b/lib/busi_api/directory.ex index 3ef704d..3ec9df9 100644 --- a/lib/busi_api/directory.ex +++ b/lib/busi_api/directory.ex @@ -7,6 +7,7 @@ defmodule BusiApi.Directory do alias BusiApi.Repo alias BusiApi.Directory.Business + alias BusiApi.Directory.Colaborator @doc """ Returns the list of businesses. @@ -21,6 +22,15 @@ defmodule BusiApi.Directory do Repo.all(Business) end + 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 + @doc """ Gets a single business. @@ -101,4 +111,14 @@ defmodule BusiApi.Directory do def change_business(%Business{} = business, attrs \\ %{}) do Business.changeset(business, attrs) end + + 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 + end diff --git a/lib/busi_api/directory/business.ex b/lib/busi_api/directory/business.ex index 8eb1c3b..7f2aba3 100644 --- a/lib/busi_api/directory/business.ex +++ b/lib/busi_api/directory/business.ex @@ -7,7 +7,7 @@ defmodule BusiApi.Directory.Business do field :name, :string field :tag, :string field :year, :integer - #has_many :colaborators, Business.Colaborators + has_many :colaborators, BusiApi.Directory.Colaborator timestamps() end diff --git a/lib/busi_api/directory/colaborator.ex b/lib/busi_api/directory/colaborator.ex new file mode 100644 index 0000000..ac7a06f --- /dev/null +++ b/lib/busi_api/directory/colaborator.ex @@ -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 + + @doc false + def changeset(colaborator, attrs) do + colaborator + |> cast(attrs, [:name, :lastname, :email]) + |> validate_required([:name, :lastname, :email]) + end +end diff --git a/lib/busi_api_web/controllers/business_controller.ex b/lib/busi_api_web/controllers/business_controller.ex index 759b29d..2cbcdbb 100644 --- a/lib/busi_api_web/controllers/business_controller.ex +++ b/lib/busi_api_web/controllers/business_controller.ex @@ -3,6 +3,7 @@ defmodule BusiApiWeb.BusinessController do alias BusiApi.Directory alias BusiApi.Directory.Business + alias BusiApi.Directory.Colaborator action_fallback BusiApiWeb.FallbackController @@ -40,4 +41,9 @@ defmodule BusiApiWeb.BusinessController 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) + render(conn, "colaborator.json", colaborators: colaborators) + end end diff --git a/lib/busi_api_web/controllers/colaborator_controller.ex b/lib/busi_api_web/controllers/colaborator_controller.ex new file mode 100644 index 0000000..0012957 --- /dev/null +++ b/lib/busi_api_web/controllers/colaborator_controller.ex @@ -0,0 +1,12 @@ +defmodule BusiApiWeb.ColaboratorController do + use BusiApiWeb, :controller + alias BusiApi.Directory + alias BusiApi.Directory.Colaborator + + action_fallback BusiApiWeb.FallbackController + + def get_businesess_collaborators(conn, %{"id" => id, "year" => year}) do + colaborators = Directory.get_businesess_collaborators(id, year) + render(conn, "index.json", colaborators: colaborators) + end + end diff --git a/lib/busi_api_web/router.ex b/lib/busi_api_web/router.ex index 6b9a703..be9e7cb 100644 --- a/lib/busi_api_web/router.ex +++ b/lib/busi_api_web/router.ex @@ -21,8 +21,10 @@ defmodule BusiApiWeb.Router do scope "/api", BusiApiWeb do pipe_through :api resources "/businesses", BusinessController, except: [:new, :edit] + get "/get_collaborators_businesess_by_year", ColaboratorController, :get_businesess_collaborators end + # Enables LiveDashboard only for development # # If you want to use the LiveDashboard in production, you should put diff --git a/lib/busi_api_web/views/colaborator_view.ex b/lib/busi_api_web/views/colaborator_view.ex new file mode 100644 index 0000000..389aa43 --- /dev/null +++ b/lib/busi_api_web/views/colaborator_view.ex @@ -0,0 +1,21 @@ +defmodule BusiApiWeb.ColaboratorView do + use BusiApiWeb, :view + alias BusiApiWeb.ColaboratorView + + def render("index.json", %{colaborators: colaborators}) do + %{data: render_many(colaborators, ColaboratorView, "colaborator.json")} + end + + def render("show.json", %{colaborator: colaborator}) do + %{data: render_one(colaborator, ColaboratorView, "colaborator.json")} + end + + def render("colaborator.json", %{colaborator: colaborator}) do + %{ + id: colaborator.id, + name: colaborator.name, + lastname: colaborator.lastname, + email: colaborator.email + } + end + end \ No newline at end of file diff --git a/priv/repo/migrations/20200723192526_create_colaborators.exs b/priv/repo/migrations/20200723192526_create_colaborators.exs new file mode 100644 index 0000000..ba6c861 --- /dev/null +++ b/priv/repo/migrations/20200723192526_create_colaborators.exs @@ -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 diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 282c592..67a60d8 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -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", 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 } \ No newline at end of file +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: 5 } +Repo.insert! %Colaborator{name: "Jane", lastname: "Dow", email: "some@gmail.com", business_id: 6 } +Repo.insert! %Colaborator{name: "Some", lastname: "Other", email: "some@gmail.com", business_id: 7 } From 8213320045924d594cffb12b0c190bd5728153f3 Mon Sep 17 00:00:00 2001 From: Maria Gabriela Moreno Exposito Date: Thu, 23 Jul 2020 18:26:48 -0400 Subject: [PATCH 3/6] Readme Updated --- README.md | 120 +++++++++++++++++++++++++++++++++++++++++++- priv/repo/seeds.exs | 6 +-- 2 files changed, 122 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ab5042f..7023765 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,128 @@ To start your Phoenix server: * Install Node.js dependencies with `npm install` inside the `assets` directory * Start Phoenix endpoint with `mix phx.server` -Now you can visit [`localhost:4000`](http://localhost:4000) from your browser. +- [Business](#business) + - [Get All Business](#http://localhost:4001/api/businesses-get) + - [Add New Business](#http://localhost:4001/api/businesses-post) +- [Collaborators](#collaborators) + - [Get All Colaborators From a Business and a Year](#http://localhost:4001/api/get_collaborators_businesess_by_yearidyear-get) + +### Business + +#### http://localhost:4001/api/businesses (GET) + +Get All Businesses Registered. + +**REQUEST** +Headers: `Content-Type: application/json` + +**RESPONSE** +``` +{ + "data": [ + { + "date": "2020-07-23 20:07:36", + "description": "Short description ...", + "id": 5, + "name": "Company 1", + "tag": "IT, Software", + "year": 2000 + }, + { + "date": "2020-07-23 20:07:36", + "description": "Short description ...", + "id": 6, + "name": "Company 2", + "tag": "Marketing", + "year": 2000 + }, + { + "date": "2020-07-23 20:07:36", + "description": "Short description ...", + "id": 7, + "name": "Company 3", + "tag": "Accounting", + "year": 2000 + } + ] +} +``` + +--- + +#### http://localhost:4001/api/businesses (POST) + +Get All Businesses Registered. + +**REQUEST** +Headers: `Content-Type: application/json` +``` +{ + "business": + { + "name": "Yet another company", + "description": "Another short description!", + "tag": "A Tag" + } +} +``` +**RESPONSE** +``` +{ + "data": { + "date": "2020-07-23 22:09:43", + "description": "Another short description!", + "id": 11, + "name": "Yet another company", + "tag": "A Tag", + "year": null + } +} +``` + +--- + + +Now you can visit [`localhost:4001`](http://localhost:4001) from your browser. Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html). + + + + + + + + + + +### Collaborators +#### http://localhost:4001/api/get_collaborators_businesess_by_year/:id/:year (GET) + +Get All Collaborators that belongs to a specific Business in a specific Year . + +**REQUEST** +Headers: `Content-Type: application/json` +Path Variables: id = 1, year = 2000 + +**RESPONSE** +``` +{ + "data": [ + { + "email": "some@gmail.com", + "id": 1, + "lastname": "Doe", + "name": "Jhon" + } + ] +} +``` + +--- + + ## Learn more * Official website: https://www.phoenixframework.org/ diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 67a60d8..f96dca8 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -17,6 +17,6 @@ Repo.insert! %Business{name: "Company 2", description: "Short description ...", 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: 5 } -Repo.insert! %Colaborator{name: "Jane", lastname: "Dow", email: "some@gmail.com", business_id: 6 } -Repo.insert! %Colaborator{name: "Some", lastname: "Other", email: "some@gmail.com", business_id: 7 } +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 } From aabf10a919d3b1f244863b9e2c112a6458eb7a4c Mon Sep 17 00:00:00 2001 From: Maria Gabriela Moreno Exposito Date: Fri, 24 Jul 2020 13:07:51 -0400 Subject: [PATCH 4/6] Added to Json response the related data from a Model - Modification of the services, to preload data related - Added Service, getAll Collaborators - Modification of the service, getAll Businesses --- lib/busi_api/directory.ex | 20 +++++++++++-------- lib/busi_api/directory/business.ex | 4 ++++ .../controllers/business_controller.ex | 11 ++++------ .../controllers/colaborator_controller.ex | 10 +++++++++- lib/busi_api_web/router.ex | 3 ++- lib/busi_api_web/views/business_view.ex | 7 ++++++- lib/busi_api_web/views/colaborator_view.ex | 16 +++++++++++++++ 7 files changed, 53 insertions(+), 18 deletions(-) diff --git a/lib/busi_api/directory.ex b/lib/busi_api/directory.ex index 3ec9df9..0955c3e 100644 --- a/lib/busi_api/directory.ex +++ b/lib/busi_api/directory.ex @@ -9,6 +9,7 @@ defmodule BusiApi.Directory do alias BusiApi.Directory.Business alias BusiApi.Directory.Colaborator + @spec list_businesses :: any @doc """ Returns the list of businesses. @@ -19,18 +20,13 @@ defmodule BusiApi.Directory do """ def list_businesses do - Repo.all(Business) - end - - 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 + join: c in assoc(b, :colaborators), + preload: [colaborators: c] Repo.all(query) end + @doc """ Gets a single business. @@ -112,6 +108,7 @@ defmodule BusiApi.Directory 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, @@ -121,4 +118,11 @@ defmodule BusiApi.Directory do Repo.all(query) end + def list_collaborators do + query = from c in Colaborator, + join: b in assoc(c, :business), + preload: [business: b] + Repo.all(query) + end + end diff --git a/lib/busi_api/directory/business.ex b/lib/busi_api/directory/business.ex index 7f2aba3..f57c752 100644 --- a/lib/busi_api/directory/business.ex +++ b/lib/busi_api/directory/business.ex @@ -11,6 +11,10 @@ defmodule BusiApi.Directory.Business do timestamps() end + @spec changeset( + {map, map} | %{:__struct__ => atom | %{__changeset__: map}, optional(atom) => any}, + :invalid | %{optional(:__struct__) => none, optional(atom | binary) => any} + ) :: Ecto.Changeset.t() @doc false def changeset(business, attrs) do business diff --git a/lib/busi_api_web/controllers/business_controller.ex b/lib/busi_api_web/controllers/business_controller.ex index 2cbcdbb..8b0103d 100644 --- a/lib/busi_api_web/controllers/business_controller.ex +++ b/lib/busi_api_web/controllers/business_controller.ex @@ -1,14 +1,14 @@ defmodule BusiApiWeb.BusinessController do use BusiApiWeb, :controller - alias BusiApi.Directory alias BusiApi.Directory.Business - alias BusiApi.Directory.Colaborator + #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 @@ -26,6 +26,7 @@ defmodule BusiApiWeb.BusinessController do render(conn, "show.json", business: business) end + @spec update(any, map) :: any def update(conn, %{"id" => id, "business" => business_params}) do business = Directory.get_business!(id) @@ -34,6 +35,7 @@ defmodule BusiApiWeb.BusinessController do end end + @spec delete(any, map) :: any def delete(conn, %{"id" => id}) do business = Directory.get_business!(id) @@ -41,9 +43,4 @@ defmodule BusiApiWeb.BusinessController 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) - render(conn, "colaborator.json", colaborators: colaborators) - end end diff --git a/lib/busi_api_web/controllers/colaborator_controller.ex b/lib/busi_api_web/controllers/colaborator_controller.ex index 0012957..ef18e3f 100644 --- a/lib/busi_api_web/controllers/colaborator_controller.ex +++ b/lib/busi_api_web/controllers/colaborator_controller.ex @@ -1,12 +1,20 @@ defmodule BusiApiWeb.ColaboratorController do use BusiApiWeb, :controller alias BusiApi.Directory - alias BusiApi.Directory.Colaborator + #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 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 diff --git a/lib/busi_api_web/router.ex b/lib/busi_api_web/router.ex index be9e7cb..17a4a81 100644 --- a/lib/busi_api_web/router.ex +++ b/lib/busi_api_web/router.ex @@ -21,7 +21,8 @@ defmodule BusiApiWeb.Router do scope "/api", BusiApiWeb do pipe_through :api resources "/businesses", BusinessController, except: [:new, :edit] - get "/get_collaborators_businesess_by_year", ColaboratorController, :get_businesess_collaborators + resources "/collaborators", ColaboratorController, except: [:new, :edit] + get "/get_collaborators_businesess_by_year/:id/:year", ColaboratorController, :get_businesess_collaborators end diff --git a/lib/busi_api_web/views/business_view.ex b/lib/busi_api_web/views/business_view.ex index e56098b..759c6ea 100644 --- a/lib/busi_api_web/views/business_view.ex +++ b/lib/busi_api_web/views/business_view.ex @@ -1,6 +1,7 @@ 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")} @@ -17,7 +18,11 @@ defmodule BusiApiWeb.BusinessView do description: business.description, tag: business.tag, date: NaiveDateTime.to_string(business.inserted_at), - year: business.year + year: business.year, + collaborators: + %{ + data: render_many(business.colaborators, ColaboratorView, "colaboratorFromBusiness.json") + } } end end diff --git a/lib/busi_api_web/views/colaborator_view.ex b/lib/busi_api_web/views/colaborator_view.ex index 389aa43..1ed03ab 100644 --- a/lib/busi_api_web/views/colaborator_view.ex +++ b/lib/busi_api_web/views/colaborator_view.ex @@ -11,6 +11,22 @@ defmodule BusiApiWeb.ColaboratorView do end def render("colaborator.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("colaboratorFromBusiness.json", %{colaborator: colaborator}) do %{ id: colaborator.id, name: colaborator.name, From 20159cbf126fe41c14e313a42e39622756b5cb2e Mon Sep 17 00:00:00 2001 From: Maria Gabriela Moreno Exposito Date: Sat, 25 Jul 2020 20:32:34 -0400 Subject: [PATCH 5/6] Crud Collaborators -Added assoc_constraint to colaborator schema -Updated Json responses --- lib/busi_api/directory.ex | 47 +++++++++++++------ lib/busi_api/directory/business.ex | 5 -- lib/busi_api/directory/colaborator.ex | 6 +-- .../controllers/business_controller.ex | 2 - .../controllers/colaborator_controller.ex | 31 +++++++++++- lib/busi_api_web/views/business_view.ex | 2 +- lib/busi_api_web/views/colaborator_view.ex | 8 ++-- 7 files changed, 71 insertions(+), 30 deletions(-) diff --git a/lib/busi_api/directory.ex b/lib/busi_api/directory.ex index 0955c3e..61cd788 100644 --- a/lib/busi_api/directory.ex +++ b/lib/busi_api/directory.ex @@ -7,9 +7,7 @@ defmodule BusiApi.Directory do alias BusiApi.Repo alias BusiApi.Directory.Business - alias BusiApi.Directory.Colaborator - @spec list_businesses :: any @doc """ Returns the list of businesses. @@ -19,12 +17,14 @@ defmodule BusiApi.Directory do [%Business{}, ...] """ - 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 + # 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 """ @@ -41,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. @@ -118,11 +118,30 @@ defmodule BusiApi.Directory do Repo.all(query) end - def list_collaborators do - query = from c in Colaborator, - join: b in assoc(c, :business), - preload: [business: b] - Repo.all(query) + 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 diff --git a/lib/busi_api/directory/business.ex b/lib/busi_api/directory/business.ex index f57c752..a859753 100644 --- a/lib/busi_api/directory/business.ex +++ b/lib/busi_api/directory/business.ex @@ -11,11 +11,6 @@ defmodule BusiApi.Directory.Business do timestamps() end - @spec changeset( - {map, map} | %{:__struct__ => atom | %{__changeset__: map}, optional(atom) => any}, - :invalid | %{optional(:__struct__) => none, optional(atom | binary) => any} - ) :: Ecto.Changeset.t() - @doc false def changeset(business, attrs) do business |> cast(attrs, [:name, :description, :tag]) diff --git a/lib/busi_api/directory/colaborator.ex b/lib/busi_api/directory/colaborator.ex index ac7a06f..c1604a5 100644 --- a/lib/busi_api/directory/colaborator.ex +++ b/lib/busi_api/directory/colaborator.ex @@ -10,10 +10,10 @@ defmodule BusiApi.Directory.Colaborator do timestamps() end - @doc false def changeset(colaborator, attrs) do colaborator - |> cast(attrs, [:name, :lastname, :email]) - |> validate_required([:name, :lastname, :email]) + |> cast(attrs, [:name, :lastname, :email, :business_id]) + |> validate_required([:name, :lastname, :email, :business_id]) + |> assoc_constraint(:business) end end diff --git a/lib/busi_api_web/controllers/business_controller.ex b/lib/busi_api_web/controllers/business_controller.ex index 8b0103d..da83a68 100644 --- a/lib/busi_api_web/controllers/business_controller.ex +++ b/lib/busi_api_web/controllers/business_controller.ex @@ -26,7 +26,6 @@ defmodule BusiApiWeb.BusinessController do render(conn, "show.json", business: business) end - @spec update(any, map) :: any def update(conn, %{"id" => id, "business" => business_params}) do business = Directory.get_business!(id) @@ -35,7 +34,6 @@ defmodule BusiApiWeb.BusinessController do end end - @spec delete(any, map) :: any def delete(conn, %{"id" => id}) do business = Directory.get_business!(id) diff --git a/lib/busi_api_web/controllers/colaborator_controller.ex b/lib/busi_api_web/controllers/colaborator_controller.ex index ef18e3f..792d923 100644 --- a/lib/busi_api_web/controllers/colaborator_controller.ex +++ b/lib/busi_api_web/controllers/colaborator_controller.ex @@ -1,7 +1,7 @@ defmodule BusiApiWeb.ColaboratorController do use BusiApiWeb, :controller alias BusiApi.Directory - #alias BusiApi.Directory.Colaborator + alias BusiApi.Directory.Colaborator action_fallback BusiApiWeb.FallbackController @@ -11,10 +11,39 @@ defmodule BusiApiWeb.ColaboratorController do 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 diff --git a/lib/busi_api_web/views/business_view.ex b/lib/busi_api_web/views/business_view.ex index 759c6ea..71bf77d 100644 --- a/lib/busi_api_web/views/business_view.ex +++ b/lib/busi_api_web/views/business_view.ex @@ -21,7 +21,7 @@ defmodule BusiApiWeb.BusinessView do year: business.year, collaborators: %{ - data: render_many(business.colaborators, ColaboratorView, "colaboratorFromBusiness.json") + data: render_many(business.colaborators, ColaboratorView, "colaborator.json") } } end diff --git a/lib/busi_api_web/views/colaborator_view.ex b/lib/busi_api_web/views/colaborator_view.ex index 1ed03ab..8c9bd46 100644 --- a/lib/busi_api_web/views/colaborator_view.ex +++ b/lib/busi_api_web/views/colaborator_view.ex @@ -3,14 +3,14 @@ defmodule BusiApiWeb.ColaboratorView do alias BusiApiWeb.ColaboratorView def render("index.json", %{colaborators: colaborators}) do - %{data: render_many(colaborators, ColaboratorView, "colaborator.json")} + %{data: render_many(colaborators, ColaboratorView, "colaborator_assoc.json")} end def render("show.json", %{colaborator: colaborator}) do - %{data: render_one(colaborator, ColaboratorView, "colaborator.json")} + %{data: render_one(colaborator, ColaboratorView, "colaborator_assoc.json")} end - def render("colaborator.json", %{colaborator: colaborator}) do + def render("colaborator_assoc.json", %{colaborator: colaborator}) do %{ id: colaborator.id, name: colaborator.name, @@ -26,7 +26,7 @@ defmodule BusiApiWeb.ColaboratorView do } } end - def render("colaboratorFromBusiness.json", %{colaborator: colaborator}) do + def render("colaborator.json", %{colaborator: colaborator}) do %{ id: colaborator.id, name: colaborator.name, From deab456887e139a1d7bb518b85f48b0a4faa13ab Mon Sep 17 00:00:00 2001 From: Maria Gabriela Moreno Exposito Date: Sat, 25 Jul 2020 21:22:06 -0400 Subject: [PATCH 6/6] Added Year to cast methos on Businesses Schema Readme Updated --- README.md | 318 ++++++++++++++++-- lib/busi_api/directory/business.ex | 4 +- .../controllers/business_controller.ex | 2 +- lib/busi_api_web/views/business_view.ex | 16 +- 4 files changed, 312 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 7023765..bd8dca1 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,21 @@ To start your Phoenix server: * Install Node.js dependencies with `npm install` inside the `assets` directory * Start Phoenix endpoint with `mix phx.server` -- [Business](#business) +- [Businesses](#businesses) - [Get All Business](#http://localhost:4001/api/businesses-get) - - [Add New Business](#http://localhost:4001/api/businesses-post) + - [Get a Business](#http://localhost:4001/api/businessesid-get) + - [Add a Business](#http://localhost:4001/api/businesses-post) + - [Update a Business](#http://localhost:4001/api/businessesid-put) + - [Delete a Business](#http://localhost:4001/api/businessesid-delete) - [Collaborators](#collaborators) + - [Get All Collaborators](#http://localhost:4001/api/collaborators-get) + - [Get one Collaborator](#http://localhost:4001/api/collaboratorsid-get) + - [Add a Collaborator](#http://localhost:4001/api/collaborators-post) + - [Update a Collaborator](#http://localhost:4001/api/collaboratorsid-put) + - [Delete a Collaborator](#http://localhost:4001/api/collaboratorsid-delete) - [Get All Colaborators From a Business and a Year](#http://localhost:4001/api/get_collaborators_businesess_by_yearidyear-get) -### Business +### Businesses #### http://localhost:4001/api/businesses (GET) @@ -27,25 +35,55 @@ Headers: `Content-Type: application/json` { "data": [ { - "date": "2020-07-23 20:07:36", + "collaborators": { + "data": [ + { + "email": "some@gmail.com", + "id": 1, + "lastname": "Doe", + "name": "Jhon" + } + ] + }, + "date": "2020-07-26 00:39:09", "description": "Short description ...", - "id": 5, + "id": 1, "name": "Company 1", "tag": "IT, Software", "year": 2000 }, { - "date": "2020-07-23 20:07:36", + "collaborators": { + "data": [ + { + "email": "some@gmail.com", + "id": 2, + "lastname": "Dow", + "name": "Jane" + } + ] + }, + "date": "2020-07-26 00:39:09", "description": "Short description ...", - "id": 6, + "id": 2, "name": "Company 2", "tag": "Marketing", "year": 2000 }, { - "date": "2020-07-23 20:07:36", + "collaborators": { + "data": [ + { + "email": "some@gmail.com", + "id": 3, + "lastname": "Other", + "name": "Some" + } + ] + }, + "date": "2020-07-26 00:39:09", "description": "Short description ...", - "id": 7, + "id": 3, "name": "Company 3", "tag": "Accounting", "year": 2000 @@ -55,20 +93,81 @@ Headers: `Content-Type: application/json` ``` --- +#### http://localhost:4001/api/businesses/:id (GET) + +Get one Business Registered. + +**REQUEST** +Headers: `Content-Type: application/json` +Path Variables: `id` = `1` +**RESPONSE** +``` +{ + "data": { + "collaborators": { + "data": [ + { + "email": "some@gmail.com", + "id": 1, + "lastname": "Doe", + "name": "Jhon" + } + ] + }, + "date": "2020-07-26 00:39:09", + "description": "Short description ...", + "id": 1, + "name": "Name Updated", + "tag": "IT, Software", + "year": 2000 + } +} +``` + +--- #### http://localhost:4001/api/businesses (POST) -Get All Businesses Registered. +Store a new Business. + +**REQUEST** +Headers: `Content-Type: application/json` +``` +{ + "business": + { + "name": "Yet another new company", + "description": "Another new short description!", + "tag": "A Tag", + "year": "2000" + } +} +``` +**RESPONSE** +``` +{ + "date": "2020-07-26 00:51:36", + "description": "Another new short description!", + "id": 4, + "name": "Yet another new company", + "tag": "A Tag", + "year": 2000 +} +``` + +--- +#### http://localhost:4001/api/businesses/:id (PUT) + +Update a Business **REQUEST** Headers: `Content-Type: application/json` +Path Variables: `id` = `1` ``` { "business": { - "name": "Yet another company", - "description": "Another short description!", - "tag": "A Tag" + "name": "Name Updated" } } ``` @@ -76,34 +175,204 @@ Headers: `Content-Type: application/json` ``` { "data": { - "date": "2020-07-23 22:09:43", - "description": "Another short description!", - "id": 11, - "name": "Yet another company", - "tag": "A Tag", - "year": null + "collaborators": { + "data": [ + { + "email": "some@gmail.com", + "id": 1, + "lastname": "Doe", + "name": "Jhon" + } + ] + }, + "date": "2020-07-26 00:39:09", + "description": "Short description ...", + "id": 1, + "name": "Name Updated", + "tag": "IT, Software", + "year": 2000 } } ``` --- +#### http://localhost:4001/api/businesses/:id (DELETE) + +Delete one Business. + +**REQUEST** +Headers: `x-access-token` +Path Variables: id = 2 + +**RESPONSE** +``` + If ok no Response + +``` + +--- +### Collaborators +#### http://localhost:4001/api/collaborators (GET) + +Get All Collaborators Registered. + +**REQUEST** +Headers: `Content-Type: application/json` + +**RESPONSE** +``` +{ + "data": [ + { + "business": { + "description": "Short description ...", + "id": 1, + "name": "Name Updated", + "tag": "IT, Software", + "year": 2000 + }, + "email": "some@gmail.com", + "id": 1, + "lastname": "Doe", + "name": "Jhon" + }, + { + "business": { + "description": "Short description ...", + "id": 2, + "name": "Company 2", + "tag": "Marketing", + "year": 2000 + }, + "email": "some@gmail.com", + "id": 2, + "lastname": "Dow", + "name": "Jane" + }, + { + "business": { + "description": "Short description ...", + "id": 3, + "name": "Company 3", + "tag": "Accounting", + "year": 2000 + }, + "email": "some@gmail.com", + "id": 3, + "lastname": "Other", + "name": "Some" + } + ] +} +``` + +--- +#### http://localhost:4001/api/collaborators/:id (GET) +Get one Collaborator Registered. -Now you can visit [`localhost:4001`](http://localhost:4001) from your browser. +**REQUEST** +Headers: `Content-Type: application/json` +Path Variables: `id` = `1` -Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html). +**RESPONSE** +``` +{ + "data": { + "business": { + "description": "Short description ...", + "id": 1, + "name": "Name Updated", + "tag": "IT, Software", + "year": 2000 + }, + "email": "some@gmail.com", + "id": 1, + "lastname": "Doe", + "name": "Jhon" + } +} +``` +--- +#### http://localhost:4001/api/collaborators (POST) +Store a new Collaborator. +**REQUEST** +Headers: `Content-Type: application/json` +``` +{ + "colaborator": + { + "name": "Some Other Collaborator Name", + "lastname": "Some Other Collaborator LastName", + "email": "some_email@some.com", + "business_id": 2 + } +} +``` +**RESPONSE** +``` +{ + "email": "some_email@some.com", + "id": 4, + "lastname": "Some Other Collaborator LastName", + "name": "Some Other Collaborator Name" +} +``` +--- +#### http://localhost:4001/api/collaborators/:id (PUT) +Update a Collaborator +**REQUEST** +Headers: `Content-Type: application/json` +Path Variables: `id` = `1` +``` +{ + "colaborator": + { + "name": "Name Updated" + } +} +``` +**RESPONSE** +``` +{ + "data": { + "business": { + "description": "Short description ...", + "id": 1, + "name": "Name Updated", + "tag": "IT, Software", + "year": 2000 + }, + "email": "some@gmail.com", + "id": 1, + "lastname": "Doe", + "name": "Name Updated" + } +} +``` + +--- +#### http://localhost:4001/api/collaborators/:id (DELETE) +Delete one Business. +**REQUEST** +Headers: `x-access-token` +Path Variables: id = 2 +**RESPONSE** +``` + If ok no Response +``` +--- -### Collaborators #### http://localhost:4001/api/get_collaborators_businesess_by_year/:id/:year (GET) Get All Collaborators that belongs to a specific Business in a specific Year . @@ -129,6 +398,11 @@ Path Variables: id = 1, year = 2000 --- + + + + + ## Learn more * Official website: https://www.phoenixframework.org/ diff --git a/lib/busi_api/directory/business.ex b/lib/busi_api/directory/business.ex index a859753..5182cef 100644 --- a/lib/busi_api/directory/business.ex +++ b/lib/busi_api/directory/business.ex @@ -13,7 +13,7 @@ defmodule BusiApi.Directory.Business do 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 diff --git a/lib/busi_api_web/controllers/business_controller.ex b/lib/busi_api_web/controllers/business_controller.ex index da83a68..63a1535 100644 --- a/lib/busi_api_web/controllers/business_controller.ex +++ b/lib/busi_api_web/controllers/business_controller.ex @@ -17,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 diff --git a/lib/busi_api_web/views/business_view.ex b/lib/busi_api_web/views/business_view.ex index 71bf77d..da1c70b 100644 --- a/lib/busi_api_web/views/business_view.ex +++ b/lib/busi_api_web/views/business_view.ex @@ -4,14 +4,14 @@ defmodule BusiApiWeb.BusinessView do 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.json", %{business: business}) do + def render("business_assoc.json", %{business: business}) do %{ id: business.id, name: business.name, @@ -25,4 +25,14 @@ defmodule BusiApiWeb.BusinessView do } } 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), + year: business.year + } + end end