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

2022-12-27 15:58:12 浏览数 (1)

本期代码量较大,且涉及多表操作逻辑复杂建议配合目录食用!每个功能的编写顺序都是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

0 人点赞