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

104 lines
3.1 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, host: request.base_url), 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. wiki_page_body = material.tag.tag_name.wiki_page&.current_revision&.body
  25. render json: MaterialRepr.base(material, host: request.base_url).merge(wiki_page_body:)
  26. end
  27. def create
  28. return head :unauthorized unless current_user
  29. tag_name_raw = params[:tag].to_s.strip
  30. file = params[:file]
  31. url = params[:url].to_s.strip.presence
  32. return render_bad_request('タグは必須です.', field: :tag) if tag_name_raw.blank?
  33. return render_bad_request('ファイルまたは URL は必須です.') if file.blank? && url.blank?
  34. tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
  35. tag = tag_name.tag
  36. tag = Tag.create!(tag_name:, category: :material) unless tag
  37. material = Material.new(tag:, url:,
  38. created_by_user: current_user,
  39. updated_by_user: current_user)
  40. material.file.attach(file)
  41. if material.save
  42. render json: MaterialRepr.base(material, host: request.base_url), status: :created
  43. else
  44. render_model_errors(material)
  45. end
  46. end
  47. def update
  48. return head :unauthorized unless current_user
  49. return head :forbidden unless current_user.gte_member?
  50. material = Material.with_attached_file.find_by(id: params[:id])
  51. return head :not_found unless material
  52. tag_name_raw = params[:tag].to_s.strip
  53. file = params[:file]
  54. url = params[:url].to_s.strip.presence
  55. return render_bad_request('タグは必須です.', field: :tag) if tag_name_raw.blank?
  56. return render_bad_request('ファイルまたは URL は必須です.') if file.blank? && url.blank?
  57. tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
  58. tag = tag_name.tag
  59. tag = Tag.create!(tag_name:, category: :material) unless tag
  60. material.update!(tag:, url:, updated_by_user: current_user)
  61. if file
  62. material.file.attach(file)
  63. else
  64. material.file.purge
  65. end
  66. if material.save
  67. render json: MaterialRepr.base(material, host: request.base_url)
  68. else
  69. render_model_errors(material)
  70. end
  71. end
  72. def destroy
  73. return head :unauthorized unless current_user
  74. return head :forbidden unless current_user.gte_member?
  75. material = Material.find_by(id: params[:id])
  76. return head :not_found unless material
  77. material.discard
  78. head :no_content
  79. end
  80. end