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

102 lines
3.0 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 head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
  33. tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
  34. tag = tag_name.tag
  35. tag = Tag.create!(tag_name:, category: :material) unless tag
  36. material = Material.new(tag:, url:,
  37. created_by_user: current_user,
  38. updated_by_user: current_user)
  39. material.file.attach(file)
  40. if material.save
  41. render json: MaterialRepr.base(material, host: request.base_url), status: :created
  42. else
  43. render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
  44. end
  45. end
  46. def update
  47. return head :unauthorized unless current_user
  48. return head :forbidden unless current_user.gte_member?
  49. material = Material.with_attached_file.find_by(id: params[:id])
  50. return head :not_found unless material
  51. tag_name_raw = params[:tag].to_s.strip
  52. file = params[:file]
  53. url = params[:url].to_s.strip.presence
  54. return head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
  55. tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
  56. tag = tag_name.tag
  57. tag = Tag.create!(tag_name:, category: :material) unless tag
  58. material.update!(tag:, url:, updated_by_user: current_user)
  59. if file
  60. material.file.attach(file)
  61. else
  62. material.file.purge
  63. end
  64. if material.save
  65. render json: MaterialRepr.base(material, host: request.base_url)
  66. else
  67. render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
  68. end
  69. end
  70. def destroy
  71. return head :unauthorized unless current_user
  72. return head :forbidden unless current_user.gte_member?
  73. material = Material.find_by(id: params[:id])
  74. return head :not_found unless material
  75. material.discard
  76. head :no_content
  77. end
  78. end