The project is continuously updated:
Table of contents
Total number of likes on the video page
User page like video list display
Mutual fans video waterfall list display
Total number of likes on the video page
There will be a number under our likes, representing the total number of likes. We need to get data from redis for display
Add a method to get the number of likes in redis in the implementation class of service:
private Integer getVlogBeLikedCounts(String vlogId){
String countsStr = redis.get(REDIS_VLOG_BE_LIKED_COUNTS+":"+vlogId);
if(StringUtils.isNotBlank(countsStr) ){
countsStr = "0";
}
return Integer.valueOf(countsStr);
}
In fact, we still need to complete a refresh of the value after the like, and re-up the number.
We restart, test, pull down to refresh.
At this time, the number of likes has changed to 1:
But when we open a video that has been recorded and liked, the value here is still 0, because we have not done a video query here
We open the frontend, which is a new controller:
In the backend:
@PostMapping("totalLikedCounts")
public GraceJSONResult totalLikedCounts(@RequestParam String vlogId) {
vlogService.getVlogBeLikedCounts(vlogId);
return GraceJSONResult.ok();
}
service interface:
service method implementation:
@Override
public Integer getVlogBeLikedCounts(String vlogId){
String countsStr = redis.get(REDIS_VLOG_BE_LIKED_COUNTS+":"+vlogId);
if(StringUtils.isNotBlank(countsStr) ){
countsStr = "0";
}
return Integer.valueOf(countsStr);
}
Then restart and test:
At this time, no matter whether you like or unfollow, the number of likes will change accordingly.
Here it has been implemented to the database and redis.
User page like video list display
We have completed the work and private display earlier
There is also a list of videos that have been liked, so these are all to be queried in association with the database, which involves a table
Here we need to query with the help of the following three tables
First, let's write an interface definition in Mapper:
Next, let's write our custom sql query:
<select id="getMyLikedVlogList" parameterType="map" resultType="com.imooc.vo.IndexVlogVO">
SELECT
v.id as vlogId,
v.vloger_id as vlogerId,
-- u.face as vlogerFace,
-- u.nickname as vlogerName,
v.title as content,
v.url as url,
v.cover as cover,
v.width as width,
v.height as height,
v.like_counts as likeCounts,
v.comments_counts as commentsCounts,
v.is_private as isPrivate
FROM
vlog v
LEFT JOIN
my_liked_vlog mlv
ON
v.id = mlv.vlog_id
LEFT JOIN
users u
on
mlv.user_id = u.id
WHERE
u.id = #{paramMap.userId}
AND
v.is_private = 0
ORDER BY
v.created_time
ASC
</select>
Also add one to the service:
Realized in service:
@Override
public PagedGridResult getMyVlogList(String usedId, Integer page, Integer pageSize) {
PageHelper.startPage(page,pageSize);
Map<String,Object> map = new HashMap<>();
map.put("userId",usedId);
List<IndexVlogVO> list = vlogMapperCustom.getMyLikedVlogList(map);
return setterPagedGrid(list,page);
}
Open the front end to view:
Our backend and frontend are consistent and placed in the same code area:
@GetMapping("myLikedList")
public GraceJSONResult myLikedList(@RequestParam String userId,
@RequestParam Integer page,
@RequestParam Integer pageSize) {
if (page == null) {
page = COMMON_START_PAGE;
}
if (pageSize == null) {
pageSize = COMMON_PAGE_SIZE;
}
PagedGridResult gridResult = vlogService.getMyVlogList(userId, page, pageSize);
return GraceJSONResult.ok(gridResult);
}
Let's restart and test:
Here is a demonstration of the video we liked.
My follow video list display
In the past, all we completed were video recommendations.
We can swipe right here:
In Follow, as long as I follow bloggers, their published videos will be displayed on the follow page.
Here is also the need to perform multi-table association through our sql:
A query through these three tables
We write sql statements in the xml file:
<select id="getMyFollowList" parameterType="map" resultType="com.imooc.vo.IndexVlogVO">
SELECT
v.id as vlogId,
v.vloger_id as vlogerId,
u.face as vlogerFace,
u.nickname as vlogerName,
v.title as content,
v.url as url,
v.cover as cover,
v.width as width,
v.height as height,
v.like_counts as likeCounts,
v.comments_counts as commentsCounts,
v.is_private as isPrivate
FROM
vlog v
LEFT JOIN
fans f
ON
v.vloge_Id = u.id
LEFT JOIN
users u
on
f.vloger_id = u.id
WHERE
v.is_private = 0
AND
f.fan_id=#{paramMap.myId}
ORDER BY
v.created_time
DESC
</select>
mapper interface:
Then in the service:
accomplish:
@Override
public PagedGridResult getMyFollowVlogList(String myId, Integer page, Integer pageSize) {
PageHelper.startPage(page,pageSize);
Map<String,Object> map = new HashMap<>();
map.put("myId",myId);
List<IndexVlogVO> list = vlogMapperCustom.getMyFollowVlogList(map);
for(IndexVlogVO v : list){
String vlogerId = v.getVlogerId();
String vlogId = v.getVlogId();
if(StringUtils.isNotBlank(myId)){
//用户必定关注过该博主
v.setDoILikeThisVlog(true);
//判断当前用户是否点赞
v.setDoILikeThisVlog(doILikeVlog(myId,vlogId));
}
//获得当前视频的点过赞的总数
v.setLikeCounts(getVlogBeLikedCounts(vlogId));
}
return setterPagedGrid(list,page);
}
Here we need to make a supplement to the previous query operation:
Here it is necessary to determine whether the user has followed the blogger
//用户是否关注该博主
boolean doIFollowVloger = fansService.queryDoIFollowVloger(userId,vlogerId);
v.setDoIFollowVloger(doIFollowVloger);
Before writing the controller, we first open the front end:
On the backend we write:
@GetMapping("followList")
public GraceJSONResult followList(@RequestParam String myId,
@RequestParam Integer page,
@RequestParam Integer pageSize) {
if (page == null) {
page = COMMON_START_PAGE;
}
if (pageSize == null) {
pageSize = COMMON_PAGE_SIZE;
}
PagedGridResult gridResult = vlogService.getMyFollowVlogList(myId, page, pageSize);
return GraceJSONResult.ok(gridResult);
}
Then reboot and test:
Here is the video list of the bloggers I follow
Here you can unfollow bloggers, pay attention to bloggers and conduct some tests.
Mutual fans video waterfall list display
When we switch at the bottom of the homepage, there is a list of videos posted by users who are related to each other.
The writing of the sql statement here is actually similar to ours above, but an additional judgment condition is added
If it is 1 here, it means mutual attention
Then implement it in our Mapper interface:
<select id="getMyFriendVlogList" parameterType="map" resultType="com.imooc.vo.IndexVlogVO">
SELECT
v.id as vlogId,
v.vloger_id as vlogerId,
u.face as vlogerFace,
u.nickname as vlogerName,
v.title as content,
v.url as url,
v.cover as cover,
v.width as width,
v.height as height,
v.like_counts as likeCounts,
v.comments_counts as commentsCounts,
v.is_private as isPrivate
FROM
vlog v
LEFT JOIN
fans f
ON
v.vloge_Id = f.fan_id
LEFT JOIN
users u
on
f.fan_id = u.id
WHERE
v.is_private = 0
AND
f.vloger_id=#{paramMap.myId}
AND
f.is_fan_friend_of_mine = 1
ORDER BY
v.created_time
DESC
</select>
Then build our service:
@Override
public PagedGridResult getMyFriendVlogList(String myId, Integer page, Integer pageSize) {
PageHelper.startPage(page,pageSize);
Map<String,Object> map = new HashMap<>();
map.put("myId",myId);
List<IndexVlogVO> list = vlogMapperCustom.getMyFriendVlogList(map);
for(IndexVlogVO v : list){
String vlogerId = v.getVlogerId();
String vlogId = v.getVlogId();
if(StringUtils.isNotBlank(myId)){
//用户必定关注过该博主
v.setDoILikeThisVlog(true);
//判断当前用户是否点赞
v.setDoILikeThisVlog(doILikeVlog(myId,vlogId));
}
//获得当前视频的点过赞的总数
v.setLikeCounts(getVlogBeLikedCounts(vlogId));
}
return setterPagedGrid(list,page);
}
Here you can combine the getMyFollowVlogList and getMyFriendVlogList methods, which will be more elegant and beautiful.
Let's look at the front end:
Write our backend controller:
@GetMapping("friendList")
public GraceJSONResult friendList(@RequestParam String myId,
@RequestParam Integer page,
@RequestParam Integer pageSize) {
if (page == null) {
page = COMMON_START_PAGE;
}
if (pageSize == null) {
pageSize = COMMON_PAGE_SIZE;
}
PagedGridResult gridResult = vlogService.getMyFriendVlogList(myId, page, pageSize);
return GraceJSONResult.ok(gridResult);
}
Finally restart and test:
Here is a waterfall flow, the more content in the list, the more pages will slide down.
If we follow and like on the homepage, we will find that it throws an exception, because we did not make corresponding settings when we did the detailed query.
Combined primary key duplicates
This is a problem with detail, let's make a fix
Because there are many repeated operations like this here, we take it out separately
private IndexVlogVO setterVO(IndexVlogVO v, String userId){
String vlogerId = v.getVlogerId();
String vlogId = v.getVlogId();
if(StringUtils.isNotBlank(userId)){
//用户必定关注过该博主
v.setDoILikeThisVlog(true);
//判断当前用户是否点赞
v.setDoILikeThisVlog(doILikeVlog(userId,vlogId));
}
//获得当前视频的点过赞的总数
v.setLikeCounts(getVlogBeLikedCounts(vlogId));
return v;
}
Write method implementation in servce:
@Override
public IndexVlogVO getVlogDetailById(String userId,String vlogId) {
Map<String,Object> map = new HashMap<>();
map.put("vlogId",vlogId);
List<IndexVlogVO> list= vlogMapperCustom.getIndexVlogList(map);
if(list!= null && list.size()>0 && !list.isEmpty()){
IndexVlogVO vlogVO=list.get(0);
setterVO(vlogVO,userId);
return setterVO(vlogVO,userId);
}
return null;
}
In the controller:
@GetMapping("friendList")
public GraceJSONResult friendList(@RequestParam String myId,
@RequestParam Integer page,
@RequestParam Integer pageSize) {
if (page == null) {
page = COMMON_START_PAGE;
}
if (pageSize == null) {
pageSize = COMMON_PAGE_SIZE;
}
PagedGridResult gridResult = vlogService.getMyFriendVlogList(myId, page, pageSize);
return GraceJSONResult.ok(gridResult);
}
Let's restart and test:
Follow and like are all realized here.
The above completes the development of all our fan business modules.