前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >八. 实现房间的新增,查找,删除,修改接口实现

八. 实现房间的新增,查找,删除,修改接口实现

作者头像
用户8988577
发布2022-12-27 15:58:12
4940
发布2022-12-27 15:58:12
举报
文章被收录于专栏:言云纪

本期代码量较大,且涉及多表操作逻辑复杂建议配合目录食用!每个功能的编写顺序都是Service=>ServiceImpl=>Dao=>DaoImpl 最后再用Controller调用Service完成接口的实现

前期准备

本篇的验证都是基于token进行操作,所以需要先准备一个token生成器的test方法!最后将生成的token保存到请求头的token中!

代码语言:javascript
复制
class TokenUtilsTest {

    @Test
    void sign() {
    // 如果不能使用builder方法,请查看pom文件中是否存在lombok依赖,若依赖存在请检查User实体类上是否添加@Builder注解
        User user = User.builder()
                .openid("9IWM51WXRKD9BN21CJV SS9SXBAMV4F FEA")
                .build();
 
        System.out.println(TokenUtils.sign(user));
    }
}

查看已创建的房间

Dao数据持久层

  1. 暴露接口

VideoroomDao 根据vrid查询视频房间信息,返回数组对象

代码语言:javascript
复制
public List<Videoroom> get_videoroom(Long vrid);

CreateRoomDao 根据加密后的openid查询创建房间记录表中的所以数据

代码语言:javascript
复制
public List<CreateRoom> get_createRoomList(String md5_openid);
  1. 数据持久层接口实现

VideoroomDaoImpl

代码语言:javascript
复制
    @Autowired
    private VideoroomMapper videoroomMapper;

    private VideoroomExample videoroomExample = new VideoroomExample();

    public List<Videoroom> get_videoroom(Long vrid){
        videoroomExample.createCriteria().andVrIdEqualTo(vrid);
        return videoroomMapper.selectByExample(videoroomExample);
    }

CreateRoomDao

代码语言:javascript
复制
    @Autowired
    private CreateRoomMapper createRoomMapper;
    private CreateRoomExample createRoomExample = new CreateRoomExample();

    public List<CreateRoom> get_createRoomList(String md5_openid){
        createRoomExample.createCriteria().andOpenidEqualTo(md5_openid);
        return createRoomMapper.selectByExample(createRoomExample);
    }

Service服务层

  1. 暴露接口

VideoroomService

代码语言:javascript
复制
public BaseResponse get_videoroom(String token);
  1. 服务层接口实现

VideoroomServiceImpl

代码语言:javascript
复制
    @Autowired
    private CreateRoomDao createRoomDao;
    @Autowired
    private VideoroomDao videoroomDao;
    /*
     * @Description 根据token查看已创建的房间等信息
     * @Param null
     * @Return {@link null}
     * @Author Ocean_IT
     * @Email 14312400@qq.com
     * @Date 2022/10/5 2:27
     */

    public BaseResponse get_videoroom(String token){
        List<CreateRoom> createRoomList = createRoomDao.get_createRoomList(TokenUtils.getOpenid(token));
        if(createRoomList.size() == 0){
            return RespGenerator.success("未查询到用户已创建房间信息!");
        }
        return RespGenerator.success(videoroomDao.get_videoroom(createRoomList.get(0).getVrId()));
    }

Controller控制器层

Videoroomcontroller

代码语言:javascript
复制
    @Autowired
    private VideoroomService videoroomService;
    //查看自己创建的房间
    @GetMapping("/videoroom")
    public BaseResponse get_videoroom(HttpServletRequest request){
        return videoroomService.get_videoroom(request.getHeader("token"));
    }

调用方法

url:http://127.0.0.1:8080/videoroomcontroller/videoroom Methed:GET Header:token return:json

创建房间

Mapper编写

UserMapper.xml

代码语言:javascript
复制
  <update id="updateByAddCount_create_room" parameterType="map">
    <!--
    create_room 数量 + 1
    -->
    update user
    set create_room = create_room + 1
    where openid = #{record.openid,jdbcType=VARCHAR}
  </update>

  <update id="updateByDelCount_create_room" parameterType="map">
    <!--
    create_room 数量 - 1
    -->
    update user
    set create_room = create_room - 1
    where openid = #{record.openid,jdbcType=VARCHAR}
  </update>

Mapper接口暴露

UserMapper.java

代码语言:javascript
复制
    int updateByAddCount_create_room(String md5_openid);

    int updateByDelCount_create_room(String md5_openid);java

Dao数据持久层

  1. 暴露接口

VideoDao 根据vid查询视频表信息,返回对象。

