• 评论中心的实现
• 评论中心
需要评论的各种场景服务:
评论中心的设计:
统一的评论服务,覆盖所有的实体,可以进行评论
通用的模块开发流程
1. Database Column
2. Model:模型定义,和数据库相匹配
3. DAO:数据读取
4. Service:服务包装
5. Controller:业务入口
6. Test
站内信开发流程
1. Database Column
id
content
entity_id ->questionId/commentId
//表示任意实体的id;
entity_type question/comment
//表示评论的对象
created_date
user_id
2. Model:模型定义,和数据库相匹配
package
com.nowcoder.model
;
import
java.util.Date
;
/**
*
@Author
liguo
*
@Description
*
@Data
2018-09-06 7:50
*/
public class
Comment
{
private int
id
;
private int
userId
;
private int
entityId
;
private int
entityType
;
private
String
content
;
private
Date
createdDate
;
private int
status
;
public int
getId
()
{
return
id
;
}
public void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public int
getUserId
()
{
return
userId
;
}
public void
setUserId
(
int
userId
)
{
this
.
userId
=
userId
;
}
public int
getEntityId
()
{
return
entityId
;
}
public void
setEntityId
(
int
entityId
)
{
this
.
entityId
=
entityId
;
}
public int
getEntityType
()
{
return
entityType
;
}
public void
setEntityType
(
int
entityType
)
{
this
.
entityType
=
entityType
;
}
public
String
getContent
()
{
return
content
;
}
public void
setContent
(
String
content
)
{
this
.
content
=
content
;
}
public
Date
getCreatedDate
()
{
return
createdDate
;
}
public void
setCreatedDate
(
Date
createdDate
)
{
this
.
createdDate
=
createdDate
;
}
public int
getStatus
()
{
return
status
;
}
public void
setStatus
(
int
status
)
{
this
.
status
=
status
;
}
}
3. DAO:数据读取
//数据库接口的crud
@Mapper
public interface
CommentDAO
{
String
TABLE_NAME
=
" comment "
;
String
INSERT_FIELDS
=
" user_id, content, created_date, entity_id, entity_type, status "
;
String
SELECT_FIELDS
=
" id, "
+
INSERT_FIELDS
;
@Insert
(
{
"insert into "
,
TABLE_NAME
,
"("
,
INSERT_FIELDS
,
") values (#{userId},#{content},#{createdDate},#{entityId},#{entityType},#{status})"
}
)
int
addComment
(
Comment
comment
)
;
@Select
(
{
"select "
,
SELECT_FIELDS
,
" from "
,
TABLE_NAME
,
" where id=#{id}"
}
)
Comment
getCommentById
(
int
id
)
;
@Select
(
{
"select "
,
SELECT_FIELDS
,
" from "
,
TABLE_NAME
,
" where entity_id=#{entityId} and entity_type=#{entityType} order by created_date desc"
}
)
List
<
Comment
>
selectCommentByEntity
(
@Param
(
"entityId"
)
int
entityId
,
@Param
(
"entityType"
)
int
entityType
)
;
@Select
(
{
"select count(id) from "
,
TABLE_NAME
,
" where entity_id=#{entityId} and entity_type=#{entityType}"
}
)
int
getCommentCount
(
@Param
(
"entityId"
)
int
entityId
,
@Param
(
"entityType"
)
int
entityType
)
;
@Update
(
{
"update comment set status=#{status} where id=#{id}"
}
)
int
updateStatus
(
@Param
(
"id"
)
int
id
,
@Param
(
"status"
)
int
status
)
;
@Select
(
{
"select count(id) from "
,
TABLE_NAME
,
" where user_id=#{userId}"
}
)
int
getUserCommentCount
(
int
userId
)
;
4. Service:服务包装
package
com.LG.service
;
import
com.LG.dao.
CommentDAO
;
import
org.springframework.beans.factory.annotation.
Autowired
;
import
org.springframework.stereotype.
Service
;
import
org.springframework.web.util.
HtmlUtils
;
import
java.util.
List
;
/**
*
@Author
liguo
*
@Description
*
@Data
2018-09-06 8:54
*/
@Service
public class
CommentService
{
@Autowired
CommentDAO
commentDAO
;
@Autowired
SensitiveService
sensitiveService
;
public
List
<
Comment
>
getCommentsByEntity
(
int
entityId
,
int
entityType
)
{
return
commentDAO
.
selectCommentByEntity
(
entityId
,
entityType
)
;
}
//过滤敏感词后进行插入
public int
addComment
(
Comment
comment
)
{
comment
.
setContent
(
HtmlUtils
.
htmlEscape
(
comment
.
getContent
() ) )
;
comment
.
setContent
(
sensitiveService
.
filter
(
comment
.
getContent
() ) )
;
return
commentDAO
.
addComment
(
comment
)
>
0
?
comment
.
getId
()
:
0
;
}
public int
getCommentCount
(
int
entityId
,
int
entityType
)
{
return
commentDAO
.
getCommentCount
(
entityId
,
entityType
)
;
}
public int
getUserCommentCount
(
int
userId
)
{
return
commentDAO
.
getUserCommentCount
(
userId
)
;
}
public boolean
deleteComment
(
int
commentId
)
{
return
commentDAO
.
updateStatus
(
commentId
,
1
)
>
0
;
}
public
Comment
getCommentById
(
int
id
)
{
return
commentDAO
.
getCommentById
(
id
)
;
}
}
5. Controller:业务入口
package
com.LG.controller
;
import
com.LG.async.EventModel
;
import
com.LG.async.EventProducer
;
import
com.LG.async.
EventType
;
import
com.LG.model.EntityType
;
import
com.LG.model.HostHolder
;
import
com.LG.service.QuestionService
;
import
com.LG.util.WendaUtil
;
import
org.slf4j.
Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.
Autowired
;
import
org.springframework.stereotype.
Controller
;
import
org.springframework.web.bind.annotation.
RequestMapping
;
import
org.springframework.web.bind.annotation.
RequestMethod
;
import
org.springframework.web.bind.annotation.
RequestParam
;
import
java.util.Date
;
/**
*
@Author
liguo
*
@Description
*
@Data
2018-09-06 8:53
*/
@Controller
public class
CommentController
{
private static final
Logger
logger
=
LoggerFactory
.
getLogger
(
CommentController
.
class
)
;
@Autowired
HostHolder
hostHolder
;
@Autowired
CommentService
commentService
;
@Autowired
QuestionService
questionService
;
@Autowired
EventProducer
eventProducer
;
@RequestMapping
(
path
= {
"/addComment"
},
method
= {
RequestMethod
.
POST
}
)
public
String
addComment
(
@RequestParam
(
"questionId"
)
int
questionId
,
@RequestParam
(
"content"
)
String
content
)
{
try
{
Comment
comment
=
new
Comment
()
;
comment
.
setContent
(
content
)
;
if
(
hostHolder
.
getUser
()
!=
null
)
{
comment
.
setUserId
(
hostHolder
.
getUser
()
.
getId
())
;
}
else
{
comment
.
setUserId
(
WendaUtil
.
ANONYMOUS_USERID
)
;
// return "redirect:/reglogin";
}
comment
.
setCreatedDate
(
new
Date
())
;
comment
.
setEntityType
(
EntityType
.
ENTITY_QUESTION
)
;
comment
.
setEntityId
(
questionId
)
;
commentService
.
addComment
(
comment
)
;
int
count
=
commentService
.
getCommentCount
(
comment
.
getEntityId
()
,
comment
.
getEntityType
())
;
questionService
.
updateCommentCount
(
comment
.
getEntityId
()
,
count
)
;
eventProducer
.
fireEvent
(
new
EventModel
(
EventType
.
COMMENT
)
.
setActorId
(
comment
.
getUserId
() )
.
setEntityId
(
questionId
))
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"增加评论失败"
+
e
.
getMessage
())
;
}
return
"redirect:/question/"
+
questionId
;
}
}
}