Skip to content

Commit

Permalink
Day 2 Task : Category and Expense
Browse files Browse the repository at this point in the history
  • Loading branch information
mailsg committed Sep 20, 2023
1 parent d7d3c40 commit d41d365
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 4 deletions.
72 changes: 72 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,74 @@
class CategoriesController < ApplicationController
before_action :set_category, only: %i[show edit update destroy]

# GET /categories or /categories.json
def index
@user = current_user
@categories = @user.category
end

# GET /categories/1 or /categories/1.json
def show
@category = Category.find(params[:id])
@expenses = @category.expense.order(created_at: :desc)
@categories = current_user.category
end

# GET /categories/new
def new
@user = current_user
@category = Category.new
end

# POST /categories or /categories.json
def create
@user = current_user
@category = @user.category.new(category_params)

respond_to do |format|
if @category.save
format.html { redirect_to categories_url, notice: 'Category created successfully.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /categories/1 or /categories/1.json
def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to category_url(@category), notice: 'Category updated successfully .' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end

# DELETE /categories/1 or /categories/1.json
def destroy
@category = Category.find(params[:id])
@category.destroy

respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category deleted successfully.' }
format.json { head :no_content }
end
end

private

# Callbacks to share common setup between actions.
def set_category
@category = Category.find(params[:id])
end

# Allow a list of trusted parameters.
def category_params
params.require(:category).permit(:name, :icon)
end
end
88 changes: 88 additions & 0 deletions app/controllers/expenses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,90 @@
class ExpensesController < ApplicationController
before_action :set_expense, only: %i[show edit update destroy]

# GET /expenses or /expenses.json
def index
@category = Category.find(params[:category_id])
@expenses = @category.expense.all
end

# GET /expenses/new
def new
@author = current_user
@category = Category.find(params[:category_id])
@expense = Expense.new
end

# POST /expenses or /expenses.json
def create
@author = current_user
saved_expenses = []
category_ids = params[:expense][:category_ids].reject(&:empty?).map(&:to_i).uniq

category_ids.each do |c_id|
@category = Category.find(c_id)
@expense = Expense.new(expense_params)
@expense.author_id = current_user.id
@category.expense << @expense

if @expense.save
saved_expenses << @expense
else
# Handle the case where an expense can't be saved
respond_to do |format|
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @expense.errors, status: :unprocessable_entity }
end
end
end

save?(saved_expenses, category_ids)
# Redirect to the first category's show page after all expenses have been processed
end

# PATCH/PUT /expenses/1 or /expenses/1.json
def update
respond_to do |format|
if @expense.update(expense_params)
format.html { redirect_to expense_url(@expense), notice: 'Expense updated successfully.' }
format.json { render :show, status: :ok, location: @expense }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @expense.errors, status: :unprocessable_entity }
end
end
end

# DELETE /expenses/1 or /expenses/1.json
def destroy
@expense.destroy

