Added Items and Dashboard
This commit is contained in:
15
app/controllers/dashboard_controller.rb
Normal file
15
app/controllers/dashboard_controller.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class DashboardController < ApplicationController
|
||||
def index
|
||||
# 1. Zählt alle registrierten Hardware-Unikate
|
||||
@total_items = Item.count
|
||||
|
||||
# 2. Berechnet den Gesamtwert aller Geräte (summiert das Feld :price)
|
||||
@total_value = Item.sum(:price)
|
||||
|
||||
# 3. Zählt die Artikel, die weder einem User noch einem Raum gehören (= im Lager liegen)
|
||||
@items_in_storage = Item.where(user_id: nil, room_id: nil).count
|
||||
|
||||
# 4. Holt die letzten 10 registrierten Artikel für die Aktivitätenanzeige
|
||||
@recent_items = Item.order(created_at: :desc).limit(10).includes(:category, :user, :room)
|
||||
end
|
||||
end
|
||||
81
app/controllers/items_controller.rb
Normal file
81
app/controllers/items_controller.rb
Normal file
@@ -0,0 +1,81 @@
|
||||
class ItemsController < ApplicationController
|
||||
before_action :set_item, only: %i[ show edit update destroy ]
|
||||
|
||||
# GET /items or /items.json
|
||||
def index
|
||||
@items = Item.all.includes(:category, :user, :room).order(created_at: :desc)
|
||||
end
|
||||
|
||||
# GET /items/1 or /items/1.json
|
||||
def show
|
||||
@assignment_logs = @item.assignment_logs.includes(:user, :room).order(assigned_at: :desc)
|
||||
end
|
||||
|
||||
# GET /items/new
|
||||
def new
|
||||
@item = Item.new
|
||||
end
|
||||
|
||||
# GET /items/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /items or /items.json
|
||||
def create
|
||||
@item = Item.new(item_params)
|
||||
|
||||
respond_to do |format|
|
||||
if @item.save
|
||||
format.html { redirect_to @item, notice: "Artikel '#{@item.name}' wurde erfolgreich im System registriert." }
|
||||
format.json { render :show, status: :created, location: @item }
|
||||
else
|
||||
format.html { render :new, status: :unprocessable_content }
|
||||
format.json { render json: @item.errors, status: :unprocessable_content }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /items/1 or /items/1.json
|
||||
def update
|
||||
respond_to do |format|
|
||||
if @item.update(item_params)
|
||||
format.html { redirect_to @item, notice: "Artikel '#{@item.name} wurde erfolgreich aktualisiert.", status: :see_other }
|
||||
format.json { render :show, status: :ok, location: @item }
|
||||
else
|
||||
format.html { render :edit, status: :unprocessable_content }
|
||||
format.json { render json: @item.errors, status: :unprocessable_content }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /items/1 or /items/1.json
|
||||
def destroy
|
||||
@item.destroy!
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to items_path, notice: "Artikel wurde erfolgreich aus dem System gelöscht", status: :see_other }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_item
|
||||
@item = Item.find(params.expect(:id))
|
||||
end
|
||||
|
||||
# Strong Parameters: Schützt vor Mass-Assignment-Injections
|
||||
def item_params
|
||||
params.require(:item).permit(
|
||||
:name,
|
||||
:sku,
|
||||
:sticker_id,
|
||||
:serial_number,
|
||||
:price,
|
||||
:notes,
|
||||
:category_id,
|
||||
:user_id, # Für die flexible Zuweisung an Mitarbeiter
|
||||
:room_id # Für die flexible Zuweisung an Räume
|
||||
)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user