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

Config files added togitignore #23

Open
wants to merge 15 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
25,663 changes: 0 additions & 25,663 deletions .elixir-tools/next-ls.log

This file was deleted.

44 changes: 44 additions & 0 deletions .github/check-format-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Check Format and Run Tests

on:
pull_request:
paths:
- '**/*.ex'
- '**/*.exs'
- 'mix.exs'
- '.formatter.exs'

jobs:
format-and-test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Elixir
uses: erlef/setup-elixir@v2
with:
elixir-version: '1.14'
otp-version: '25'

- name: Debug Environment
run: |
mix --version
elixir --version
otp --version
env

- name: Install dependencies
run: mix deps.get

- name: Check code formatting
run: |
mix format --check-formatted
if [ $? -ne 0 ]; then
echo "Code is not formatted correctly. Please run 'mix format' and commit your changes."
exit 1
fi

- name: Run tests
run: mix test
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,35 @@ polly-*.tar
npm-debug.log
/assets/node_modules/

.elixir_ls/
*.plt
credo_report/
.elixir-tools

# Ignore directories created by editors and IDEs
*.swp
*.swo
.idea/
.vscode/
*.sublime-project
*.sublime-workspace

# Ignore node_modules (if using assets with Node.js)
assets/node_modules/

# Ignore logs and temporary files
/log
/tmp

# Ignore secrets and environment variables
config/*.secret.exs
config/*.prod.exs
config/*.dev.exs
.env
.env.*

# Ignore compiled output
*.beam
*.ez

.elixir-tools/
22 changes: 22 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,25 @@
@import "tailwindcss/utilities";

/* This file is for your main application CSS */

.btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0.5rem 1rem;
border-radius: 0.375rem;
font-weight: bold;
text-align: center;
text-decoration: none;
cursor: pointer;
transition: background-color 0.3s ease, color 0.3s ease;
}

.btn-danger {
background-color: #dc3545; /* Bootstrap Danger Color */
color: white;
}

.btn-danger:hover {
background-color: #c82333; /* Darker shade on hover */
}
4 changes: 4 additions & 0 deletions assets/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}

.flex-col {
flex-direction: column;
}
18 changes: 16 additions & 2 deletions lib/polly/polls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ defmodule Polly.Polls do
|> do_update_poll(poll)
end

defp do_update_poll({:ok, %Poll{} = updated_poll}, _poll) do
:ok = Polly.PollsManager.update_poll(updated_poll)
defp do_update_poll({:ok, %Poll{} = updated_poll}, poll_id) do
:ok = Polly.PollsManager.update_poll(poll_id, updated_poll)
{:ok, updated_poll}
end


defp do_update_poll({:error, changeset}, _poll) do
{:error, changeset}
end
Expand All @@ -61,4 +62,17 @@ defmodule Polly.Polls do
def change_poll(%Poll{} = poll, attrs \\ %{}) do
Poll.changeset(poll, attrs)
end

@spec delete_poll(binary()) :: :ok | {:error, term()}
def delete_poll(id) do
case Polly.PollsManager.get_poll!(id) do
nil ->
{:error, :poll_not_found}

poll ->
Polly.PollsManager.remove_poll(id)
:ok
end
end

end
31 changes: 21 additions & 10 deletions lib/polly/polls_manager.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
defmodule Polly.PollsManager do
alias Polly.Schema.Poll
# alias Polly.StorageBehaviour

@storage_module Application.compile_env(:polly, :storage_module, Polly.ETSStorage)

def init() do
@storage_module.init()
end
Expand All @@ -21,7 +20,7 @@ defmodule Polly.PollsManager do
end
end

@spec list_polls_with_ids :: Keyword.t()
@spec list_polls_with_ids() :: Keyword.t()
def list_polls_with_ids() do
@storage_module.list_polls_with_ids()
|> Enum.map(fn {id, poll} ->
Expand Down Expand Up @@ -73,10 +72,15 @@ defmodule Polly.PollsManager do

@spec update_poll(binary(), Poll.t()) :: :ok | {:error, atom()}
def update_poll(poll_id, %Poll{} = updated_poll) do
if @storage_module.get_poll!(poll_id) do
@storage_module.update_poll(poll_id, updated_poll)
else
{:error, :poll_not_found}
try do
if @storage_module.get_poll!(poll_id) do
@storage_module.update_poll(poll_id, updated_poll)
else
{:error, :poll_not_found}
end
rescue
ArgumentError ->
{:error, :poll_not_found}
end
end

Expand All @@ -86,8 +90,15 @@ defmodule Polly.PollsManager do
end


@spec update_poll(Poll.t()) :: :ok | {:error, any()}
def update_poll(%Poll{} = _poll) do
# Your ETS or other storage logic to update the poll
@spec remove_poll(binary()) :: :ok | {:error, term()}
def remove_poll(id) do
case @storage_module.get_poll!(id) do
nil ->
{:error, :poll_not_found}

_poll ->
@storage_module.delete_poll(id)
:ok
end
end
end
1 change: 1 addition & 0 deletions lib/polly/storage/behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ defmodule Polly.StorageBehaviour do
@callback safe_lookup_element(binary()) :: integer()
@callback update_poll(binary(), Poll.t()) :: :ok | {:error, atom()}
# @callback replace_option_votes(Poll.t(), boolean()) :: Poll.t()
@callback delete_poll(binary()) :: :ok

end
37 changes: 33 additions & 4 deletions lib/polly/storage/ets_storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@ defmodule Polly.ETSStorage do
@impl Polly.StorageBehaviour
def init() do
:ets.new(@polls, [:public, :named_table, write_concurrency: true, read_concurrency: true])
:ets.new(@polls_votes, [:public, :named_table, write_concurrency: true, read_concurrency: true])
:ets.new(@polls_options_votes, [:public, :named_table, write_concurrency: true, read_concurrency: true])

:ets.new(@polls_votes, [
:public,
:named_table,
write_concurrency: true,
read_concurrency: true
])

:ets.new(@polls_options_votes, [
:public,
:named_table,
write_concurrency: true,
read_concurrency: true
])

:ok
end

Expand Down Expand Up @@ -53,14 +66,17 @@ defmodule Polly.ETSStorage do

@impl Polly.StorageBehaviour
def get_poll_votes!(poll_id) do
:ets.lookup_element(@polls_votes, poll_id, 2)
case :ets.lookup(:polls_votes, poll_id) do
[{^poll_id, votes}] -> votes
[] -> []
end
end

def replace_option_votes(poll, true) do
updated_options =
Enum.map(poll.options, fn option ->
Map.replace(option, :votes, safe_lookup_element(option.id))
end)

Map.replace(poll, :options, updated_options)
end

Expand Down Expand Up @@ -93,4 +109,17 @@ defmodule Polly.ETSStorage do
{:error, :poll_not_found}
end
end

@impl Polly.StorageBehaviour
@spec delete_poll(binary()) :: :ok | {:error, term()}
def delete_poll(id) do
case :ets.lookup(:polls, id) do
[] ->
{:error, :poll_not_found}

_ ->
:ets.delete(:polls, id)
:ok
end
end
end
Loading