|
| 1 | +module AP |
| 2 | + class Web::PostsAPI < Grape::API |
| 3 | + |
| 4 | + helpers do |
| 5 | + def format_post(post) |
| 6 | + { |
| 7 | + id: post.id, |
| 8 | + title: post.title, |
| 9 | + body: post.body, |
| 10 | + total_view: post.total_view, |
| 11 | + total_feedback: post.feedbacks.count, |
| 12 | + created_at: post.created_at.to_fs(:ymd_hms) |
| 13 | + } |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + resource :posts do |
| 18 | + |
| 19 | + desc 'Get a list of posts by limit' |
| 20 | + params do |
| 21 | + optional :limit, type: Integer, default: 10, desc: 'Limit number of posts' |
| 22 | + end |
| 23 | + get do |
| 24 | + posts = Post.approved.limit(params[:limit]).map { |post| format_post(post) } |
| 25 | + status 200 |
| 26 | + { |
| 27 | + success: true, |
| 28 | + posts: posts |
| 29 | + } |
| 30 | + end |
| 31 | + |
| 32 | + desc 'Get a list of posts by newest' |
| 33 | + params do |
| 34 | + optional :limit, type: Integer, default: 10, desc: 'Limit number of posts' |
| 35 | + end |
| 36 | + get '/newest' do |
| 37 | + posts = Post.approved.newest.limit(params[:limit]).map { |post| format_post(post) } |
| 38 | + status 200 |
| 39 | + { |
| 40 | + success: true, |
| 41 | + posts: posts |
| 42 | + } |
| 43 | + end |
| 44 | + |
| 45 | + desc 'Get a list of posts by most viewed' |
| 46 | + params do |
| 47 | + optional :limit, type: Integer, default: 10, desc: 'Limit number of posts' |
| 48 | + end |
| 49 | + get '/most_viewed' do |
| 50 | + posts = Post.approved.most_viewed.limit(params[:limit]).map { |post| format_post(post) } |
| 51 | + status 200 |
| 52 | + { |
| 53 | + success: true, |
| 54 | + posts: posts |
| 55 | + } |
| 56 | + end |
| 57 | + |
| 58 | + desc 'Get a post' |
| 59 | + params do |
| 60 | + requires :id, type: Integer, desc: 'Post id' |
| 61 | + end |
| 62 | + get ':id' do |
| 63 | + post = Post.find_by(id: params[:id]) |
| 64 | + if post |
| 65 | + post.update(total_view: post.total_view + 1) |
| 66 | + status 200 |
| 67 | + { |
| 68 | + success: true, |
| 69 | + post: format_post(post) |
| 70 | + } |
| 71 | + else |
| 72 | + status 400 |
| 73 | + { |
| 74 | + success: false, |
| 75 | + error: 'Post not found' |
| 76 | + } |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + desc 'Create a post' |
| 81 | + params do |
| 82 | + requires :title, type: String, desc: 'Post title', allow_blank: false |
| 83 | + requires :body, type: String, desc: 'Post body', allow_blank: false |
| 84 | + end |
| 85 | + post do |
| 86 | + authenticate_user! |
| 87 | + if current_user.admin? |
| 88 | + post = current_user.posts.new({ |
| 89 | + title: params[:title], |
| 90 | + body: params[:body], |
| 91 | + status: 'approved' |
| 92 | + }) |
| 93 | + end |
| 94 | + |
| 95 | + if post.save |
| 96 | + status 200 |
| 97 | + { |
| 98 | + success: true, |
| 99 | + message: 'Post created successfully' |
| 100 | + } |
| 101 | + else |
| 102 | + status 400 |
| 103 | + { |
| 104 | + success: false, |
| 105 | + error: 'Post cannot be created' |
| 106 | + } |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + desc 'Update a post' |
| 111 | + params do |
| 112 | + requires :id, type: Integer, desc: 'Post id' |
| 113 | + optional :new_title, type: String, desc: 'New post title' |
| 114 | + optional :new_body, type: String, desc: 'New post body' |
| 115 | + end |
| 116 | + put ':id' do |
| 117 | + authenticate_user! |
| 118 | + post = current_user.posts.find_by(id: params[:id]) |
| 119 | + if post |
| 120 | + post.update({ |
| 121 | + title: params[:new_title], |
| 122 | + body: params[:new_body] |
| 123 | + }) |
| 124 | + status 200 |
| 125 | + { |
| 126 | + success: true, |
| 127 | + message: 'Post updated successfully' |
| 128 | + } |
| 129 | + else |
| 130 | + status 400 |
| 131 | + { |
| 132 | + success: false, |
| 133 | + error: 'Post cannot be updated' |
| 134 | + } |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + desc 'Delete a post' |
| 139 | + params do |
| 140 | + requires :id, type: Integer, desc: 'Post id' |
| 141 | + end |
| 142 | + delete ':id' do |
| 143 | + authenticate_user! |
| 144 | + if current_user.admin? |
| 145 | + post = Post.find_by(id: params[:id]) |
| 146 | + else |
| 147 | + post = current_user.posts.find_by(id: params[:id]) |
| 148 | + end |
| 149 | + |
| 150 | + if post |
| 151 | + post.update(status: 'deleted') |
| 152 | + status 200 |
| 153 | + { |
| 154 | + success: true, |
| 155 | + message: 'Post deleted successfully' |
| 156 | + } |
| 157 | + else |
| 158 | + status 400 |
| 159 | + { |
| 160 | + success: false, |
| 161 | + error: 'Post cannot be deleted' |
| 162 | + } |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + desc 'List of posts by user' |
| 167 | + params do |
| 168 | + requires :id, type: Integer, desc: 'User id' |
| 169 | + end |
| 170 | + get '/user/:id' do |
| 171 | + status 200 |
| 172 | + { |
| 173 | + success: true, |
| 174 | + posts: current_user.posts.approved.map { |post| format_post(post) } |
| 175 | + } |
| 176 | + end |
| 177 | + |
| 178 | + desc 'Approve a post' |
| 179 | + params do |
| 180 | + requires :id, type: Integer, desc: 'Post id' |
| 181 | + end |
| 182 | + put '/approve/:id' do |
| 183 | + authenticate_user! |
| 184 | + if current_user.admin? |
| 185 | + post = Post.find_by(id: params[:id]) |
| 186 | + if post |
| 187 | + post.update(status: 'approved') |
| 188 | + status 200 |
| 189 | + { |
| 190 | + success: true, |
| 191 | + message: 'Post approved successfully' |
| 192 | + } |
| 193 | + end |
| 194 | + else |
| 195 | + status 400 |
| 196 | + { |
| 197 | + success: false, |
| 198 | + error: 'You are not authorized' |
| 199 | + } |
| 200 | + end |
| 201 | + end |
| 202 | + |
| 203 | + desc 'Get a list feedbacks of a post' |
| 204 | + params do |
| 205 | + requires :id, type: Integer, desc: 'Post id' |
| 206 | + end |
| 207 | + get '/:id/feedbacks' do |
| 208 | + feedbacks = Post.find_by(id: params[:id])&.feedbacks |
| 209 | + return status 400 unless feedbacks |
| 210 | + status 200 |
| 211 | + { |
| 212 | + success: true, |
| 213 | + feedbacks: feedbacks.map do |feedback| |
| 214 | + { |
| 215 | + id: feedback.id, |
| 216 | + user_name: feedback.user.name, |
| 217 | + content: feedback.content, |
| 218 | + total_like: feedback.total_likes, |
| 219 | + created_at: feedback.created_at.to_fs(:ymd_hms) |
| 220 | + } |
| 221 | + end |
| 222 | + } |
| 223 | + end |
| 224 | + end |
| 225 | + end |
| 226 | +end |
0 commit comments