app/controllers/releases_controller.rb
# frozen_string_literal: true
class ReleasesController < ApplicationController
before_action :authenticate_user!, except: %i[show]
swagger_controller :releases, 'Release Management'
swagger_api :show do
summary 'View Release Details'
param :path, :user_id, :string, :required, 'Name of the user'
param :path, :id, :string, :required, 'Release slug or ID'
response :ok, 'The Release Details'
response :not_found, 'when release cannot be found'
end
def show
@user = User.find_by(name: params[:user_id])
@release = @user.releases.find(params[:id])
@title = "#{@release.name} - #{@user.display_name}"
end
def new
@release = current_user.releases.build
@released_track = @release.released_tracks.build
@track = @released_track.build_track
end
def edit
@release = current_user.releases.find(params[:id])
end
swagger_api :create do
summary 'Create a New Release'
param :form, 'release[name]', :string, :required, 'Name of the release'
param :form, 'release[description]', :string, :optional, 'Description'
param :form, 'release[released_tracks_attributes][number]', :string, :optional, 'Track Number'
param :form, 'release[released_tracks_attributes][track_attributes][name]', :string, :optional, 'Track Name'
param :form, 'release[released_tracks_attributes][track_attributes][description]', :string, :optional, 'Track Description'
param :form, 'release[released_tracks_attributes][track_attributes][downloadable]', :boolean, :optional, 'Downloadable?'
end
def create
@release = current_user.releases.build(create_params)
respond_to do |format|
if @release.save
flash[:notice] = t('.success', name: @release.name)
format.html { redirect_back fallback_location: [@release.user, @release] }
format.json { render json: @release, status: :created }
else
errors = @release.errors.full_messages.to_sentence
flash[:alert] = t('.failure', errors: errors)
format.html { render :new }
format.json { render json: @release.errors, status: :unprocessable_entity }
end
end
end
swagger_api :update do
summary 'Edit an existing release'
param :form, 'release[name]', :string, :required, 'Name of the release'
param :form, 'release[description]', :string, :optional, 'Description'
param :form, 'release[released_tracks_attributes][number]', :string, :optional, 'Track Number'
param :form, 'release[released_tracks_attributes][_destroy]', :boolean, :optional, 'Remove this Track from Release?'
param :form, 'release[released_tracks_attributes][track_attributes][name]', :string, :optional, 'Track Name'
param :form, 'release[released_tracks_attributes][track_attributes][description]', :string, :optional, 'Track Description'
param :form, 'release[released_tracks_attributes][track_attributes][downloadable]', :boolean, :optional, 'Downloadable?'
param :path, :id, :string, :required, 'ID of the Release'
end
def update
@release = current_user.releases.find(params[:id])
respond_to do |format|
if @release.update(update_params)
flash[:notice] = t('.success', name: @release.name)
format.html { redirect_back fallback_location: [@release.user, @release] }
format.json { render json: @release, status: :created }
else
errors = @release.errors.full_messages.to_sentence
flash[:alert] = t('.failure', errors: errors)
format.html { render :new }
format.json { render json: @release.errors, status: :unprocessable_entity }
end
end
end
swagger_api :destroy do
summary 'Destroy an existing release'
param :path, :id, :string, :required, 'ID of the Release'
response :ok, 'when release is destroyed'
response :unprocessable_entity, 'when a problem occurs'
end
def destroy
@release = current_user.releases.find(params[:id])
respond_to do |format|
if @release.destroy
flash[:notice] = t('.success', name: @release.name)
format.html { redirect_back fallback_location: @release.user }
format.json { render json: @release, status: :ok }
else
errors = @release.errors.full_messages.to_sentence
flash[:alert] = t('.failure', errors: errors)
format.html { render :new }
format.json { render json: @release.errors, status: :unprocessable_entity }
end
end
end
private
def create_params
params.require(:release).permit(
:name,
:description,
released_tracks_attributes: [
:number,
:_destroy,
track_attributes: %i[name audio description user_id]
]
)
end
def update_params
params.require(:release).permit(
:name,
:description,
released_tracks_attributes: [
:id,
:number,
:_destroy,
track_attributes: %i[name user_id description]
]
)
end
end