Skip to content
Draft
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
7 changes: 4 additions & 3 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
import_deps: [:ecto, :phoenix],
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
subdirectories: ["priv/*/migrations"]
import_deps: [:ecto, :ecto_sql, :phoenix],
subdirectories: ["priv/*/migrations"],
plugins: [Phoenix.LiveView.HTMLFormatter],
inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}", "priv/*/seeds.exs"]
]
2 changes: 0 additions & 2 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@

.alert {
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
}
Expand Down Expand Up @@ -260,7 +259,6 @@
/* Alerts and form errors used by phx.new */
.alert {
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
}
Expand Down
7 changes: 4 additions & 3 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ config :fitness, FitnessWeb.Endpoint,
pubsub_server: Fitness.PubSub,
live_view: [signing_salt: "vOF6D4CD"]


config :tailwind, version: "3.3.1", default: [
config :tailwind,
version: "3.3.1",
default: [
args: ~w(
--config=tailwind.config.js
--input=css/app.css
Expand All @@ -27,7 +28,7 @@ config :fitness, FitnessWeb.Endpoint,
cd: Path.expand("../assets", __DIR__)
]

config :chartkick, json_serializer: Jason
config :chartkick, json_serializer: Jason

# Configures the mailer
#
Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ config :fitness, FitnessWeb.Endpoint,
config :fitness, Fitness.Mailer, adapter: Swoosh.Adapters.Test

# Print only warnings and errors during test
config :logger, level: :warn
config :logger, level: :warning

# Initialize plugs at runtime for faster test compilation
config :phoenix, :plug_init_mode, :runtime
2 changes: 1 addition & 1 deletion lib/fitness/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ defmodule Fitness.Accounts do

alias Fitness.Accounts.{User, UserToken, UserNotifier}


def is_admin?(user) do
user.is_admin
end

def list_of_users do
Repo.all(User)
end

## Database getters

@doc """
Expand Down
1 change: 0 additions & 1 deletion lib/fitness/accounts/services/chart_data.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Fitness.Accounts.Services.ChartData do

alias Fitness.WorkoutTemplates

def all_complete_workout_chart_data(current_user_id) do
Expand Down
23 changes: 17 additions & 6 deletions lib/fitness/accounts/services/player_scores.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ defmodule Fitness.Accounts.Services.PlayerScores do
alias Fitness.WorkoutTemplates

def update_new_player_score_and_broadcast_score_board(current_user, workout_templates) do

list_of_workout_template_is_finish_and_is_belong_to_user =
Enum.filter(workout_templates, fn each -> each.is_finished == true and each.user_id == current_user.id end)
Enum.filter(workout_templates, fn each ->
each.is_finished == true and each.user_id == current_user.id
end)

total_workout_template_score =
Enum.reduce(list_of_workout_template_is_finish_and_is_belong_to_user, 0, fn each, acc ->
acc + each.workout_template_score
end)

Accounts.update_user_player_score(current_user, %{"player_score" => "#{total_workout_template_score}"})
Accounts.update_user_player_score(current_user, %{
"player_score" => "#{total_workout_template_score}"
})

update_users = Accounts.list_of_users()
update_workout_template = WorkoutTemplates.list_workout_templates()
Expand All @@ -23,8 +26,16 @@ defmodule Fitness.Accounts.Services.PlayerScores do
end)
end

defp broadcast_score_board(updated_users, update_workout_template, updated_player_score, user_id) do
Phoenix.PubSub.broadcast(Fitness.PubSub, "score_board",
{:update_users, {updated_users, update_workout_template, updated_player_score, user_id}})
defp broadcast_score_board(
updated_users,
update_workout_template,
updated_player_score,
user_id
) do
Phoenix.PubSub.broadcast(
Fitness.PubSub,
"score_board",
{:update_users, {updated_users, update_workout_template, updated_player_score, user_id}}
)
end
end
13 changes: 8 additions & 5 deletions lib/fitness/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ defmodule Fitness.Accounts.User do
defp validate_username(changeset) do
changeset
|> validate_required([:username])
|> validate_format(:username, ~r/^[a-zA-Z0-9!#$_:+-?]+$/, message: "A username should consist of both letters and numbers, and should not contain any spaces")
|> validate_format(:username, ~r/^[a-zA-Z0-9!#$_:+-?]+$/,
message:
"A username should consist of both letters and numbers, and should not contain any spaces"
)
|> validate_length(:username, min: 5, max: 40)
|> unsafe_validate_unique(:username, Fitness.Repo)
|> unique_constraint(:username)
Expand All @@ -64,7 +67,10 @@ defmodule Fitness.Accounts.User do
defp validate_name(changeset) do
changeset
|> validate_required([:name])
|> validate_format(:name, ~r/\A[a-zA-Z ]+\z/, message: "A name must be made up of only letters and should not include any symbols or numbers.")
|> validate_format(:name, ~r/\A[a-zA-Z ]+\z/,
message:
"A name must be made up of only letters and should not include any symbols or numbers."
)
|> validate_length(:name, max: 40)
end

Expand Down Expand Up @@ -109,7 +115,6 @@ defmodule Fitness.Accounts.User do
end

def name_changeset(user, attrs) do

user
|> cast(attrs, [:name])
|> validate_name()
Expand All @@ -126,11 +131,9 @@ defmodule Fitness.Accounts.User do
%{changes: %{image: _}} = changeset -> changeset
%{} = changeset -> add_error(changeset, :image, "did not change")
end

end

def player_score_changeset(user, attrs) do

user
|> cast(attrs, [:player_score])
|> case do
Expand Down
4 changes: 2 additions & 2 deletions lib/fitness/exercises.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ defmodule Fitness.Exercises do

"""
def list_exercises(search_query) do
exercises = Repo.all(Exercise)
SearchTerm.filter_by_search_term(exercises, search_query)
exercises = Repo.all(Exercise)
SearchTerm.filter_by_search_term(exercises, search_query)
end

def list_exercises do
Expand Down
4 changes: 0 additions & 4 deletions lib/fitness/exercises/finder/search_term.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
defmodule Fitness.Exercises.Finder.SearchTerm do

def filter_by_search_term(exercises, search_query) do


found_search_query =
case search_query do
"" ->
Expand Down Expand Up @@ -31,5 +28,4 @@ defmodule Fitness.Exercises.Finder.SearchTerm do

found_search_query
end

end
3 changes: 1 addition & 2 deletions lib/fitness/workout_templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ defmodule Fitness.WorkoutTemplates do
"""
def get_workout_template!(id) do
Repo.get!(WorkoutTemplate, id)
|> Repo.preload(workout_items: from(w in WorkoutItem, order_by: w.id))
|> Repo.preload(:workout_items)
end

def get_workout_item!(id) do
Repo.get!(WorkoutItem, id)
end


@doc """
Creates a workout_template.

Expand Down
22 changes: 10 additions & 12 deletions lib/fitness/workout_templates/services/workout_item_logic.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
defmodule Fitness.WorkoutTemplates.Services.WorkoutItemLogic do
alias Fitness.WorkoutTemplates


# calculate single workout template score
def update_workout_template_score(workout_template) do

list_of_complete_workout_items =
Enum.filter(workout_template.workout_items, fn each ->
each.check_box == true
Expand All @@ -13,21 +11,21 @@ defmodule Fitness.WorkoutTemplates.Services.WorkoutItemLogic do
if length(list_of_complete_workout_items) == length(workout_template.workout_items) do
total_score = Enum.reduce(list_of_complete_workout_items, 0, fn _each, acc -> acc + 20 end)

WorkoutTemplates.update_workout_template(workout_template, %{
"workout_template_score" => "#{total_score + 50}",
"is_finished" => "true"
})
WorkoutTemplates.update_workout_template(workout_template, %{
"workout_template_score" => "#{total_score + 50}",
"is_finished" => "true"
})

true
true
else
total_score = Enum.reduce(list_of_complete_workout_items, 0, fn _each, acc -> acc + 20 end)

WorkoutTemplates.update_workout_template(workout_template, %{
"workout_template_score" => "#{total_score}",
"is_finished" => "true"
})
WorkoutTemplates.update_workout_template(workout_template, %{
"workout_template_score" => "#{total_score}",
"is_finished" => "true"
})

false
false
end
end

Expand Down
10 changes: 9 additions & 1 deletion lib/fitness/workout_templates/workout_item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ defmodule Fitness.WorkoutTemplates.WorkoutItem do
@doc false
def changeset(workout, attrs) do
workout
|> cast(attrs, [:sets, :weight, :weight_unit, :check_box, :reps, :exercise_id, :workout_template_id])
|> cast(attrs, [
:sets,
:weight,
:weight_unit,
:check_box,
:reps,
:exercise_id,
:workout_template_id
])
|> validate_required([:sets, :weight, :weight_unit, :reps, :workout_template_id, :exercise_id])
end
end
19 changes: 17 additions & 2 deletions lib/fitness_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ defmodule FitnessWeb do
and import those modules here.
"""

def static_paths, do: ~w(assets fonts images videos favicon.ico robots.txt)

def controller do
quote do
use Phoenix.Controller, namespace: FitnessWeb

import Plug.Conn
import FitnessWeb.Gettext
alias FitnessWeb.Router.Helpers, as: Routes

unquote(verified_routes())
end
end

Expand All @@ -45,7 +49,7 @@ defmodule FitnessWeb do
def live_view do
quote do
use Phoenix.LiveView,
layout: {FitnessWeb.LayoutView, "live.html"}
layout: {FitnessWeb.LayoutView, :live}

unquote(view_helpers())
end
Expand Down Expand Up @@ -90,7 +94,8 @@ defmodule FitnessWeb do
use Phoenix.HTML

# Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
import Phoenix.LiveView.Helpers

import Phoenix.Component
import FitnessWeb.LiveHelpers

# Import basic rendering functionality (render, render_layout, etc)
Expand All @@ -99,6 +104,16 @@ defmodule FitnessWeb do
import FitnessWeb.ErrorHelpers
import FitnessWeb.Gettext
alias FitnessWeb.Router.Helpers, as: Routes
unquote(verified_routes())
end
end

def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: FitnessWeb.Endpoint,
router: FitnessWeb.Router,
statics: FitnessWeb.static_paths()
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/fitness_web/controllers/user_session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ defmodule FitnessWeb.UserSessionController do
%{"email" => email, "password" => password} = user_params

if user = Accounts.get_user_by_email_and_password(email, password) do

conn
|> UserAuth.log_in_user(user, user_params)
else
Expand Down
2 changes: 1 addition & 1 deletion lib/fitness_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule FitnessWeb.Endpoint do
at: "/",
from: :fitness,
gzip: false,
only: ~w(assets fonts images favicon.ico robots.txt)
only: FitnessWeb.static_paths()

# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
Expand Down
4 changes: 2 additions & 2 deletions lib/fitness_web/live/exercise_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule FitnessWeb.ExerciseLive.FormComponent do
{:noreply,
socket
|> put_flash(:info, "Exercise updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
|> push_navigate(to: socket.assigns.return_to)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
Expand All @@ -46,7 +46,7 @@ defmodule FitnessWeb.ExerciseLive.FormComponent do
{:noreply,
socket
|> put_flash(:info, "Exercise created successfully")
|> push_redirect(to: socket.assigns.return_to)}
|> push_navigate(to: socket.assigns.return_to)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
Expand Down
Loading