/
tofflin
/
music_project
Обзор
Документация
Войти
/
tofflin
/
music_project
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
app/controllers/profile_controller.rb
100 строк
3 KB
Dmitriy
Закончил обновление пользователя
16 июн 2025, 14:59
16 июн 2025, 14:59
53e494e
Код
Авторство
О чём код?
class ProfileController < ApplicationController include UserSerialization include UserSetup before_action :authenticate_user! before_action :set_user, only: %i[ show followers following ] include Rails.application.routes.url_helpers def show avatar_url = @user.avatar.attached? ? url_for(@user.avatar) : nil banner_url = @user.banner.attached? ? url_for(@user.banner) : nil render json: { user: { id: @user.id, nickname: @user.nickname, first_name: @user.first_name, last_name: @user.last_name, country: @user.country, city: @user.city, bio: @user.bio, avatar_url: avatar_url, banner_url: banner_url, subscribers_count: @user.subscribers_count, subscriptions_count: @user.subscriptions_count, is_current_user: @user == current_user, is_subscribed: current_user.subscribed?(@user) } }, status: :ok end def update if current_user.update(profile_params) head :no_content else render_validation_errors(current_user) end end def update_avatar if params[:avatar].present? current_user.avatar.attach(params[:avatar]) if current_user.avatar.attached? render json: { avatar_url: url_for(current_user.avatar) }, status: :ok else render json: { error: "Failed to attach avatar" }, status: :unprocessable_entity end else render json: { error: "No avatar provided" }, status: :bad_request end end def update_banner if params[:banner].present? current_user.banner.attach(params[:banner]) if current_user.banner.attached? render json: { banner_url: url_for(current_user.banner) }, status: :ok else render json: { error: "Failed to attach banner" }, status: :unprocessable_entity end else render json: { error: "No banner provided" }, status: :bad_request end end def followers followers = @user.subscribers render json: { followers: serialize_users(followers), followers_count: followers.count, current_user_subscribed: current_user.subscribed?(@user) }, status: :ok end def following following = @user.subscribed_to_users render json: { following: serialize_users(following), following_count: following.count }, status: :ok end private def profile_params params.require(:user).permit(:nickname, :first_name, :last_name, :country, :city, :bio) end def render_validation_errors(resource) errors = resource.errors.entries.each_with_object({}) do |(attr, message), hash| hash[attr] ||= [] if attr == :nickname && message == "has already been taken" hash[attr] << "This nickname is already taken" else hash[attr] << message end end render json: { errors: errors }, status: :unprocessable_entity end end