19-课程计划添加-接口开发

本文介绍了如何在开发中实现课程计划表(teachplan)的增删操作,包括获取根节点、根据用户选择级别添加内容,以及使用Spring Boot和Hibernate的Repository进行数据库交互。关键步骤包括验证输入、查询根节点、设置节点等级并保存至数据库。
摘要由CSDN通过智能技术生成

开发逻辑

在这里插入图片描述
course_base这个是课程列表。
在这里插入图片描述
teachplan这个是课程计划表
就是某课程下计划讲什么重点内容。

开发步骤

第一步:
先查询Bootstrap开发框架,id=4028e581617f945f01617f9dabc40000 这个课程的根节点,如果这个课程没有根节点就根据这个课程的名称添加为根节点,如果查询到根节点,就返回这个根节点的id。
也是就service中的 getTeachplanRoot(String courseId)方法。

第二步:
teachplan表中的grade字段代表重点内容的级别,1级代表一级节点,2:代表2级节点,3:代表3级节点。
当用户不选择父节点,就是2级节点(因为1级节点是课程名称),如果选择父节点就是3级节点。
然后将前端提交过来的内容添加到teachplan表中
在这里插入图片描述

添加api接口

在这里插入图片描述

添加controller

在这里插入图片描述

//因为这个方法是post方法,所以需要将json转为java对象。所以通过@RequestBody 这个参数可以转化
@RequestBody  
//这个是事务处理
    @Transactional

添加service

在这里插入图片描述
具体service代码如下

    @Autowired
    TeachplanRepository teachplanRepository;
    
    @Transactional
    public ResponseResult addTeachplan(Teachplan teachplan) {

        if(teachplan == null ||
                StringUtils.isEmpty(teachplan.getPname()) ||
                StringUtils.isEmpty(teachplan.getCourseid())){
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }
        //课程id
        String courseid = teachplan.getCourseid();
        //父结点的id
        String parentid = teachplan.getParentid();
        if(StringUtils.isEmpty(parentid)){
            //获取课程的根结点
            parentid = getTeachplanRoot(courseid);
        }
        //查询根结点信息
        Optional<Teachplan> optional = teachplanRepository.findById(parentid);
        Teachplan teachplan1 = optional.get();
        //父结点的级别
        String parent_grade = teachplan1.getGrade();
        //创建一个新结点准备添加
        Teachplan teachplanNew = new Teachplan();
        //将teachplan的属性拷贝到teachplanNew中
        BeanUtils.copyProperties(teachplan,teachplanNew);
        //要设置必要的属性
        teachplanNew.setParentid(parentid);
        if(parent_grade.equals("1")){
            teachplanNew.setGrade("2");
        }else{
            teachplanNew.setGrade("3");
        }
        teachplanNew.setStatus("0");//未发布
        teachplanRepository.save(teachplanNew);
        return new ResponseResult(CommonCode.SUCCESS);
    }
    //获取课程的根结点
    public String getTeachplanRoot(String courseId){
        Optional<CourseBase> optional = courseBaseRepository.findById(courseId);
        if(!optional.isPresent()){
            return null;
        }
        CourseBase courseBase = optional.get();
        //调用dao查询teachplan表得到该课程的根结点(一级结点)
        List<Teachplan> teachplanList = teachplanRepository.findByCourseidAndParentid(courseId, "0");
        if(teachplanList == null || teachplanList.size()<=0){
            //新添加一个课程的根结点
            Teachplan teachplan = new Teachplan();
            teachplan.setCourseid(courseId);
            teachplan.setParentid("0");
            teachplan.setGrade("1");//一级结点
            teachplan.setStatus("0");
            teachplan.setPname(courseBase.getName());
            teachplanRepository.save(teachplan);
            return teachplan.getId();

        }
        //返回根结点的id
        return teachplanList.get(0).getId();

    }

添加Repository

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值