前言
文章板块,除了有关推荐的接口设计,还有其他的一些边缘的功能的接口需要设计,共同配合才能完成文章板块
有关文章的点赞和浏览
本来没有文章的点赞表,后来为了做热榜,在流行度和新鲜度的推荐上,加上了点赞对于评分的影响,点一个赞对于评分的影响是浏览一次文章的两倍。
在每次点赞和浏览之后,都会触发相应的接口,在数据库中插入一条浏览记录或点赞记录,然后为文章更新评分。
(1)用户的点赞
controller层:
/**
* 用户给文章点赞
* @param username
* @param articleId
* @return
*/
@PermitAll
@RequestMapping(value = "/addLikeArticle", method = RequestMethod.GET, produces = "application/json")
public Response addLikeArticle(
@RequestAttribute String username,
@RequestParam Integer articleId) {
User ouser = userService.selectUserByName(username);
//更新评分
freshPopularRecommended.addLikeScore(articleId);
//添加点赞记录
userDataRecordService.addUserLiking(ouser.getId(),articleId);
return Response.success("点赞成功");
}
redis的操作:
/**
* 当一个博客被赞时调用
* @param articleId
*/
public void addLikeScore(int articleId) {
redisUtils.zsetIncrScore(ZSET_KEY, String.valueOf(articleId), LIKE_COUNT_K);
}
service层:
/**
* 用户给文章点赞
* @param userId
* @param articleId
*/
public void addUserLiking(int userId,int articleId)
{
//设置日期格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取当前时间为爬取时间
String browsingTime = df.format(new Date());
// 添加一条用户like记录
userDataRecordDao.addUserLiking(userId,articleId,browsingTime);
//更新文章信息,like_count+1
articleDao.updateArticleLikeCount(articleId);
}
dao层:
/**
* 当给文章点赞后 点赞数+1
* @param articleId
*/
@Update("UPDATE article SET like_count = like_count +1 where id = #{articleId}")
void updateArticleLikeCount(@Param("articleId") int articleId);
(2)当用户浏览时,添加一条浏览记录,并更新评分,并且次文章的点击量需要加一
controller层:
/**
* 当用户点击浏览文章详情时,需要添加一条用户的浏览记录
* @param articleId
* @return
*/
@PermitAll
@RequestMapping(value = "/browerHistory", method = RequestMethod.GET, produces = "application/json")
public Response addBrowerHistory(
@RequestAttribute String username,
@RequestParam Integer articleId) {
User ouser = userService.selectUserByName(username);
//点击量+1
articleService.updateArticleReadCount(articleId);
userDataRecordService.addUserBrowsingHistory(ouser.getId(),articleId);
//给这篇文章加分
freshPopularRecommended.addReadScore(articleId);
return Response.success("添加用户浏览记录成功");
}
操作redis:
/**
* 当一个博客被点击后 点击量会+1 得分会增加K
*
* @param articleId
*/
public void addReadScore(int articleId) {
redisUtils.zsetIncrScore(ZSET_KEY, String.valueOf(articleId), READ_COUNT_K);
}
service层:
/**
* 当用户浏览一篇文章时 添加用户浏览记录
* @param userId
* @param articleId
*/
public void addUserBrowsingHistory(int userId,int articleId)
{
//设置日期格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取当前时间为爬取时间
String browsingTime = df.format(new Date());
userDataRecordDao.addUserBrowsingHistory(userId,articleId,browsingTime);
}
dao层:
/**
* 当用户浏览一篇文章时,添加用户浏览记录
* @param userId
* @param articleId
* @param browsingTime
*/
@Insert("INSERT into user_browsing_history (user_id,article_id,browse_time) VALUES (#{userId},#{articleId},#{browsingTime})")
void addUserBrowsingHistory(@Param("userId") int userId,@Param("articleId") int articleId,@Param("browsingTime") String browsingTime);
有关用户的历史记录
需要返回有关用户对于文章的所有的点赞记录和浏览的历史信息
(1)获取用户的浏览历史
controller层:
/**
* 返回用户的浏览历史
* @param username
* @param page
* @param size
* @return
* @throws IOException
* @throws TasteException
*/
@PermitAll
@RequestMapping(value = "/getBrowsingHistory", method = RequestMethod.GET, produces = "application/json")
public Response getUserBrowsingHistory(
@RequestAttribute String username,
@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
if (page < 0) {
page = 1;
}
if (size < 0) {
size = 20;
}
User ouser = userService.selectUserByName(username);
List<Article> articleList = userDataRecordService.selectUserBrowsingHistory(ouser.getId(),page,size);
for(Article article :articleList)
{
getArticleInfo(article);
}
return Response.success("获取用户浏览历史成功",articleList);
}
service层:
/**
* 查找用户浏览的历史文章
* @param userId
* @param page
* @param size
* @return
*/
public List<Article> selectUserBrowsingHistory(int userId ,int page ,int size)
{
int start = (page - 1) * size;
if (start < 0) {
start = 0;
}
if (size < 0 || size > 100) {
size = 10;
}
return userDataRecordDao.selectUserBrowsingHistory(userId,size,start);
}
dao层:
需要将article表和user_browsing_history进行自然连接,找到用户浏览历史中的文章的详细信息
/**
* 返回用户的浏览历史
* @param userId
* @param limit
* @param offset
* @return
*/
@Select("SELECT A.id, A.author ,A.origin_link,A.update_time,A.title," +
"A.content,A.verbal_content,A.summary,A.read_count," +
" A.like_count,A.crawling_time,A.blog_id FROM " +
" article as A , user_browsing_history as B " +
"where B.user_id = #{userId} and A.id = B.article_id " +
"ORDER BY B.browse_time DESC limit #{limit} offset #{offset} ")
@Results(id = "Article", value = {
@Result(property = "id", column = "id"),
@Result(property = "author", column = "author"),
@Result(property = "originLink", column = "origin_link"),
@Result(property = "updateTime", column = "update_time"),
@Result(property = "title", column = "title"),
@Result(property = "content", column = "content"),
@Result(property = "verbalContent", column = "verbal_content"),
@Result(property = "summary", column = "summary"),
@Result(property = "readCount", column = "read_count"),
@Result(property = "likeCount", column = "like_count"),
@Result(property = "crawlingTime", column = "crawling_time"),
@Result(property = "blogId", column = "blog_id"),
})
List<Article> selectUserBrowsingHistory(@Param("userId") int userId, @Param("limit") int limit,@Param("offset") int offset );
(2)获取用户的点赞信息(用户喜欢的文章列表)
controller层:
/**
* 获取用户喜欢的博客列表
* @param username
* @param page
* @param size
* @return
*/
@PermitAll
@RequestMapping(value = "/getLikingList", method = RequestMethod.GET, produces = "application/json")
public Response getUserLikingList(
@RequestAttribute String username,
@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
if (page < 0) {
page = 1;
}
if (size < 0) {
size = 20;
}
User ouser = userService.selectUserByName(username);
List<Article> articleList = userDataRecordService.selectUserLikeList(ouser.getId(),page,size);
for(Article article :articleList)
{
getArticleInfo(article);
}
return Response.success("获取用户点赞列表成功",articleList);
}
service层:
/**
* 获取用户点赞过的文章列表
* @param userId
* @param page
* @param size
* @return
*/
public List<Article> selectUserLikeList(int userId ,int page ,int size)
{
int start = (page - 1) * size;
if (start < 0) {
start = 0;
}
if (size < 0 || size > 100) {
size = 10;
}
return userDataRecordDao.selectUserLikeList(userId,size,start);
}
dao层:
/**
*获取用户点赞过的文章列表
* @param userId
* @param limit
* @param offset
* @return
*/
@Select("SELECT A.id, A.author ,A.origin_link,A.update_time,A.title," +
"A.content,A.verbal_content,A.summary,A.read_count," +
" A.like_count,A.crawling_time,A.blog_id FROM " +
" article as A , user_liking as B " +
"where B.user_id = #{userId} and A.id = B.article_id " +
"ORDER BY B.click_liking_time DESC limit #{limit} offset #{offset} ")
@ResultMap("Article")
List<Article> selectUserLikeList(@Param("userId") int userId, @Param("limit") int limit,@Param("offset") int offset );
有关tag的接口
(1)根据每个tag拥有文章的数量,降序返回tag列表
/**
* 根据标签下文章的数量,返回标签列表
* @param page
* @param size
* @return
*/
@PermitAll
@RequestMapping(value = "/listOrderByCount", method = RequestMethod.GET, produces = "application/json")
public Response getTagListOrderByCount(
@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "size", required = false, defaultValue = "20") Integer size
) {
List<Tag> tagList = tagService.selectTagListOrderByCount(page,size);
return Response.success("获取标签列表成功", tagList);
}
/**
* 根据标签下文章的数量,返回标签列表
* @param page
* @param size
* @return
*/
public List<Tag> selectTagListOrderByCount(int page,int size)
{
int start = (page - 1) * size;
if (start < 0) {
start = 0;
}
if (size < 0 || size > 100) {
size = 20;
}
return tagDao.selectTagListOrderByCount(start,size);
}
/**
*根据标签下文章的数量,返回标签列表
* @param start
* @param size
* @return
*/
@Select("SELECT * FROM tag " +
"ORDER BY count DESC " +
" LIMIT #{size} OFFSET #{start} ")
List<Tag> selectTagListOrderByCount(@Param("start") int start, @Param("size") int size);
(2)根据tagId,返回文章列表
/**
* 根据tagid 返回文章列表
* @param tagId
* @param page
* @param size
* @return
*/
@PermitAll
@RequestMapping(value = "/articleListByTag", method = RequestMethod.GET, produces = "application/json")
public Response getArticleListByTag(
@RequestParam Integer tagId,
@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
if (page < 0) {
page = 1;
}
if (size < 0) {
size = 20;
}
TagArticleList tagArticleList = articleTagService.selectArticleByTag(tagId,page,size);
return Response.success("获取文章成功", tagArticleList);
}
/**
* 根据tagid 查询对应的文章列表
* @param tagId
* @param page
* @param size
* @return
*/
public TagArticleList selectArticleByTag(int tagId, int page, int size)
{
int start = (page - 1) * size;
if (start < 0) {
start = 0;
}
if (size < 0 || size > 100) {
size = 20;
}
//获取标签的详细描述
Tag tag = tagDao.getTagById(tagId);
List<Article> articleList = articleTagDao.selectArticleByTag(tagId,size,start);
for(Article article :articleList)
{
List<Tag> tagList = selectArticleTag(article.getId());
List<Category> categoryList = articleCategoryDao.selectArticleCategory(article.getId());
article.setTagList(tagList);
article.setCategory(categoryList.get(0));
article.setRatingCount(freshPopularRecommended.getArticleScore(article.getId()));
}
TagArticleList tagArticleList = new TagArticleList();
tagArticleList.setTag(tag);
tagArticleList.setArticleList(articleList);
return tagArticleList;
/**
* 根据标签id 获取对应标签下的文章信息
* @param tagId
* @param limit
* @param offset
* @return
*/
@Select("SELECT B.id, B.author ,B.origin_link,B.update_time,B.title," +
"B.content,B.verbal_content,B.summary,B.read_count,B.like_count,B.crawling_time,B.blog_id " +
"FROM article_tag as A , article as B " +
"WHERE A.tag_id = #{tagId} " +
"and A.article_id = B.id " +
" ORDER BY update_time DESC " +
" LIMIT #{limit} OFFSET #{offset}")
@Results(id = "Article", value = {
@Result(property = "id", column = "id"),
@Result(property = "author", column = "author"),
@Result(property = "originLink", column = "origin_link"),
@Result(property = "updateTime", column = "update_time"),
@Result(property = "title", column = "title"),
@Result(property = "content", column = "content"),
@Result(property = "verbalContent", column = "verbal_content"),
@Result(property = "summary", column = "summary"),
@Result(property = "readCount", column = "read_count"),
@Result(property = "likeCount", column = "like_count"),
@Result(property = "crawlingTime", column = "crawling_time"),
@Result(property = "blogId", column = "blog_id"),
})
List<Article> selectArticleByTag(@Param("tagId") int tagId,@Param("limit") int limit,@Param("offset") int offset);
根据点赞数量降序的文章列表
/**
* 根据点赞数量降序的文章列表
* @param page
* @param size
* @return
*/
@PermitAll
@RequestMapping(value = "/articleListByLikeCount", method = RequestMethod.GET, produces = "application/json")
public Response getArticleListByLikeCount(
@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
if (page < 0) {
page = 1;
}
if (size < 0) {
size = 20;
}
List<Article> articleList = articleService.selectArticleListOrderByLikeCount(page,size);
for(Article article :articleList)
{
getArticleInfo(article);
}
return Response.success("获取文章成功", articleList);
}
/**
* 根据点赞量排序 返回文章列表
* @param page
* @param size
* @return
*/
public List<Article> selectArticleListOrderByLikeCount(int page,int size)
{
int start = (page - 1) * size;
if (start < 0) {
start = 0;
}
if (size < 0 || size > 100) {
size = 20;
}
return articleDao.selectArticleListOrderByLikeCount(start,size);
}
/**
* 按照点赞数量 获取文章列表
* @param offset
* @param limit
* @return
*/
@Select("SELECT * FROM article " +
"ORDER BY like_count DESC " +
"LIMIT #{limit} OFFSET #{offset}")
@ResultMap("Article")
List<Article> selectArticleListOrderByLikeCount(@Param("offset")int offset,@Param("limit") Integer limit);