ぼざクリタグ広場 https://hub.nizika.monster
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.
 
 
 
 
 
 

100 lines
2.8 KiB

  1. class MaterialsController < ApplicationController
  2. def index
  3. page = (params[:page].presence || 1).to_i
  4. limit = (params[:limit].presence || 20).to_i
  5. page = 1 if page < 1
  6. limit = 1 if limit < 1
  7. offset = (page - 1) * limit
  8. tag_id = params[:tag_id].presence
  9. parent_id = params[:parent_id].presence
  10. q = Material.includes(:tag, :created_by_user).with_attached_file
  11. q = q.where(tag_id:) if tag_id
  12. q = q.where(parent_id:) if parent_id
  13. count = q.count
  14. materials = q.order(created_at: :desc, id: :desc).limit(limit).offset(offset)
  15. render json: { materials: MaterialRepr.many(materials), count: count }
  16. end
  17. def show
  18. material =
  19. Material
  20. .includes(:tag)
  21. .with_attached_file
  22. .find_by(id: params[:id])
  23. return head :not_found unless material
  24. render json: MaterialRepr.base(material)
  25. end
  26. def create
  27. return head :unauthorized unless current_user
  28. tag_name_raw = params[:tag].to_s.strip
  29. file = params[:file]
  30. url = params[:url].to_s.strip.presence
  31. return head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
  32. tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
  33. tag = tag_name.tag
  34. tag = Tag.create!(tag_name:, category: :material) unless tag
  35. material = Material.new(tag:, url:,
  36. created_by_user: current_user,
  37. updated_by_user: current_user)
  38. material.file.attach(file)
  39. if material.save
  40. render json: MaterialRepr.base(material), status: :created
  41. else
  42. render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
  43. end
  44. end
  45. def update
  46. return head :unauthorized unless current_user
  47. return head :forbidden unless current_user.gte_member?
  48. material = Material.with_attached_file.find_by(id: params[:id])
  49. return head :not_found unless material
  50. tag_name_raw = params[:tag].to_s.strip
  51. file = params[:file]
  52. url = params[:url].to_s.strip.presence
  53. return head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
  54. tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
  55. tag = tag_name.tag
  56. tag = Tag.create!(tag_name:, category: :material) unless tag
  57. material.update!(tag:, url:, updated_by_user: current_user)
  58. if file
  59. material.file.attach(file)
  60. else
  61. material.file.purge
  62. end
  63. if material.save
  64. render json: MaterialRepr.base(material)
  65. else
  66. render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
  67. end
  68. end
  69. def destroy
  70. return head :unauthorized unless current_user
  71. return head :forbidden unless current_user.gte_member?
  72. material = Material.find_by(id: params[:id])
  73. return head :not_found unless material
  74. material.discard
  75. head :no_content
  76. end
  77. end