v0/src/main/java/com/yupi/springbootinit/model/vo/PostVO.java

119 lines
2.1 KiB
Java

package com.yupi.springbootinit.model.vo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yupi.springbootinit.model.entity.Post;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.Data;
import org.springframework.beans.BeanUtils;
/**
* 帖子视图
*
* @author <a href="https://github.com/liyupi">程序员鱼皮</a>
* @from <a href="https://yupi.icu">编程导航知识星球</a>
*/
@Data
public class PostVO implements Serializable {
private final static Gson GSON = new Gson();
/**
* id
*/
private Long id;
/**
* 标题
*/
private String title;
/**
* 内容
*/
private String content;
/**
* 点赞数
*/
private Integer thumbNum;
/**
* 收藏数
*/
private Integer favourNum;
/**
* 创建用户 id
*/
private Long userId;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
/**
* 标签列表
*/
private List<String> tagList;
/**
* 创建人信息
*/
private UserVO user;
/**
* 是否已点赞
*/
private Boolean hasThumb;
/**
* 是否已收藏
*/
private Boolean hasFavour;
/**
* 包装类转对象
*
* @param postVO
* @return
*/
public static Post voToObj(PostVO postVO) {
if (postVO == null) {
return null;
}
Post post = new Post();
BeanUtils.copyProperties(postVO, post);
List<String> tagList = postVO.getTagList();
if (tagList != null) {
post.setTags(GSON.toJson(tagList));
}
return post;
}
/**
* 对象转包装类
*
* @param post
* @return
*/
public static PostVO objToVo(Post post) {
if (post == null) {
return null;
}
PostVO postVO = new PostVO();
BeanUtils.copyProperties(post, postVO);
postVO.setTagList(GSON.fromJson(post.getTags(), new TypeToken<List<String>>() {
}.getType()));
return postVO;
}
}