Skip to content

Commit

Permalink
Create lap time belonging to the logged in user
Browse files Browse the repository at this point in the history
  • Loading branch information
robbyronk committed Oct 12, 2023
1 parent 158f502 commit 45a3d2b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 80 deletions.
2 changes: 2 additions & 0 deletions lib/leadfoot/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defmodule Leadfoot.Accounts.User do
field :confirmed_at, :naive_datetime
field :role, :string

has_many :lap_times, Leadfoot.Leaderboard.LapTime

timestamps()
end

Expand Down
6 changes: 4 additions & 2 deletions lib/leadfoot/leaderboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Leadfoot.Leaderboard do

import Ecto.Query, warn: false

alias Leadfoot.Accounts.User
alias Leadfoot.Leaderboard.LapTime
alias Leadfoot.Repo

Expand Down Expand Up @@ -49,8 +50,9 @@ defmodule Leadfoot.Leaderboard do
{:error, %Ecto.Changeset{}}
"""
def create_lap_time(attrs \\ %{}) do
%LapTime{}
def create_lap_time(%User{} = user, attrs \\ %{}) do
user
|> Ecto.build_assoc(:lap_times)
|> LapTime.changeset(attrs)
|> Repo.insert()
end
Expand Down
2 changes: 1 addition & 1 deletion lib/leadfoot_web/live/dashboard_live/view.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<div class="top-display display">
<div class="tire-temps relative">
<div class="mx-6 flex items-center justify-center">
<img src="https://i.imgur.com/H7myIcm.png"/>
<img src="https://i.imgur.com/H7myIcm.png" />
</div>
<p class="text-center text-xs md:text-sm mt-2">Tire Temperature</p>
<div class="text-md md:text-lg font-bold tire-temp absolute top-0 left-0 md:top-10 mt-5">
Expand Down
2 changes: 1 addition & 1 deletion lib/leadfoot_web/live/lap_time_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule LeadfootWeb.LapTimeLive.FormComponent do
end

defp save_lap_time(socket, :new, lap_time_params) do
case Leaderboard.create_lap_time(lap_time_params) do
case Leaderboard.create_lap_time(socket.assigns.current_user, lap_time_params) do
{:ok, lap_time} ->
notify_parent({:saved, lap_time})

Expand Down
9 changes: 8 additions & 1 deletion lib/leadfoot_web/live/lap_time_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
<:col :let={{_id, lap_time}} label="Video url"><%= lap_time.video_url %></:col>
<:col :let={{_id, lap_time}} label="Notes"><%= lap_time.notes %></:col>
<:action :let={{_id, lap_time}}>
<div :if={lap_time.user_id == @current_user_id} class="sr-only">
<div
:if={
lap_time.user_id ==
@current_user_id
}
class="sr-only"
>
<.link navigate={~p"/lap_times/#{lap_time}"}>Show</.link>
</div>
<.link :if={lap_time.user_id == @current_user_id} patch={~p"/lap_times/#{lap_time}/edit"}>
Expand Down Expand Up @@ -46,6 +52,7 @@
title={@page_title}
action={@live_action}
lap_time={@lap_time}
current_user={@current_user}
patch={~p"/lap_times"}
/>
</.modal>
32 changes: 0 additions & 32 deletions lib/leadfoot_web/live/lap_time_live/show.ex

This file was deleted.

37 changes: 0 additions & 37 deletions lib/leadfoot_web/live/lap_time_live/show.html.heex

This file was deleted.

9 changes: 7 additions & 2 deletions test/leadfoot/leaderboard_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Leadfoot.LeaderboardTest do
use Leadfoot.DataCase

alias Leadfoot.AccountsFixtures
alias Leadfoot.Leaderboard

describe "lap_times" do
Expand All @@ -21,6 +22,8 @@ defmodule Leadfoot.LeaderboardTest do
end

test "create_lap_time/1 with valid data creates a lap_time" do
user = AccountsFixtures.user_fixture()

valid_attrs = %{
car: "some car",
input_method: "some input_method",
Expand All @@ -31,7 +34,7 @@ defmodule Leadfoot.LeaderboardTest do
video_url: "some video_url"
}

assert {:ok, %LapTime{} = lap_time} = Leaderboard.create_lap_time(valid_attrs)
assert {:ok, %LapTime{} = lap_time} = Leaderboard.create_lap_time(user, valid_attrs)
assert lap_time.car == "some car"
assert lap_time.input_method == "some input_method"
assert lap_time.lap_time_millis == 42
Expand All @@ -42,7 +45,9 @@ defmodule Leadfoot.LeaderboardTest do
end

test "create_lap_time/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Leaderboard.create_lap_time(@invalid_attrs)
user = AccountsFixtures.user_fixture()

assert {:error, %Ecto.Changeset{}} = Leaderboard.create_lap_time(user, @invalid_attrs)
end

test "update_lap_time/2 with valid data updates the lap_time" do
Expand Down
12 changes: 8 additions & 4 deletions test/support/fixtures/leaderboard_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ defmodule Leadfoot.LeaderboardFixtures do
entities via the `Leadfoot.Leaderboard` context.
"""

alias Leadfoot.AccountsFixtures

@doc """
Generate a lap_time.
"""
def lap_time_fixture(attrs \\ %{}) do
{:ok, lap_time} =
attrs
|> Enum.into(%{
user = AccountsFixtures.user_fixture()

attrs =
Enum.into(attrs, %{
car: "some car",
input_method: "some input_method",
lap_time_millis: 42,
Expand All @@ -19,7 +22,8 @@ defmodule Leadfoot.LeaderboardFixtures do
tune: "some tune",
video_url: "some video_url"
})
|> Leadfoot.Leaderboard.create_lap_time()

{:ok, lap_time} = Leadfoot.Leaderboard.create_lap_time(user, attrs)

lap_time
end
Expand Down

0 comments on commit 45a3d2b

Please sign in to comment.