Skip to content

Commit

Permalink
Add number of tickets field
Browse files Browse the repository at this point in the history
  • Loading branch information
lgmfred committed Oct 11, 2023
1 parent 3e3712d commit 2e55d86
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 38 deletions.
5 changes: 3 additions & 2 deletions lib/elixir_conf_africa/ticket_types/ticket_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule ElixirConfAfrica.TicketTypes.TicketType do
field :name, :string
field :description, :string
field :price, :decimal
field :number, :integer
belongs_to :event, ElixirConfAfrica.Events.Event

timestamps()
Expand All @@ -16,8 +17,8 @@ defmodule ElixirConfAfrica.TicketTypes.TicketType do
@doc false
def changeset(ticket_type, attrs) do
ticket_type
|> cast(attrs, [:event_id, :name, :description, :price])
|> validate_required([:event_id, :name, :description, :price])
|> cast(attrs, [:event_id, :name, :description, :price, :number])
|> validate_required([:event_id, :name, :description, :price, :number])
|> foreign_key_constraint(:event_id)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ defmodule ElixirConfAfricaWeb.TicketTypeLive.FormComponent do
phx-change="validate"
phx-submit="save"
>
<.input field={@form[:event_id]} type="number" />
<.input field={@form[:event_id]} type="number" label="Event ID" />
<.input field={@form[:name]} type="text" label="Name" />
<.input field={@form[:number]} type="number" label="Number" />
<.input field={@form[:description]} type="text" label="Description" />
<.input field={@form[:price]} type="number" label="Price" step="any" />
<:actions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
row_click={fn {_id, ticket_type} -> JS.navigate(~p"/ticket_types/#{ticket_type}") end}
>
<:col :let={{_id, ticket_type}} label="Name"><%= ticket_type.name %></:col>
<:col :let={{_id, ticket_type}} label="Number"><%= ticket_type.number %></:col>
<:col :let={{_id, ticket_type}} label="Event ID">
<.link navigate={~p"/events/#{ticket_type.event_id}"}><%= ticket_type.event_id %></.link>
</:col>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<.list>
<:item title="Name"><%= @ticket_type.name %></:item>
<:item title="Number"><%= @ticket_type.number %></:item>
<:item title="Description"><%= @ticket_type.description %></:item>
<:item title="Price"><%= @ticket_type.price %></:item>
</.list>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule ElixirConfAfrica.Repo.Migrations.CreateTicketTypes do
add :name, :string, null: false
add :description, :text, null: false
add :price, :decimal, null: false
add :number, :integer, null: false
add :event_id, references(:events, on_delete: :nothing), null: false

timestamps()
Expand Down
15 changes: 10 additions & 5 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Enum.each(1..10, fn _num ->
event_id: event.id,
name: type,
description: Faker.Lorem.sentence(),
price: 0.0 + Enum.random(20..500)
price: 0.0 + Enum.random(20..500),
number: Enum.random(100..500)
}
|> Repo.insert!()
end)
Expand Down Expand Up @@ -71,7 +72,8 @@ Enum.each(Enum.shuffle(ticket_types_names), fn type ->
event_id: event.id,
name: type,
description: Faker.Lorem.sentence(),
price: 0.0 + Enum.random(20..500)
price: 0.0 + Enum.random(20..500),
number: Enum.random(100..500)
}
|> Repo.insert!()
end)
Expand All @@ -98,7 +100,8 @@ Enum.each(Enum.shuffle(ticket_types_names), fn type ->
event_id: event.id,
name: type,
description: Faker.Lorem.sentence(),
price: 0.0 + Enum.random(20..500)
price: 0.0 + Enum.random(20..500),
number: Enum.random(100..500)
}
|> Repo.insert!()
end)
Expand All @@ -123,7 +126,8 @@ Enum.each(Enum.shuffle(ticket_types_names), fn type ->
event_id: event.id,
name: type,
description: Faker.Lorem.sentence(),
price: 0.0 + Enum.random(20..500)
price: 0.0 + Enum.random(20..500),
number: Enum.random(100..500)
}
|> Repo.insert!()
end)
Expand All @@ -150,7 +154,8 @@ Enum.each(Enum.shuffle(ticket_types_names), fn type ->
event_id: event.id,
name: type,
description: Faker.Lorem.sentence(),
price: 0.0 + Enum.random(20..500)
price: 0.0 + Enum.random(20..500),
number: Enum.random(100..500)
}
|> Repo.insert!()
end)
9 changes: 6 additions & 3 deletions test/elixir_conf_africa/ticket_types_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule ElixirConfAfrica.TicketTypesTest do
import ElixirConfAfrica.TicketTypesFixtures
import ElixirConfAfrica.EventsFixtures

@invalid_attrs %{name: nil, description: nil, price: nil}
@invalid_attrs %{name: nil, description: nil, price: nil, number: nil}

test "list_ticket_types/0 returns all ticket_types" do
event = event_fixture()
Expand All @@ -31,7 +31,8 @@ defmodule ElixirConfAfrica.TicketTypesTest do
event_id: event.id,
name: "some name",
description: "some description",
price: "120.5"
price: "120.5",
number: "357"
}

assert {:ok, %TicketType{} = ticket_type} = TicketTypes.create_ticket_type(valid_attrs)
Expand All @@ -51,7 +52,8 @@ defmodule ElixirConfAfrica.TicketTypesTest do
update_attrs = %{
name: "some updated name",
description: "some updated description",
price: "456.7"
price: "456.7",
number: "579"
}

assert {:ok, %TicketType{} = ticket_type} =
Expand All @@ -60,6 +62,7 @@ defmodule ElixirConfAfrica.TicketTypesTest do
assert ticket_type.name == "some updated name"
assert ticket_type.description == "some updated description"
assert ticket_type.price == Decimal.new("456.7")
assert ticket_type.number == 579
end

test "update_ticket_type/2 with invalid data returns error changeset" do
Expand Down
12 changes: 9 additions & 3 deletions test/elixir_conf_africa_web/live/ticket_type_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ defmodule ElixirConfAfricaWeb.TicketTypeLiveTest do
import ElixirConfAfrica.TicketTypesFixtures
import ElixirConfAfrica.EventsFixtures

@create_attrs %{name: "some name", description: "some description", price: "120.5"}
@create_attrs %{
name: "some name",
description: "some description",
price: "120.5",
number: "357"
}
@update_attrs %{
name: "some updated name",
description: "some updated description",
price: "456.7"
price: "456.7",
number: "579"
}
@invalid_attrs %{name: nil, description: nil, price: nil}
@invalid_attrs %{name: nil, description: nil, price: nil, number: nil}

defp create_ticket_type(_) do
event = event_fixture()
Expand Down
3 changes: 2 additions & 1 deletion test/support/fixtures/ticket_types_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule ElixirConfAfrica.TicketTypesFixtures do
|> Enum.into(%{
name: "some name",
description: "some description",
price: "120.5"
price: "120.5",
number: "357"
})
|> ElixirConfAfrica.TicketTypes.create_ticket_type()

Expand Down
23 changes: 0 additions & 23 deletions test/support/fixtures/tickets_fixtures.ex

This file was deleted.

0 comments on commit 2e55d86

Please sign in to comment.