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.
140 lines
3.4 KiB
140 lines
3.4 KiB
class LivresController < ApplicationController
|
|
def index
|
|
if params[:query].present?
|
|
@livres = Livre.where("titre LIKE ? OR auteur LIKE ?", "%#{params[:query]}%", "%#{params[:query]}%")
|
|
else
|
|
@livres = Livre.all
|
|
end
|
|
end
|
|
|
|
def new
|
|
@livre = Livre.new
|
|
respond_to do |format|
|
|
format.html
|
|
format.turbo_stream
|
|
end
|
|
end
|
|
|
|
|
|
def create
|
|
@livre = Livre.new(livre_params)
|
|
if @livre.save
|
|
redirect_to livres_path, notice: "Livre ajouté avec succès."
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def edit
|
|
@livre = Livre.find(params[:id])
|
|
render :form, layout: false
|
|
end
|
|
|
|
def update
|
|
@livre = Livre.find(params[:id])
|
|
if @livre.update(livre_params)
|
|
render partial: "livres/ligne", locals: { livre: @livre }
|
|
else
|
|
render :form, layout: false, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def show
|
|
@livre = Livre.find(params[:id])
|
|
render partial: "livres/ligne", locals: { livre: @livre }
|
|
end
|
|
|
|
def destroy
|
|
@livre = Livre.find(params[:id])
|
|
@livre.destroy
|
|
redirect_to livres_path, notice: "Livre supprimé avec succès."
|
|
end
|
|
|
|
# === Cellule Titre ===
|
|
def edit_titre
|
|
@livre = Livre.find(params[:id])
|
|
render turbo_stream: turbo_stream.replace(
|
|
"livre_#{@livre.id}_titre",
|
|
partial: "livres/edit_titre",
|
|
locals: { livre: @livre }
|
|
)
|
|
end
|
|
|
|
def update_titre
|
|
@livre = Livre.find(params[:id])
|
|
if @livre.update(titre: params[:livre][:titre])
|
|
render turbo_stream: turbo_stream.replace(
|
|
"livre_#{@livre.id}_titre",
|
|
partial: "livres/cellule_titre",
|
|
locals: { livre: @livre }
|
|
)
|
|
else
|
|
render turbo_stream: turbo_stream.replace(
|
|
"livre_#{@livre.id}_titre",
|
|
partial: "livres/edit_titre",
|
|
locals: { livre: @livre }
|
|
), status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
# === Cellule Auteur ===
|
|
def edit_auteur
|
|
@livre = Livre.find(params[:id])
|
|
render turbo_stream: turbo_stream.replace(
|
|
"livre_#{@livre.id}_auteur",
|
|
partial: "livres/edit_auteur",
|
|
locals: { livre: @livre }
|
|
)
|
|
end
|
|
|
|
def update_auteur
|
|
@livre = Livre.find(params[:id])
|
|
if @livre.update(auteur: params[:livre][:auteur])
|
|
render turbo_stream: turbo_stream.replace(
|
|
"livre_#{@livre.id}_auteur",
|
|
partial: "livres/cellule_auteur",
|
|
locals: { livre: @livre }
|
|
)
|
|
else
|
|
render turbo_stream: turbo_stream.replace(
|
|
"livre_#{@livre.id}_auteur",
|
|
partial: "livres/edit_auteur",
|
|
locals: { livre: @livre }
|
|
), status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
# === Cellule Date de sortie ===
|
|
def edit_date
|
|
@livre = Livre.find(params[:id])
|
|
render turbo_stream: turbo_stream.replace(
|
|
"livre_#{@livre.id}_date",
|
|
partial: "livres/edit_date",
|
|
locals: { livre: @livre }
|
|
)
|
|
end
|
|
|
|
def update_date
|
|
@livre = Livre.find(params[:id])
|
|
if @livre.update(date_de_sortie: params[:livre][:date_de_sortie])
|
|
render turbo_stream: turbo_stream.replace(
|
|
"livre_#{@livre.id}_date",
|
|
partial: "livres/cellule_date",
|
|
locals: { livre: @livre }
|
|
)
|
|
else
|
|
render turbo_stream: turbo_stream.replace(
|
|
"livre_#{@livre.id}_date",
|
|
partial: "livres/edit_date",
|
|
locals: { livre: @livre }
|
|
), status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
|
|
def livre_params
|
|
params.require(:livre).permit(:titre, :auteur, :date_de_sortie)
|
|
end
|
|
end
|