summaryrefslogtreecommitdiff
path: root/app/controllers/api/v1/cakes_controller.rb
blob: 8888d286f2e89703abeaed0778c5c7c9d6fe560d (plain)
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
module Api
  module V1
    class CakesController < ApiController
      def index
        @cakes = current_user.creations
      end

      def show
        @cake = current_user.creations.find(params[:id])
      end

      def create
        name = cake_params[:name]
        category = Category.find(cake_params[:category_id])
        @cake = current_user.create_cake(name: name, category: category)
        if @cake.save
          one_hour = 1.hour.from_now
          PublishToTwitterJob.set(wait_until: one_hour).perform_later(@cake)
        end
      end

      def update
        @cake = current_user.creations.find(params[:id])
        current_user.tag(@cake, with: params[:cake][:tags], on: :tags)
        @cake.update!(cake_params.reject { |key, _| key == "tags" })
      end

      def destroy
        @cake = current_user.creations.find(params[:id])
        @cake.destroy!
      end

      private

      def cake_params
        params.require(:cake).permit(:name, :story, :category_id, :tags)
      end
    end
  end
end