查看我的动态
一、MovementsController
/**
* 查看我的动态:
* 请求路径:/movements/all
* 请求方式:get
* 请求参数:page,当前页码;pagesize,每页显示数;userId,用户id
* 响应结果:PageResult
*/
@GetMapping("/all")
public ResponseEntity lookMyMovements(Long userId,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize){
//1.直接调用业务层完成查看我的动态业务
PageResult pageResult = movementsService.lookMyMovements(userId,page,pageSize);
//2.构造返回值
return ResponseEntity.ok(pageResult);
}
二、MovementsService
/**
*
*查看我的动态
*/
public PageResult lookMyMovements(Long userId, Integer page, Integer pageSize) {
//1.调用api分页查询我的动态数据
PageResult pageResult = movementsApi.lookMyMovements(userId,page,pageSize);
//2.在PageResult里获取movements数据
List<Movement> movements = (List<Movement>) pageResult.getItems();
//3.判断movement是否为空,为空直接返回pageResult
if(movements == null){
return pageResult;
}
//4.调用userInfoA根据用户id查询用户详情
UserInfo userInfo = userInfoApi.queryUserInfo(userId);
List<MovementsVo> movementsVoList = new ArrayList<>();
//5.遍历movements,每条动态构造一个vo对象
for (Movement movement : movements) {
MovementsVo movementsVo = MovementsVo.init(userInfo, movement);
movementsVoList.add(movementsVo);
}
//5.构造返回值
pageResult.setItems(movementsVoList);
return pageResult;
}
三、lookMyMovements
/**
*
* 查看我的动态
*/
public PageResult lookMyMovements(Long userId, Integer page, Integer pageSize) {
//1.创建Criteria对象,设置查询条件
Criteria criteria = Criteria.where("userId").is(userId);
//2.根据criteria创建Query
Query query = Query.query(criteria);
query.skip(( page - 1 ) * pageSize ).limit(pageSize).with(Sort.by(Sort.Order.desc("created")));
//3.调用mongoTemplate查询
List<Movement> movements = mongoTemplate.find(query, Movement.class);
//4.构造返回值
PageResult pageResult = new PageResult(page,pageSize,0,movements);
return pageResult;
}
四、vo对象
package com.tanhua.model.vo;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Movement;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MovementsVo implements Serializable {
private String id; //动态id
private Long userId; //用户id
private String avatar; //头像
private String nickname; //昵称
private String gender; //性别 man woman
private Integer age; //年龄
private String[] tags; //标签
private String textContent; //文字动态
private String[] imageContent; //图片动态
private String distance; //距离
private String createDate; //发布时间 如: 10分钟前
private Integer likeCount; //点赞数
private Integer commentCount; //评论数
private Integer loveCount; //喜欢数
private Integer hasLiked; //是否点赞(1是,0否)
private Integer hasLoved; //是否喜欢(1是,0否)
public static MovementsVo init(UserInfo userInfo, Movement item) {
MovementsVo vo = new MovementsVo();
//设置动态数据
BeanUtils.copyProperties(item, vo);
vo.setId(item.getId().toHexString());
//设置用户数据
BeanUtils.copyProperties(userInfo, vo);
if(!StringUtils.isEmpty(userInfo.getTags())) {
vo.setTags(userInfo.getTags().split(","));
}
//图片列表
vo.setImageContent(item.getMedias().toArray(new String[]{}));
//距离
vo.setDistance("500米");
Date date = new Date(item.getCreated());
vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date));
//设置是否点赞(后续处理)
vo.setHasLoved(0);
vo.setHasLiked(0);
return vo;
}
}
标签:String,查看,userId,private,vo,import,Integer,动态
From: https://www.cnblogs.com/zhangdashuaige/p/16840934.html