代码语言:javascript
复制
public Video get_vid(Long vid);

VideoroomDao 传入一个Videoroom对象并将他插入数据库,返回布尔类型

代码语言:javascript
复制
public Boolean post_videoroom(Videoroom videoroom);

UserDao 根据openid查询user表数据,并根据type执行createVideo字段的自增和自减,返回布尔类型

代码语言:javascript
复制
public Boolean put_createVideo_num(String md5_openid,Boolean type);

CreateRoomDao 根据主键查询表中是否超出创建房间的数量再执行创建。返回布尔类型

代码语言:javascript
复制
public Boolean Post_createRoom(CreateRoom createRoom);
  1. 数据持久层接口实现

VideoDaoImpl

代码语言:javascript
复制
    @Autowired
    private VideoMapper videoMapper;
    public Video get_vid(Long vid){
        return videoMapper.selectByPrimaryKey(vid);
    }

VideoroomDaoImpl

代码语言:javascript
复制
    public Boolean post_videoroom(Videoroom videoroom){
        return videoroomMapper.insert(videoroom) == 1?true:false;
    }

UserDaoImpl

代码语言:javascript
复制
    public Boolean put_createVideo_num(String md5_openid,Boolean type){
        if(type){
            return userMapper.updateByAddCount_create_room(md5_openid)==1?true:false;
        }else{
            return userMapper.updateByDelCount_create_room(md5_openid)==1?true:false;
        }
    }

CreateRoomDaoImpl

代码语言:javascript
复制
    public Boolean Post_createRoom(CreateRoom createRoom){
        createRoomExample.createCriteria().andOpenidEqualTo(createRoom.getOpenid());
        return createRoomMapper.selectByExample(createRoomExample).size()<1 ?createRoomMapper.insert(createRoom) ==1?true:false:false;
    }

Service服务层

  1. 暴露接口

VideoroomService

代码语言:javascript
复制
public BaseResponse post_videoroom(Videoroom videoroom,Long vid,String token);
  1. 服务层接口实现

VideoroomServiceImpl

代码语言:javascript
复制
    @Autowired
    private CreateRoomDao createRoomDao;
    @Autowired
    private VideoroomDao videoroomDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private VideoDao videoDao;
    /*
     * @Description 根据token创建房间
     * @Param null
     * @Return {@link null}
     * @Author Ocean_IT
     * @Email 14312400@qq.com
     * @Date 2022/10/5 2:28
     */
    @Transactional
    public BaseResponse post_videoroom(Videoroom videoroom,Long vid,String token){
        Video video = videoDao.get_vid(vid);
        Assert.notNull(video,"未查询到视频信息,请重新选择视频创建房间");
        videoroom.setVrId(IdWorker.getId());
        Assert.isTrue(videoroomDao.post_videoroom(videoroom),"房间创建失败");
        CreateRoom createRoom = CreateRoom.builder()
                .openid(TokenUtils.getOpenid(token))
                .crId(IdWorker.getId())
                .vrId(videoroom.getVrId())
                .vid(vid).build();
        Assert.isTrue(userDao.put_createVideo_num(TokenUtils.getOpenid(token),true),"房间数更新失败!");
        return createRoomDao.Post_createRoom(createRoom)?RespGenerator.success(createRoom):RespGenerator.fail("400","房间创建失败");
    }

Controller控制器层

videoroomcontroller

代码语言:javascript
复制
    //创建房间
    @PostMapping("/videoroom/{vid}")
    public BaseResponse post_videoroom(@PathVariable("vid") Long vid,@RequestBody Videoroom videoroom, HttpServletRequest request){
        return videoroomService.post_videoroom(videoroom,vid,request.getHeader("token"));
    }

调用方法

url:http://127.0.0.1:8080/videoroomcontroller/videoroom/{vid} Methed:POST Header:token DATA:{ "vrTitle":"标题", "vrDescribe":"描述信息" } return:json

修改房间信息

Dao数据持久层

  1. 暴露接口

VideoroomDao 根据vrid查询并修改数据为videoroom对象中的值

代码语言:javascript
复制
public Boolean put_videoroom(Long vrid,Videoroom videoroom);
  1. 数据持久层接口实现

VideoroomDaoImpl

代码语言:javascript
复制
    public Boolean put_videoroom(Long vrid,Videoroom videoroom){
        videoroomExample.createCriteria().andVrIdEqualTo(vrid);
        videoroom.setVrId(null);
        return videoroomMapper.updateByExampleSelective(videoroom,videoroomExample)==1?true:false;
    }

Service服务层

  1. 暴露接口