respond_to do |format|
format.html { redirect_to expenses_url, notice: 'Expense successfully deleted.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup between actions.
def set_expense
@expense = Expense.find(params[:id])
end

# Allow a list of trusted parameters.
def expense_params
params.require(:expense).permit(:name, :amount, :author_id)
end

def save?(saved_expenses, category_ids)
if saved_expenses.present?
redirect_to category_path(category_ids.first), notice: 'Expenses created successfully.'
else
# The case where no expenses were successfully saved
respond_to do |format|
format.html { render :new, status: :unprocessable_entity }
format.json { render json: { error: 'No expenses saved.' }, status: :unprocessable_entity }
end
end
end
end
14 changes: 14 additions & 0 deletions app/views/categories/_category.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%= stylesheet_link_tag "categories" %>

<div id="<%= dom_id category %>" class= "category">
<div class="icon-name">
<img src='<%= category.icon %>' class= "icon"/>
<span class="cname">
<p><%= category.name %></p>
<p><%= category.created_at.to_date%></p>
</span>
</div>
<p class="tamount">
$<%=category.total_amount%>
</p>
</div>
27 changes: 27 additions & 0 deletions app/views/categories/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%=form_with(model: category) do |form| %>
<% if category.errors.any? %>
<div style="color: red">
<h2><%= pluralize(category.errors.count, "error") %> did not allow this to be saved:</h2>

<ul>
<% category.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name ,placeholder: "full name", autofocus:true %>
</div>

<div class="field">
<%= form.label :icon, style: "display: block" %>
<%= form.text_field :icon , placeholder: "url" , autofocus:true %>
</div>

<div class="field actions">
<%= form.submit "save", class: "link_to" %>
</div>
<% end %>
9 changes: 9 additions & 0 deletions app/views/categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% content_for :page_title, "Categories" %>
<%= link_to "New category", new_category_path, class: "link_to newcat" %>
<h1>Categories</h1>

<div id="categories">
<% @categories.each do |category| %>
<%= link_to render(category), category_path(category) %>
<% end %>
</div>
11 changes: 11 additions & 0 deletions app/views/categories/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= stylesheet_link_tag "form" %>
<%= stylesheet_link_tag "categories" %>
<% content_for :page_title, "New Category" %>
<div>
<%= link_to " < Back", categories_path, class:"link_to back"%>
</div>
<h1>New category</h1>
<section class = "new_cat">
<%= render "form", category: @category %>
</section>
<br>
17 changes: 17 additions & 0 deletions app/views/categories/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= stylesheet_link_tag "categories" %>
<% content_for :page_title, "Expenses" %>
<p class="tamountE"> Total Amount: <%= @category.total_amount%> </p>

<div id="expenses">
<% @expenses.each do |expense| %>
<%= render expense %>
<% end %>
</div>

<div >
<div class = "links" >
<%= link_to "Back to categories", categories_path , class: "link_to"%>
<%= link_to "New expense", new_category_expense_path(@category) , class: "link_to" %>
</div>
<%= button_to "Delete this category", @category, method: :delete , class: "link_to delete" %>
</div>
13 changes: 13 additions & 0 deletions app/views/expenses/_expense.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= stylesheet_link_tag "categories" %>
<div id="<%= dom_id expense %>", class ="expense">
<p>
<strong>Name:</strong>
<%= expense.name %>
</p>

<p>
<strong>Amount:</strong>
<%= expense.amount %>
</p>

</div>
37 changes: 37 additions & 0 deletions app/views/expenses/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<%= form_with(model: [@category, @expense]) do |form| %>
<% if expense.errors.any? %>
<div style="color: red">
<h2><%= pluralize(expense.errors.count, "error") %> prohibited this expense from being saved:</h2>

<ul>
<% expense.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class = "field">
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name , required: true %>
</div>

<div class = "field">
<%= form.label :amount, style: "display: block" %>
<%= form.text_field :amount , required: true%>
</div>

<div class="checkbox-exp">
<%= form.label :categories %>
<%= form.collection_check_boxes(:category_ids, @author.category.all, :id, :name) do |b| %>
<div class="checkbox">
<%= b.check_box %>
<%= b.label %>
</div>
<% end %>
</div>

<div class = "field actions">
<%= form.submit 'Save', class: "link_to" %>
</div>
<% end %>
11 changes: 11 additions & 0 deletions app/views/expenses/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% content_for :page_title, "Expenses" %>

<h1>Expenses</h1>

<div id="expenses">
<% @expenses.each do |expense| %>
<%= render expense %>
<% end %>
</div>

<%= link_to "New expense", new_category_expense_path %>
12 changes: 12 additions & 0 deletions app/views/expenses/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= stylesheet_link_tag "form" %>
<%= stylesheet_link_tag "categories" %>

<div>
<%= link_to "Back", category_path(@category) , class:"link_to back" %>
</div>

<% content_for :page_title, "New Expense" %>
<section class = "new-exp">
<%= render "form", expense: @expense %>
</section>
<br>
9 changes: 9 additions & 0 deletions app/views/expenses/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p style="color: green"><%= notice %></p>

<%= render @expense %>

<div>
<%= link_to "Edit this expense", edit_expense_path(@expense) %> |
<%= link_to "Back to expenses", expenses_path %>
<%= button_to "Delete this expense", @expense, method: :delete %>
</div>
2 changes: 1 addition & 1 deletion app/views/layouts/_nav.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<li class="dropdown">
<a class="btn dropbtn"><i class="fa-solid fa-bars"></i></a>
<div class="dropdown-content">
<%= link_to 'home', categories_path , class: " pages-links btn-home" %>
<%= link_to 'Home', categories_path , class: " pages-links btn-home" %>
<%= link_to 'Log out', destroy_user_session_path , data: { turbo_method: :delete } ,class: "pages-links btn-recipes"%>
</div>
</li>
Expand Down
4 changes: 2 additions & 2 deletions app/views/main/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<p style="color: green"><%= notice %></p>
<div id="mains">
<h1 class="topicm"> SpendWise </h1>
<%= link_to "Log_in", new_user_session_path , class: "link_to login"%>
<%= link_to "sign_up", new_user_registration_path, class: "link_to login signup" %>
<%= link_to "Log in", new_user_session_path , class: "link_to login"%>
<%= link_to "Sign up", new_user_registration_path, class: "link_to login signup" %>

</div>
2 changes: 1 addition & 1 deletion app/views/splash/_splash.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
</p>
</div>
<div class="start">
<%= link_to "Get started", root_path, class: "link_to start-link" %>
<%= link_to "Go", root_path, class: "link_to start-link" %>
</div>
</section>

0 comments on commit d41d365

Please sign in to comment.