parent
7e6d4e6215
commit
6d79c2cf83
@ -0,0 +1,22 @@
|
|||||||
|
#coding=utf-8
|
||||||
|
module Mobile
|
||||||
|
module Apis
|
||||||
|
class Comments < Grape::API
|
||||||
|
resource :comments do
|
||||||
|
desc '课程通知评论'
|
||||||
|
params do
|
||||||
|
requires :token, type: String
|
||||||
|
requires :comment, type: String
|
||||||
|
end
|
||||||
|
post ':id' do
|
||||||
|
cs = CommentService.new
|
||||||
|
comments = cs.news_comments params,current_user
|
||||||
|
raise "create comments failed #{comments.errors.full_messages}" if comments.new_record?
|
||||||
|
present :data, comments, with: Mobile::Entities::Comment
|
||||||
|
present :status, 0
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,30 @@
|
|||||||
|
module Mobile
|
||||||
|
module Entities
|
||||||
|
class Comment < Grape::Entity
|
||||||
|
include Redmine::I18n
|
||||||
|
def self.comment_expose(field)
|
||||||
|
expose field do |f,opt|
|
||||||
|
if f.is_a?(Hash) && f.key?(field)
|
||||||
|
f[field]
|
||||||
|
elsif f.is_a?(::Comment)
|
||||||
|
if f.respond_to?(field)
|
||||||
|
if field == :created_on
|
||||||
|
format_time(f.send(field))
|
||||||
|
else
|
||||||
|
f.send(field)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
comment_expose :id
|
||||||
|
expose :author, using: Mobile::Entities::User do |c, opt|
|
||||||
|
if c.is_a? ::Comment
|
||||||
|
c.author
|
||||||
|
end
|
||||||
|
end
|
||||||
|
comment_expose :comments
|
||||||
|
comment_expose :created_on
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,12 @@
|
|||||||
|
class CommentService
|
||||||
|
#评论
|
||||||
|
def news_comments params,current_user
|
||||||
|
raise Unauthorized unless @news.commentable?
|
||||||
|
@news = News.find(params[:id])
|
||||||
|
@comment = Comment.new
|
||||||
|
@comment.safe_attributes = params[:comment]
|
||||||
|
@comment.author = current_user
|
||||||
|
@news.comments << @comment
|
||||||
|
@comment
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue