Class: Railspress::Api::V1::PostsController

Inherits:
BaseController
  • Object
show all
Includes:
Concerns::PostSerialization
Defined in:
app/controllers/railspress/api/v1/posts_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_key

Instance Method Summary collapse

Instance Method Details

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/railspress/api/v1/posts_controller.rb', line 32

def create
  post = Railspress::Post.new(post_params.except(:header_image_signed_blob_id))
  attach_header_image_from_signed_blob(post, post_params[:header_image_signed_blob_id])

  if post.save
    render json: { data: serialize_post(post) }, status: :created
  else
    render_validation_errors(post)
  end
rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveRecord::RecordNotFound
  post.errors.add(:header_image, "signed blob id is invalid")
  render_validation_errors(post)
end

#destroyObject



60
61
62
63
# File 'app/controllers/railspress/api/v1/posts_controller.rb', line 60

def destroy
  @post.destroy
  head :no_content
end

#indexObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/railspress/api/v1/posts_controller.rb', line 11

def index
  posts = Railspress::Post.includes(:category, :tags).sorted_by(sort_column, sort_direction)
  total_count = posts.count

  posts = posts.offset((page - 1) * per_page).limit(per_page)

  render json: {
    data: posts.map { |post| serialize_post(post) },
    meta: {
      page: page,
      per: per_page,
      total_count: total_count,
      total_pages: (total_count.to_f / per_page).ceil
    }
  }
end

#showObject



28
29
30
# File 'app/controllers/railspress/api/v1/posts_controller.rb', line 28

def show
  render json: { data: serialize_post(@post) }
end

#updateObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/railspress/api/v1/posts_controller.rb', line 46

def update
  @post.assign_attributes(post_params.except(:header_image_signed_blob_id))
  attach_header_image_from_signed_blob(@post, post_params[:header_image_signed_blob_id])

  if @post.save
    render json: { data: serialize_post(@post) }
  else
    render_validation_errors(@post)
  end
rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveRecord::RecordNotFound
  @post.errors.add(:header_image, "signed blob id is invalid")
  render_validation_errors(@post)
end