手机端API添加新闻评论实体Comment,新闻实体中引用Comment,课程新闻列表添加权限判断,并修正该接口不可用问题
This commit is contained in:
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
|
|
@ -188,10 +188,11 @@ module Mobile
|
||||||
|
|
||||||
desc "课程通知列表"
|
desc "课程通知列表"
|
||||||
params do
|
params do
|
||||||
|
optional :token, type: String
|
||||||
end
|
end
|
||||||
get ":course_id/news" do
|
get ":course_id/news" do
|
||||||
cs = CoursesService.new
|
cs = CoursesService.new
|
||||||
news = cs.course_news_list params
|
news = cs.course_news_list params,current_user.nil? ? User.find(2):current_user
|
||||||
present :data, news, with: Mobile::Entities::News
|
present :data, news, with: Mobile::Entities::News
|
||||||
present :status, 0
|
present :status, 0
|
||||||
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
|
|
@ -34,7 +34,11 @@ module Mobile
|
||||||
#评论数量
|
#评论数量
|
||||||
news_expose :comments_count
|
news_expose :comments_count
|
||||||
#评论
|
#评论
|
||||||
news_expose :comments
|
expose :comments, using: Mobile::Entities::Comment do |f, opt|
|
||||||
|
if f.is_a?(Hash) && f.key?(:comments)
|
||||||
|
f[:comments]
|
||||||
|
end
|
||||||
|
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
|
|
@ -106,11 +106,14 @@ class CoursesService
|
||||||
end
|
end
|
||||||
|
|
||||||
#课程通知列表
|
#课程通知列表
|
||||||
def course_news_list params
|
def course_news_list params,current_user
|
||||||
if params[:course_id] && @course==nil
|
if params[:course_id] && @course==nil
|
||||||
@course = Course.find(params[:course_id])
|
@course = Course.find(params[:course_id])
|
||||||
end
|
end
|
||||||
scope = @course ? @course.news.course_visible : News.course_visible
|
if current_user.nil? || !(current_user.admin? || @course.is_public == 1 || (@course.is_public == 0 && current_user.member_of_course?(@course)))
|
||||||
|
raise '403'
|
||||||
|
end
|
||||||
|
scope = @course ? @course.news.course_visible(current_user) : News.course_visible(current_user)
|
||||||
news = []
|
news = []
|
||||||
scope.each do |n|
|
scope.each do |n|
|
||||||
news << {:title => n.title,:author_name => n.author.name,:author_id => n.author.id, :description => n.description,:created_on => format_time(n.created_on),:comments_count => n.comments_count}
|
news << {:title => n.title,:author_name => n.author.name,:author_id => n.author.id, :description => n.description,:created_on => format_time(n.created_on),:comments_count => n.comments_count}
|
||||||
|
|
Loading…
Reference in New Issue