ぼざクリタグ広場 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.
 
 
 
 
 
 

95 lines
2.7 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: materials.map { |m| material_json(m) }, count: count }
  16. end
  17. def show
  18. material = Material.includes(:tag, :created_by_user).with_attached_file.find_by(id: params[:id])
  19. return head :not_found unless material
  20. render json: material_json(material)
  21. end
  22. def create
  23. return head :unauthorized unless current_user
  24. return head :forbidden unless current_user.gte_member?
  25. file = params[:file]
  26. return head :bad_request if file.blank?
  27. material = Material.new(
  28. url: params[:url].presence,
  29. parent_id: params[:parent_id].presence,
  30. tag_id: params[:tag_id].presence,
  31. created_by_user: current_user)
  32. material.file.attach(file)
  33. if material.save
  34. render json: material_json(material), status: :created
  35. else
  36. render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
  37. end
  38. end
  39. def update
  40. return head :unauthorized unless current_user
  41. return head :forbidden unless current_user.gte_member?
  42. material = Material.with_attached_file.find_by(id: params[:id])
  43. return head :not_found unless material
  44. material.assign_attributes(
  45. url: params[:url].presence,
  46. parent_id: params[:parent_id].presence,
  47. tag_id: params[:tag_id].presence
  48. )
  49. material.file.attach(params[:file]) if params[:file].present?
  50. if material.save
  51. render json: material_json(material)
  52. else
  53. render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
  54. end
  55. end
  56. def destroy
  57. return head :unauthorized unless current_user
  58. return head :forbidden unless current_user.gte_member?
  59. material = Material.find_by(id: params[:id])
  60. return head :not_found unless material
  61. material.discard
  62. head :no_content
  63. end
  64. private
  65. def material_json(material)
  66. MaterialRepr.base(material).merge(
  67. 'filename' => material.file.attached? ? material.file.filename.to_s : nil,
  68. 'byte_size' => material.file.attached? ? material.file.byte_size : nil,
  69. 'content_type' => material.file.attached? ? material.file.content_type : nil,
  70. 'url' => material.file.attached? ? url_for(material.file) : nil
  71. )
  72. end
  73. end