master
 1module Api
 2  module V1
 3    class CakesController < ApiController
 4      def index
 5        @cakes = current_user.creations.includes(:category, :photos, :tags, :tag_taggings)
 6      end
 7
 8      def show
 9        @cake = current_user.creations.find(params[:id])
10      end
11
12      def create
13        name = cake_params[:name]
14        category = Category.find(cake_params[:category_id])
15        @cake = current_user.create_cake(name: name, category: category)
16        if @cake.save
17          one_hour = 1.hour.from_now
18          PublishToTwitterJob.set(wait_until: one_hour).perform_later(@cake)
19        end
20      end
21
22      def update
23        @cake = current_user.creations.find(params[:id])
24        current_user.tag(@cake, with: params[:cake][:tags], on: :tags)
25        @cake.update!(cake_params.reject { |key, _| key == "tags" })
26      end
27
28      def destroy
29        @cake = current_user.creations.find(params[:id])
30        @cake.destroy!
31      end
32
33      private
34
35      def cake_params
36        params.require(:cake).permit(:name, :story, :category_id, :tags)
37      end
38    end
39  end
40end