Java的新项目学成在线笔记-day11(二)
作者:互联网
3.2.3修改课程发布
在课程管理服务定义dao:
1)创建course_pub表的dao
[mw_shl_code=applescript,true]public interface CoursePubRepository extends JpaRepository<CoursePub, String> { }[/mw_shl_code]
2) 修改课程发布service
[mw_shl_code=applescript,true] //保存CoursePub
public CoursePub saveCoursePub(String id, CoursePub coursePub){
if(StringUtils.isNotEmpty(id)){
ExceptionCast.cast(CourseCode.COURSE_PUBLISH_COURSEIDISNULL);
}
CoursePub coursePubNew = null;
Optional<CoursePub> coursePubOptional = coursePubRepository.findById(id);
if(coursePubOptional.isPresent()){
coursePubNew = coursePubOptional.get();
}
if(coursePubNew == null){
coursePubNew = new CoursePub();
}
BeanUtils.copyProperties(coursePub,coursePubNew);
//设置主键
coursePubNew.setId(id);
//更新时间戳为最新时间
coursePub.setTimestamp(new Date());
//发布时间
[/mw_shl_code]
[mw_shl_code=applescript,true] SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY‐MM‐dd HH:mm:ss"); String date = simpleDateFormat.format(new Date());
coursePub.setPubTime(date);
coursePubRepository.save(coursePub);
return coursePub;
}
//创建coursePub对象
private CoursePub createCoursePub(String id){
CoursePub coursePub = new CoursePub();
coursePub.setId(id);
//基础信息
Optional<CourseBase> courseBaseOptional = courseBaseRepository.findById(id);
if(courseBaseOptional == null){
CourseBase courseBase = courseBaseOptional.get();
BeanUtils.copyProperties(courseBase, coursePub);
}
//查询课程图片
Optional<CoursePic> picOptional = coursePicRepository.findById(id);
if(picOptional.isPresent()){
CoursePic coursePic = picOptional.get();
BeanUtils.copyProperties(coursePic, coursePub);
}
//课程营销信息
Optional<CourseMarket> marketOptional = courseMarketRepository.findById(id);
if(marketOptional.isPresent()){
CourseMarket courseMarket = marketOptional.get();
BeanUtils.copyProperties(courseMarket, coursePub);
}
//课程计划
TeachplanNode teachplanNode = teachplanMapper.selectList(id);
//将课程计划转成json
String teachplanString = JSON.toJSONString(teachplanNode);
coursePub.setTeachplan(teachplanString);
return coursePub;
}[/mw_shl_code]修改课程发布方法,添加调用saveCoursePub方法的代码,添加部分的代码如下:
[mw_shl_code=applescript,true]//课程发布
@Transactional
public CoursePublishResult publish(String courseId){
....
//创建课程索引
//创建课程索引信息
CoursePub coursePub = createCoursePub(courseId);
//向数据库保存课程索引信息
CoursePub newCoursePub = saveCoursePub(courseId, coursePub);
if(newCoursePub==null){
//创建课程索引信息失败
ExceptionCast.cast(CourseCode.COURSE_PUBLISH_CREATE_INDEX_ERROR);
}
....
}
[/mw_shl_code]
标签:学成,code,Java,coursePub,mw,day11,shl,id,CoursePub 来源: https://blog.51cto.com/13517854/2397727