You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.3 KiB
56 lines
1.3 KiB
class EntriesController < ApplicationController
|
|
before_action :authenticate_user!
|
|
before_action :set_entry, only: %i[edit update destroy]
|
|
|
|
def index
|
|
@entries = current_user.entries.order(date: :desc)
|
|
@total_minutes = @entries.sum(&:total_minutes)
|
|
@remaining_minutes = [current_user.total_required_hours * 60 - @total_minutes, 0].max
|
|
|
|
if current_user.weekly_target_hours.positive?
|
|
remaining_hours = @remaining_minutes / 60.0
|
|
weeks_remaining = (remaining_hours / current_user.weekly_target_hours).ceil
|
|
@estimated_end_date = Date.today + (weeks_remaining * 7)
|
|
else
|
|
@estimated_end_date = nil
|
|
end
|
|
end
|
|
|
|
def new
|
|
@entry = current_user.entries.build
|
|
end
|
|
|
|
def create
|
|
@entry = current_user.entries.build(entry_params)
|
|
if @entry.save
|
|
redirect_to entries_path, notice: "Eintrag gespeichert"
|
|
else
|
|
render :new
|
|
end
|
|
end
|
|
|
|
def edit; end
|
|
|
|
def update
|
|
if @entry.update(entry_params)
|
|
redirect_to entries_path, notice: "Eintrag aktualisiert"
|
|
else
|
|
render :edit
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@entry.destroy
|
|
redirect_to entries_path, notice: "Eintrag gelöscht"
|
|
end
|
|
|
|
private
|
|
|
|
def set_entry
|
|
@entry = current_user.entries.find(params[:id])
|
|
end
|
|
|
|
def entry_params
|
|
params.require(:entry).permit(:date, :hours, :minutes)
|
|
end
|
|
end
|