<commit_before># -*- coding: UTF-8 -*-

module API
  module V1
    class Comments < Grape::API
      helpers do
        def set_book
          @book = Book.find(params[:book_id])
        end

        def comment_params
          ActionController::Parameters.new(params).permit(:body)
        end

        # Parameter check
        # :book_id
        params :book_id do
          requires :book_id, type: Integer, desc: "Book id."
        end

        # Parameter check
        # :attributes
        #   - :body
        params :attributes do
          requires :body, type: String, desc: "Comment body."
        end
      end
      resource :books do
        # common params
        params do
          use :book_id
        end

        route_param :book_id do
          resource :comments do
            desc 'GET /api/v1/books/:book_id/comments'
            get '/', jbuilder: 'api/v1/comments/index' do
              set_book
              @comments = @book.comments
            end

            desc 'POST /api/v1/books/:book_id/comments'
            params do
              use :attributes
            end
            post '/' do
              set_book
              @book.comments.create(comment_params)
            end
          end
        end
      end<commit_msg>add API PUT /api/v1/books/:book_id/comments/:id<commit_after># -*- coding: UTF-8 -*-

module API
  module V1
    class Comments < Grape::API
      helpers do
        def set_book
          @book = Book.find(params[:book_id])
        end

        def set_comment
          @comment = Comment.find(params[:id])
        end

        def comment_params
          ActionController::Parameters.new(params).permit(:body)
        end

        # Parameter check
        # :book_id
        params :book_id do
          requires :book_id, type: Integer, desc: "Book id."
        end

        # Parameter check
        # :id
        params :id do
          requires :id, type: Integer, desc: "Comment id."
        end

        # Parameter check
        # :attributes
        #   - :body
        params :attributes do
          requires :body, type: String, desc: "Comment body."
        end
      end
      resource :books do
        # common params
        params do
          use :book_id
        end

        route_param :book_id do
          resource :comments do
            desc 'GET /api/v1/books/:book_id/comments'
            get '/', jbuilder: 'api/v1/comments/index' do
              set_book
              @comments = @book.comments
            end

            desc 'POST /api/v1/books/:book_id/comments'
            params do
              use :attributes
            end
            post '/' do
              set_book
              @book.comments.create(comment_params)
            end

            desc 'PUT /api/v1/books/:book_id/comments/:id'
            params do
              use :id
              use :attributes
            end
            put '/:id' do
              set_comment
              @comment.update(comment_params)
            end
          end
        end
      end
{'fraction_non_alphanumeric': 0.08300653594771241, 'fraction_numerical': 0.00392156862745098, 'mean_word_length': 1.262379896526238, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1177799', 'n_tokens_mistral': 832, 'n_tokens_neox': 822, 'n_words': 225}