VideoroomService

代码语言:javascript
复制
public BaseResponse put_videoroom(Videoroom videoroom,String token);
  1. 服务层接口实现

VideoroomServiceImpl

代码语言:javascript
复制
    /*
     * @Description 根据token等信息修改房间信息
     * @Param null
     * @Return {@link null}
     * @Author Ocean_IT
     * @Email 14312400@qq.com
     * @Date 2022/10/5 6:20
     */
    @Transactional
    public BaseResponse put_videoroom(Videoroom videoroom,String token){
        List<CreateRoom> createRoomList = createRoomDao.get_createRoomList(TokenUtils.getOpenid(token));
        if(createRoomList.size() == 0){
            return RespGenerator.fail("400","未查询到你要修改房间的信息!");
        }else{
            Assert.isTrue(videoroomDao.put_videoroom(createRoomList.get(0).getVrId(),videoroom),"修改房间信息失败!");
            return RespGenerator.success_noData("房间信息修改完成!");
        }
    }

Controller控制器层

Videoroomcontroller

代码语言:javascript
复制
    //修改房间标题等信息
    @PutMapping("/videoroom")
    public BaseResponse put_videoroom(@RequestBody Videoroom videoroom,HttpServletRequest request){
        return videoroomService.put_videoroom(videoroom,request.getHeader("token"));
    }

调用方法

url:http://127.0.0.1:8080/videoroomcontroller/videoroom Methed:PUT Header:token Data:{ "vrTitle":"sss打赏", "vrDescribe":"dasd", "vrId":"0" } return:json

删除房间

Dao数据持久层

  1. 暴露接口

CreateRoomDao 根据vrid删除表信息

代码语言:javascript
复制
public Boolean del_createRoom(Long vrid);

VideoroomDao 根据vrid删除表信息

代码语言:javascript
复制
public Boolean del_videoroom(Long vrid);
  1. 数据持久层接口实现

CreateRoomDaoImpl

代码语言:javascript
复制
    public Boolean del_createRoom(Long vrid){
        createRoomExample.createCriteria().andVrIdEqualTo(vrid);
        return createRoomMapper.deleteByExample(createRoomExample) == 1?true:false;
    }

VideoroomDaoImpl

代码语言:javascript
复制
    public Boolean del_videoroom(Long vrid){
        return videoroomMapper.deleteByPrimaryKey(vrid) ==1?true:false;
    }

Service服务层

  1. 暴露接口

VideoroomService

代码语言:javascript
复制
public BaseResponse del_videoroom(String token);
  1. 服务层接口实现

VideoroomServiceImpl

代码语言:javascript
复制
    /*
     * @Description 根据token删除房间
     * @Param null
     * @Return {@link null}
     * @Author Ocean_IT
     * @Email 14312400@qq.com
     * @Date 2022/10/5 14:27
     */
    @Transactional
    public BaseResponse del_videoroom(String token){
        List<CreateRoom> createRoomList = createRoomDao.get_createRoomList(TokenUtils.getOpenid(token));
        if(createRoomList.size() == 0){
            return RespGenerator.fail("400","未查询到你要删除房间的信息!");
        }
        Long vrid = createRoomList.get(0).getVrId();
        Assert.isTrue(createRoomDao.del_createRoom(vrid),"删除房间记录失败");
        Assert.isTrue(videoroomDao.del_videoroom(vrid),"删除房间信息失败");
        Assert.isTrue(userDao.put_createVideo_num(TokenUtils.getOpenid(token),false),"房间数更新失败!");
        return RespGenerator.success_noData("房间删除成功!");
    }

Controller控制器层

VideoroomController

代码语言:javascript
复制
    //删除房间信息
    @DeleteMapping("/videoroom")
    public BaseResponse del_videoroom(HttpServletRequest request){
        return videoroomService.del_videoroom(request.getHeader("token"));
    }

调用方法

url:http://127.0.0.1:8080/videoroomcontroller/videoroom/{vid} Methed:DELETE Header:token return:json

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前期准备
  • 查看已创建的房间
    • Dao数据持久层
      • Service服务层
        • Controller控制器层
          • 调用方法
          • 创建房间
            • Mapper编写
              • Mapper接口暴露
                • Dao数据持久层
                  • Service服务层
                    • Controller控制器层
                      • 调用方法
                      • 修改房间信息
                        • Dao数据持久层
                          • Service服务层
                            • Controller控制器层
                              • 调用方法
                              • 删除房间
                                • Dao数据持久层
                                  • Service服务层
                                    • Controller控制器层
                                      • 调用方法
                                      领券
                                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档