class PartiesController < ApplicationController before_action :set_party, only: %i[ show edit update destroy ] http_basic_authenticate_with name: "admin", password: "passwort", only: %i[edit update create new destroy show toggle_attendent] def index @parties = Party.all end def show @friends = Friend.order(:lastname, :firstname) end def new @party = Party.new end def edit end def create @party = Party.new(party_params) respond_to do |format| if @party.save format.html { redirect_to parties_path, notice: "Party was successfully created." } else format.html { render :new, status: :unprocessable_entity } end end end def update respond_to do |format| if @party.update(party_params) format.html { redirect_to parties_path, notice: "Party was successfully updated.", status: :see_other } else format.html { render :edit, status: :unprocessable_entity } end end end def destroy @party.destroy! respond_to do |format| format.html { redirect_to parties_path, notice: "Party was successfully destroyed.", status: :see_other } end end def toggle_attendent member = false party = Party.find_by(id: params[:party_id]) friend = Friend.find_by(id: params[:friend_id]) if party.attendents.exists?( friend_id: friend.id ) puts "gefunden" party.attendents.where(friend_id: friend.id).delete_all else puts "nicht gefunden" party.attendents.create(friend_id: friend.id) member = true end render turbo_stream: turbo_stream.replace( "party-#{party.id}-#{friend.id}", partial: 'parties/attendent', locals: { friend: friend, party: party, member: member } ) end private # Use callbacks to share common setup or constraints between actions. def set_party @party = Party.find(params.expect(:id)) end # Only allow a list of trusted parameters through. def party_params params.expect(party: [ :title, :location, :start_date, :start_time, :status ]) end end