Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add foreign key constraints, fix tests #45

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README_Lunchbot.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Below is the database model for Lunchbot displayed using mermaid.js. You can mod
int id
int user_id FK
int menu_id FK
int lunch_order_id FK
int office_lunch_order_id FK
}
ORDER_ITEMS {
int id
Expand Down
4 changes: 2 additions & 2 deletions lib/lunchbot/lunchbot_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ defmodule Lunchbot.LunchbotData do

if lunch_order_id do
from(o in Order,
where: o.lunch_order_id == ^lunch_order_id,
where: o.office_lunch_order_id == ^lunch_order_id,
preload: [
user: ^from(u in User, select: u.name),
order_items: [
Expand Down Expand Up @@ -1973,7 +1973,7 @@ defmodule Lunchbot.LunchbotData do
defconfig do
number(:user_id)
number(:menu_id)
number(:lunch_order_id)
number(:office_lunch_order_id)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/lunchbot/lunchbot_data/office_lunch_order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Lunchbot.LunchbotData.OfficeLunchOrder do
field :day, :date
belongs_to :menu, Lunchbot.LunchbotData.Menu
belongs_to :office, Lunchbot.LunchbotData.Office
has_many :orders, Lunchbot.LunchbotData.Order, foreign_key: :lunch_order_id
has_many :orders, Lunchbot.LunchbotData.Order, foreign_key: :office_lunch_order_id

timestamps()
end
Expand Down
6 changes: 3 additions & 3 deletions lib/lunchbot/lunchbot_data/order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Lunchbot.LunchbotData.Order do
alias Lunchbot.Repo

schema "orders" do
field :lunch_order_id, :integer
field :office_lunch_order_id, :integer
belongs_to :menu, Lunchbot.LunchbotData.Menu
belongs_to :user, Lunchbot.Accounts.User
has_many :order_items, Lunchbot.LunchbotData.OrderItem
Expand All @@ -16,8 +16,8 @@ defmodule Lunchbot.LunchbotData.Order do
@doc false
def changeset(order, attrs) do
order
|> cast(attrs, [:user_id, :menu_id, :lunch_order_id])
|> validate_required([:user_id, :menu_id, :lunch_order_id])
|> cast(attrs, [:user_id, :menu_id, :office_lunch_order_id])
|> validate_required([:user_id, :menu_id, :office_lunch_order_id])
end

def get_total(%Lunchbot.LunchbotData.Order{} = order) do
Expand Down
14 changes: 0 additions & 14 deletions lib/lunchbot_web/controllers/admin/order_item_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,6 @@ defmodule LunchbotWeb.Admin.OrderItemController do
render(conn, "edit.html", order_item: order_item, changeset: changeset)
end

def update(conn, %{"id" => id, "order_item" => order_item_params}) do
order_item = LunchbotData.get_order_item!(id)

case LunchbotData.update_order_item(order_item, order_item_params) do
{:ok, order_item} ->
conn
|> put_flash(:info, "Order item updated successfully.")
|> redirect(to: Routes.admin_order_item_path(conn, :show, order_item))

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", order_item: order_item, changeset: changeset)
end
end

def delete(conn, %{"id" => id}) do
order_item = LunchbotData.get_order_item!(id)
{:ok, _order_item} = LunchbotData.delete_order_item(order_item)
Expand Down
2 changes: 1 addition & 1 deletion lib/lunchbot_web/live/order_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ defmodule LunchbotWeb.OrderLive do
LunchbotData.create_order(%{
user_id: current_user_id,
menu_id: socket.assigns.todays_menu_id,
lunch_order_id: office_lunch_order_id
office_lunch_order_id: office_lunch_order_id
})
|> case do
{:ok, %LunchbotData.Order{id: id}} ->
Expand Down
14 changes: 7 additions & 7 deletions lib/lunchbot_web/templates/admin/order/form.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
<%= if @changeset.action do %>
<p class="torch-form-error">Oops, something went wrong! Please check the errors below.</p>
<% end %>

<div class="torch-form-group">
<%= label f, :user_id %>
<div class="torch-form-group-input">
<%= number_input f, :user_id %>
<%= error_tag f, :user_id %>
</div>
</div>

<div class="torch-form-group">
<%= label f, :menu_id %>
<div class="torch-form-group-input">
<%= number_input f, :menu_id %>
<%= error_tag f, :menu_id %>
</div>
</div>

<div class="torch-form-group">
<%= label f, :lunch_order_id %>
<%= label f, :office_lunch_order_id %>
<div class="torch-form-group-input">
<%= number_input f, :lunch_order_id %>
<%= error_tag f, :lunch_order_id %>
<%= number_input f, :office_lunch_order_id %>
<%= error_tag f, :office_lunch_order_id %>
</div>
</div>

<div class="torch-submit-form">
<%= submit "Submit", class: "torch-submit-button" %>
</div>
Expand Down
38 changes: 19 additions & 19 deletions lib/lunchbot_web/templates/admin/order/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@
<section id="torch-filters">
<h3>Find Orders</h3>
<%= form_tag @conn.request_path, method: :get, id: "torch-filters-form" do %>




<div class="field">
<label>User</label>
<%= number_filter_select(:order, :user_id, @conn.params) %>
<%= filter_number_input(:order, :user_id, @conn.params) %>
</div>

<div class="field">
<label>Menu</label>
<%= number_filter_select(:order, :menu_id, @conn.params) %>
<%= filter_number_input(:order, :menu_id, @conn.params) %>
</div>

<div class="field">
<label>Lunch order</label>
<%= number_filter_select(:order, :lunch_order_id, @conn.params) %>
<%= filter_number_input(:order, :lunch_order_id, @conn.params) %>
<%= number_filter_select(:order, :office_lunch_order_id, @conn.params) %>
<%= filter_number_input(:order, :office_lunch_order_id, @conn.params) %>
</div>

<button type="submit" class="torch-button">Search</button>
<%= link "Clear Filters", to: Routes.admin_order_path(@conn, :index) %>
<% end %>
Expand All @@ -41,26 +41,26 @@
<table>
<thead>
<tr>

<th><%= table_link(@conn, "User", :user_id) %></th>

<th><%= table_link(@conn, "Menu", :menu_id) %></th>
<th><%= table_link(@conn, "Lunch order", :lunch_order_id) %></th>

<th><%= table_link(@conn, "Lunch order", :office_lunch_order_id) %></th>

<th><span>Actions</span></th>
</tr>
</thead>
<tbody>
<%= for order <- @orders do %>
<tr>

<td><%= order.user_id %></td>

<td><%= order.menu_id %></td>
<td><%= order.lunch_order_id %></td>

<td><%= order.office_lunch_order_id %></td>

<td class="torch-actions">
<span><%= link "Show", to: Routes.admin_order_path(@conn, :show, order) %></span>
<span><%= link "Edit", to: Routes.admin_order_path(@conn, :edit, order) %></span>
Expand Down
10 changes: 5 additions & 5 deletions lib/lunchbot_web/templates/admin/order/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
<h3>Order Details</h3>
</header>
<section class="torch-show-details">

<div class="torch-show-attribute">
<div class="torch-show-label">User:</div>
<div class="torch-show-data"><%= @order.user_id %></div>
</div>

<div class="torch-show-attribute">
<div class="torch-show-label">Menu:</div>
<div class="torch-show-data"><%= @order.menu_id %></div>
</div>

<div class="torch-show-attribute">
<div class="torch-show-label">Lunch order:</div>
<div class="torch-show-data"><%= @order.lunch_order_id %></div>
<div class="torch-show-data"><%= @order.office_lunch_order_id %></div>
</div>

</section>
</div>
</section>
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ defmodule Lunchbot.MixProject do
{:dotenv, "~> 3.0.0", only: [:dev, :test]},
{:mox, "~> 1.0", only: :test},
{:phoenix_pubsub, "~> 2.0"},
{:csv, "~> 2.4"}
{:csv, "~> 2.4"},
{:faker, "~> 0.17"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"elixir_auth_google": {:hex, :elixir_auth_google, "1.6.3", "13f9fcecc32d8a0e5eee5d24419d603dbec495fcc9c60ac314d64ba4af17fc83", [:mix], [{:httpoison, "~> 1.8.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "6c0e9852879b22ac20908ca155cfe389fc099de6075359adbffe5a3d8fa8592f"},
"elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"},
"esbuild": {:hex, :esbuild, "0.5.0", "d5bb08ff049d7880ee3609ed5c4b864bd2f46445ea40b16b4acead724fb4c4a3", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "f183a0b332d963c4cfaf585477695ea59eef9a6f2204fdd0efa00e099694ffe5"},
"faker": {:hex, :faker, "0.17.0", "671019d0652f63aefd8723b72167ecdb284baf7d47ad3a82a15e9b8a6df5d1fa", [:mix], [], "hexpm", "a7d4ad84a93fd25c5f5303510753789fc2433ff241bf3b4144d3f6f291658a6a"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"filtrex": {:hex, :filtrex, "0.4.3", "d59e496d385b19df7e3a613ad74deca4ac5ef1666352e9e435e8442fc9ae4c70", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:timex, "~> 3.1", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "a374999d00174c3a6c617def9c9d199a2bc5928d49a227d1de622ccbadbff1d0"},
"floki": {:hex, :floki, "0.33.1", "f20f1eb471e726342b45ccb68edb9486729e7df94da403936ea94a794f072781", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "461035fd125f13fdf30f243c85a0b1e50afbec876cbf1ceefe6fddd2e6d712c6"},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Lunchbot.Repo.Migrations.AddReferencesToOrders do
use Ecto.Migration

def change do
alter table(:orders) do
modify :user_id, references(:users, on_delete: :nilify_all)
# restrict means you can't delete menu_id if there are still orders that reference that menu
modify :menu_id, references(:menus, on_delete: :restrict)
modify :lunch_order_id, references(:office_lunch_orders, on_delete: :delete_all)
end

rename table(:orders), :lunch_order_id, to: :office_lunch_order_id

alter table(:order_items) do
modify :order_id, references(:orders, on_delete: :delete_all)
modify :item_id, references(:items, on_delete: :restrict)
end

alter table(:order_item_options) do
modify :order_item_id, references(:order_items, on_delete: :delete_all)
modify :option_id, references(:options, on_delete: :restrict)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Lunchbot.Repo.Migrations.AddReferencesToMenu do
use Ecto.Migration

def change do
alter table(:menus) do
modify :office_id, references(:offices, on_delete: :delete_all)
end
end
end
Loading