zoukankan      html  css  js  c++  java
  • 收藏模块的设计

    环境:mysql8.0 springboot2 mybatis-plus

    功能分析

    收藏模块是许多网站都有的一个模块,大概思路如下
    前端用户发起收藏操作时,调用api接口判断是否已经收藏?弹出消息提示已收藏:向数据库写入一条收藏记录并提示收藏成功;
    前端发送的数据要包括用户id,被收藏资源的id

    建表

    CREATE TABLE `collect` (
      `id` INT NOT NULL AUTO_INCREMENT,
      `user_id` INT NOT NULL COMMENT '用户id',
      `owner_id` INT NOT NULL COMMENT '被收藏资源的id',
      `gmt_create` DATETIME NULL DEFAULT current_timestamp COMMENT '阿里的mysql规范',
      PRIMARY KEY (`id`)
    )
    COMMENT = '用户收藏表';
    

    后端

    偷懒使用mp的代码生成器一键生成相应模块

    // 实体类部分代码
    @Data
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    public class Collect implements Serializable {
        @TableId(type = IdType.AUTO)
        private Long id;
        private Integer userId;
        private Integer ownerId;
        private Date gmtCreate;
    }
    

    简单写个接口测试下

    @RestController
    @Api("用户基本功能接口")
    public class CollectController {
        @Autowired
        ICollectService service;
    
        @ApiOperation("收藏一篇文章,需要用户id,文章id")
        @RequestMapping("/collect")
        public String collect(Integer uid, Integer pid) {
            Collect c = new Collect();
            c.setUserId(uid);
            c.setOwnerId(pid);
            boolean save = service.save(c);
            return save + "";
        }
    }
    

    返回true代表插入成功

    数据库也确实有对应的记录

    功能完善

    避免重复收藏

    一个用户id和一个文章id确定一条唯一的记录,不能重复
    有一种是收藏前,先根据用户id和文章id去查询一次,麻烦还得写个方法还多一次数据库读写
    所以这次采用mysql的设置多列为唯一约束,将用户id和文章id设置为唯一约束,当插入的一条记录存在完全相同用户id和文章id则会报错

    使用下列sql语句添加唯一约束
    ALTER TABLE `collect` ADD UNIQUE `unique_index`(`user_id`, `owner_id`);

    使用重复的数据进行测试

    取消收藏功能

    方法一

        @DeleteMapping("/collect")
        public String cancelCollect(String cid) {
            boolean b = service.removeById(cid);
            return b + "";
        }
    

    方法二:取消收藏需要用户id和文章id,避免别人调用接口乱取消收藏,其中用户id可以从token中获取

        @ApiOperation("取消收藏,需要收藏文章的id和token")
        @DeleteMapping("/collect")
        public String cancelCollect(HttpServletRequest req, Integer pid) {
            Integer uid = Integer.parseInt(JwtUtil.getId(req)); //从token中获取用户id
            Map<String,Object> map =new HashMap<>();
            map.put("user_id",uid);
            map.put("owner_id",pid);
            boolean b = service.removeByMap(map);
            return b + "";
        }
    

    根据用户收藏选择性的展示用户收藏的文章集合

    涉及到多表查询大致语句如下:

    select * from post a,collect c where a.id=c.owner_id and c.user_id= 用户id 
    

    在CollectMapper里使用@Select注解写动态sql

    @Select("select * from le_post a,collect c where a.id=c.owner_id and c.user_id= #{uid}")
    List<LePost> getUserCollection(Integer uid);
    

    然后service层,controller随便写下,就能获取uid下所有收藏的文章的具体内容了

    用户登陆时,首页文章根据用户收藏记录来渲染已经收藏的文章

        @ApiOperation("根据用户id获取收藏记录,用于渲染前端文章列表的收藏按钮")
        @GetMapping("/collect/{uid}")
        public String getUserCollectionRecord(@PathVariable String uid) throws JsonProcessingException {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user_id", uid);
            List<Collect> list = service.listByMap(map);
            return ToJsonUtil.reJson(list, 200, "ok");
        }
    

    To Be Continue→


    [1]MyBatis-Plus 代码生成器
    [3]mysql创建多列组合唯一索引,unique index

  • 相关阅读:
    uoj388 【UNR #3】配对树
    uoj386 【UNR #3】鸽子固定器
    回忆录
    xcode 把项目代码提交到远程SVN服务器
    IOS 点击按钮拨号
    ADO与达梦7产生的一个未知问题
    DSN 建立达梦7(DM)连接
    iOS UIControl 事件的说明(转)
    IOS开发copy,nonatomic, retain,weak,strong用法
    QT Creator 使用SVN的版本号做为编译的版本信息
  • 原文地址:https://www.cnblogs.com/somegenki/p/13156494.html
Copyright © 2011-2022 走看看