File size: 4,071 Bytes
397e650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
module Api
  module V1
    class AuctionsController < ApplicationController
      include JwtAuthenticatable

      skip_before_action :verify_authenticity_token
      before_action :authorize_request, except: [ :index, :show ]
      before_action :set_auction, only: [ :show, :update, :destroy ]

      def index
        @auctions = Auction.includes(:category, :submitted_by_user, images_attachments: :blob)
                           .order(created_at: :desc)
                           .page(params[:page])
                           .per(params[:per_page] || 20)

        render json: {
          auctions: @auctions.map { |auction| auction_json(auction) },
          meta: pagination_meta(@auctions)
        }, status: :ok
      end

      def show
        render json: auction_json(@auction, include_opinions: true), status: :ok
      end

      def create
        @auction = @current_user.auctions.build(auction_params)

        if @auction.save
          render json: auction_json(@auction), status: :created
        else
          render json: { errors: @auction.errors.full_messages }, status: :unprocessable_entity
        end
      end

      def update
        unless @auction.submitted_by_user_id == @current_user.id || @current_user.admin?
          render json: { error: "Unauthorized" }, status: :forbidden
          return
        end

        if @auction.update(auction_params)
          render json: auction_json(@auction), status: :ok
        else
          render json: { errors: @auction.errors.full_messages }, status: :unprocessable_entity
        end
      end

      def destroy
        unless @auction.submitted_by_user_id == @current_user.id || @current_user.admin?
          render json: { error: "Unauthorized" }, status: :forbidden
          return
        end

        @auction.destroy
        head :no_content
      end

      private

      def set_auction
        @auction = Auction.includes(:category, :submitted_by_user, :opinions, images_attachments: :blob).find(params[:id])
      rescue ActiveRecord::RecordNotFound
        render json: { error: "Auction not found" }, status: :not_found
      end

      def auction_params
        params.require(:auction).permit(:title, :description_text, :price, :currency, :external_link, :category_id, images: [])
      end

      def auction_json(auction, include_opinions: false)
        json = {
          id: auction.id,
          title: auction.title,
          description: auction.description_text,
          price: auction.price,
          currency: auction.currency,
          external_link: auction.external_link,
          verification_status: auction.verification_status,
          ai_score_authenticity: auction.ai_score_authenticity,
          ai_uncertainty_message: auction.ai_uncertainty_message,
          category: {
            id: auction.category.id,
            name: auction.category.name
          },
          submitted_by: {
            id: auction.submitted_by_user.id,
            username: auction.submitted_by_user.username
          },
          images: auction.images.map { |img| rails_blob_url(img) },
          created_at: auction.created_at,
          updated_at: auction.updated_at
        }

        if include_opinions
          json[:opinions] = auction.opinions.includes(:user).map do |opinion|
            {
              id: opinion.id,
              content: opinion.content,
              verdict: opinion.verdict,
              author_type: opinion.author_type,
              score: opinion.score,
              user: {
                id: opinion.user.id,
                username: opinion.user.username
              },
              created_at: opinion.created_at
            }
          end
        end

        json
      end

      def pagination_meta(collection)
        {
          current_page: collection.current_page,
          next_page: collection.next_page,
          prev_page: collection.prev_page,
          total_pages: collection.total_pages,
          total_count: collection.total_count
        }
      end
    end
  end
end