From 81d443f76a41aa0ceb592a4ed7417d9e2d05f62c Mon Sep 17 00:00:00 2001 From: xiaoming <31494286+gitdq1016@users.noreply.github.com> Date: Sun, 15 Sep 2024 15:30:16 +0800 Subject: [PATCH 1/5] add course --- .../officialsite/common/enums/CodeEnums.java | 1 + .../com/dl/officialsite/course/Course.java | 61 +++++++++++++ .../officialsite/course/CourseController.java | 68 +++++++++++++++ .../officialsite/course/CourseRepository.java | 11 +++ .../dl/officialsite/course/CourseService.java | 87 +++++++++++++++++++ .../course/constant/CourseStatusEnums.java | 24 +++++ .../officialsite/course/vo/CourseAddVo.java | 19 ++++ .../officialsite/course/vo/CourseQueryVo.java | 36 ++++++++ .../course/vo/CourseSearchVo.java | 21 +++++ .../course/vo/CourseUpdateVo.java | 26 ++++++ .../com/dl/officialsite/sharing/Share.java | 6 ++ .../sharing/ShareCourseController.java | 39 +++++++++ .../sharing/ShareCourseService.java | 40 +++++++++ .../sharing/SharingRepository.java | 9 ++ .../officialsite/sharing/SharingService.java | 1 + .../sharing/model/req/CourseShareAddReq.java | 27 ++++++ .../model/req/CourseShareDeleteReq.java | 17 ++++ .../sharing/model/req/UpdateSharingReq.java | 7 ++ 18 files changed, 500 insertions(+) create mode 100644 src/main/java/com/dl/officialsite/course/Course.java create mode 100644 src/main/java/com/dl/officialsite/course/CourseController.java create mode 100644 src/main/java/com/dl/officialsite/course/CourseRepository.java create mode 100644 src/main/java/com/dl/officialsite/course/CourseService.java create mode 100644 src/main/java/com/dl/officialsite/course/constant/CourseStatusEnums.java create mode 100644 src/main/java/com/dl/officialsite/course/vo/CourseAddVo.java create mode 100644 src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java create mode 100644 src/main/java/com/dl/officialsite/course/vo/CourseSearchVo.java create mode 100644 src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java create mode 100644 src/main/java/com/dl/officialsite/sharing/ShareCourseController.java create mode 100644 src/main/java/com/dl/officialsite/sharing/ShareCourseService.java create mode 100644 src/main/java/com/dl/officialsite/sharing/model/req/CourseShareAddReq.java create mode 100644 src/main/java/com/dl/officialsite/sharing/model/req/CourseShareDeleteReq.java diff --git a/src/main/java/com/dl/officialsite/common/enums/CodeEnums.java b/src/main/java/com/dl/officialsite/common/enums/CodeEnums.java index 49f9120a..4223cdb6 100644 --- a/src/main/java/com/dl/officialsite/common/enums/CodeEnums.java +++ b/src/main/java/com/dl/officialsite/common/enums/CodeEnums.java @@ -44,6 +44,7 @@ public enum CodeEnums { APPLY_REPEAT("1024", "apply repeat"), NOT_FOUND_BOUNTY("1025", "not found bounty"), + NOT_FOUND_COURSE("1026", "not found course"), // distribute ID_NEED_EMPTY("6000", "Id need empty"), diff --git a/src/main/java/com/dl/officialsite/course/Course.java b/src/main/java/com/dl/officialsite/course/Course.java new file mode 100644 index 00000000..0af8dd67 --- /dev/null +++ b/src/main/java/com/dl/officialsite/course/Course.java @@ -0,0 +1,61 @@ +package com.dl.officialsite.course; + +import lombok.Data; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityListeners; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Data +@Entity +@EntityListeners(AuditingEntityListener.class) +@Table(name = "course") +public class Course { + /** + * create table course + * ( + * id bigint auto_increment + * primary key, + * course_name varchar(255) null comment '课程名称', + * remark varchar(1024) null comment '课程简介', + * cooperate_community varchar(100) null comment '合作社区', + * creator varchar(64) null comment '创建者', + * updater varchar(64) null comment '更新者', + * create_time bigint null comment '创建时间', + * update_time bigint null comment '更新时间', + * status int null comment '状态,0:进行中,1:已结束' + * ) + * comment '课程表'; + */ + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + //课程名称 + private String courseName; + //课程简介 + private String remark; + //合作社区 + private String cooperateCommunity; + //创建者 + private String creator; + //更新者 + private String updater; + //创建时间 + @CreatedDate + @Column(updatable = false) + private Long createTime; + //更新时间 + @LastModifiedDate + @Column( updatable = false ,nullable = false) + private Long updateTime; + //状态,0:进行中,1:已结束 + private Integer status; +} diff --git a/src/main/java/com/dl/officialsite/course/CourseController.java b/src/main/java/com/dl/officialsite/course/CourseController.java new file mode 100644 index 00000000..6b1739de --- /dev/null +++ b/src/main/java/com/dl/officialsite/course/CourseController.java @@ -0,0 +1,68 @@ +package com.dl.officialsite.course; + +import com.dl.officialsite.bounty.vo.BountySearchVo; +import com.dl.officialsite.bounty.vo.BountyVo; +import com.dl.officialsite.common.base.BaseResponse; +import com.dl.officialsite.course.vo.CourseAddVo; +import com.dl.officialsite.course.vo.CourseQueryVo; +import com.dl.officialsite.course.vo.CourseSearchVo; +import com.dl.officialsite.course.vo.CourseUpdateVo; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/12 7:37 PM + **/ +@RestController +@RequestMapping("/course") +@Slf4j +public class CourseController { + + @Resource + private CourseService courseService; + + + @PostMapping("/add") + public BaseResponse add(@RequestBody CourseAddVo courseAddVo, @RequestParam String address) { + return BaseResponse.successWithData(courseService.add(courseAddVo, address)); + } + + @PostMapping("/update") + public BaseResponse update(@RequestBody CourseUpdateVo courseUpdateVo, @RequestParam String address) { + courseService.update(courseUpdateVo, address); + return BaseResponse.successWithData(null); + } + + @PostMapping("/detail") + public BaseResponse detail(@RequestParam Long id, @RequestParam String address) { + return BaseResponse.successWithData(courseService.detail(id)); + } + + @PostMapping("/list") + public BaseResponse list(@RequestBody CourseSearchVo courseSearchVo, + @RequestParam(defaultValue = "1") Integer pageNumber, + @RequestParam(defaultValue = "10") Integer pageSize) { + Pageable pageable = PageRequest.of(pageNumber - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime")); + Page page = courseService.search(courseSearchVo, pageable); + return BaseResponse.successWithData(page); + } + + @PostMapping("/delete") + public BaseResponse delete(@RequestParam Long id, @RequestParam String address) { + courseService.delete(id); + return BaseResponse.successWithData(null); + } + +} diff --git a/src/main/java/com/dl/officialsite/course/CourseRepository.java b/src/main/java/com/dl/officialsite/course/CourseRepository.java new file mode 100644 index 00000000..05a43a01 --- /dev/null +++ b/src/main/java/com/dl/officialsite/course/CourseRepository.java @@ -0,0 +1,11 @@ +package com.dl.officialsite.course; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + + +public interface CourseRepository extends JpaRepository, JpaSpecificationExecutor { + + + +} diff --git a/src/main/java/com/dl/officialsite/course/CourseService.java b/src/main/java/com/dl/officialsite/course/CourseService.java new file mode 100644 index 00000000..96863d4b --- /dev/null +++ b/src/main/java/com/dl/officialsite/course/CourseService.java @@ -0,0 +1,87 @@ +package com.dl.officialsite.course; + +import com.dl.officialsite.common.exception.BizException; +import com.dl.officialsite.course.constant.CourseStatusEnums; +import com.dl.officialsite.course.vo.CourseAddVo; +import com.dl.officialsite.course.vo.CourseQueryVo; +import com.dl.officialsite.course.vo.CourseSearchVo; +import com.dl.officialsite.course.vo.CourseUpdateVo; +import com.dl.officialsite.team.Team; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import javax.persistence.criteria.Predicate; +import java.util.ArrayList; +import java.util.List; + +import static com.dl.officialsite.common.enums.CodeEnums.NOT_FOUND_COURSE; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/12 7:32 PM + **/ +@Service +@Slf4j +public class CourseService { + + @Resource + private CourseRepository courseRepository; + public Course add(CourseAddVo courseAddVo, String address) { + Course course = new Course(); + BeanUtils.copyProperties(courseAddVo, course); + course.setCreator(address); + course.setUpdater(address); + course.setStatus(CourseStatusEnums.ING.getCode()); + courseRepository.save(course); + return course; + } + + public void update(CourseUpdateVo courseUpdateVo, String address) { + Course course = courseRepository.findById(courseUpdateVo.getId()).orElseThrow(() -> new BizException(NOT_FOUND_COURSE.getCode(), NOT_FOUND_COURSE.getMsg())); + BeanUtils.copyProperties(courseUpdateVo, course); + course.setUpdater(address); + courseRepository.save(course); + } + + public Page search(CourseSearchVo courseSearchVo, Pageable pageable) { + Page all = courseRepository.findAll((root, query, criteriaBuilder) -> { + List predicates = new ArrayList<>(); + if (StringUtils.isNotBlank(courseSearchVo.getCourseName())) { + predicates.add(criteriaBuilder.like(root.get("courseName"), "%" + courseSearchVo.getCourseName() + "%")); + } + if (StringUtils.isNotBlank(courseSearchVo.getRemark())) { + predicates.add(criteriaBuilder.like(root.get("remark"), "%" + courseSearchVo.getRemark() + "%")); + } + if (StringUtils.isNotBlank(courseSearchVo.getCooperateCommunity())) { + predicates.add(criteriaBuilder.like(root.get("cooperateCommunity"), "%" + courseSearchVo.getCooperateCommunity() + "%")); + } + if (courseSearchVo.getStatus() != null) { + predicates.add(criteriaBuilder.equal(root.get("status"), courseSearchVo.getStatus())); + } + return criteriaBuilder.and(predicates.toArray(new Predicate[0])); + }, pageable); + return all.map(course -> { + CourseQueryVo courseQueryVo = new CourseQueryVo(); + BeanUtils.copyProperties(course, courseQueryVo); + return courseQueryVo; + }); + } + + public CourseQueryVo detail(Long id) { + Course course = courseRepository.findById(id).orElseThrow(() -> new BizException(NOT_FOUND_COURSE.getCode(), NOT_FOUND_COURSE.getMsg())); + CourseQueryVo courseQueryVo = new CourseQueryVo(); + BeanUtils.copyProperties(course, courseQueryVo); + return courseQueryVo; + } + + public void delete(Long id) { + Course course = courseRepository.findById(id).orElseThrow(() -> new BizException(NOT_FOUND_COURSE.getCode(), NOT_FOUND_COURSE.getMsg())); + courseRepository.delete(course); + } +} diff --git a/src/main/java/com/dl/officialsite/course/constant/CourseStatusEnums.java b/src/main/java/com/dl/officialsite/course/constant/CourseStatusEnums.java new file mode 100644 index 00000000..f690cb70 --- /dev/null +++ b/src/main/java/com/dl/officialsite/course/constant/CourseStatusEnums.java @@ -0,0 +1,24 @@ +package com.dl.officialsite.course.constant; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/12 7:40 PM + **/ +@Getter +@AllArgsConstructor +public enum CourseStatusEnums { + /** + * 状态,0:进行中,1:已结束 + */ + ING(0, "进行中"), + END(1, "已结束"); + + + private int code; + + private String message; +} diff --git a/src/main/java/com/dl/officialsite/course/vo/CourseAddVo.java b/src/main/java/com/dl/officialsite/course/vo/CourseAddVo.java new file mode 100644 index 00000000..cc255991 --- /dev/null +++ b/src/main/java/com/dl/officialsite/course/vo/CourseAddVo.java @@ -0,0 +1,19 @@ +package com.dl.officialsite.course.vo; + +import lombok.Data; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/12 7:35 PM + **/ +@Data +public class CourseAddVo { + + //课程名称 + private String courseName; + //课程简介 + private String remark; + //合作社区 + private String cooperateCommunity; +} diff --git a/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java b/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java new file mode 100644 index 00000000..be015921 --- /dev/null +++ b/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java @@ -0,0 +1,36 @@ +package com.dl.officialsite.course.vo; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import lombok.Data; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/12 7:50 PM + **/ +@Data +public class CourseQueryVo { + +// @JsonSerialize(using = ToStringSerializer.class) + private Long id; + //课程名称 + private String courseName; + //课程简介 + private String remark; + //合作社区 + private String cooperateCommunity; + //创建者 + private String creator; + //更新者 + private String updater; + //创建时间 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Long createTime; + //更新时间 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Long updateTime; + //状态,0:进行中,1:已结束 + private Integer status; +} diff --git a/src/main/java/com/dl/officialsite/course/vo/CourseSearchVo.java b/src/main/java/com/dl/officialsite/course/vo/CourseSearchVo.java new file mode 100644 index 00000000..78b28272 --- /dev/null +++ b/src/main/java/com/dl/officialsite/course/vo/CourseSearchVo.java @@ -0,0 +1,21 @@ +package com.dl.officialsite.course.vo; + +import lombok.Data; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/12 7:48 PM + **/ +@Data +public class CourseSearchVo { + + //课程名称 + private String courseName; + //课程简介 + private String remark; + //合作社区 + private String cooperateCommunity; + //课程状态 + private Integer status; +} diff --git a/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java b/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java new file mode 100644 index 00000000..c44b2439 --- /dev/null +++ b/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java @@ -0,0 +1,26 @@ +package com.dl.officialsite.course.vo; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import lombok.Data; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/12 7:44 PM + **/ +@Data +public class CourseUpdateVo { + + +// @JsonSerialize(using = ToStringSerializer.class) + private Long id; + //课程名称 + private String courseName; + //课程简介 + private String remark; + //合作社区 + private String cooperateCommunity; + //课程状态 + private Integer status; +} diff --git a/src/main/java/com/dl/officialsite/sharing/Share.java b/src/main/java/com/dl/officialsite/sharing/Share.java index 485b5c11..428d5ea6 100644 --- a/src/main/java/com/dl/officialsite/sharing/Share.java +++ b/src/main/java/com/dl/officialsite/sharing/Share.java @@ -139,6 +139,12 @@ public class Share { @Column(name = "tag") private String tag; + /** + * 课程Id + */ + @Column(name = "course_id") + private Long courseId; + @CreatedDate @Column(updatable = false) private Long createTime; diff --git a/src/main/java/com/dl/officialsite/sharing/ShareCourseController.java b/src/main/java/com/dl/officialsite/sharing/ShareCourseController.java new file mode 100644 index 00000000..d9367b8d --- /dev/null +++ b/src/main/java/com/dl/officialsite/sharing/ShareCourseController.java @@ -0,0 +1,39 @@ +package com.dl.officialsite.sharing; + +import com.dl.officialsite.common.base.BaseResponse; +import com.dl.officialsite.sharing.model.req.CourseShareAddReq; +import com.dl.officialsite.sharing.model.req.CourseShareDeleteReq; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/13 7:57 AM + **/ +@RestController +@RequestMapping("/shareCourse") +@Slf4j +public class ShareCourseController { + + @Resource + private ShareCourseService shareCourseService; + + @PostMapping("/addOrModify") + public BaseResponse addOrModifyShareCourse(@RequestBody CourseShareAddReq courseShareAddReq, @RequestParam String address) { + shareCourseService.addShareCourse(courseShareAddReq); + return BaseResponse.successWithData(null); + } + + @PostMapping("/delete") + public BaseResponse deleteShareCourse(@RequestBody CourseShareDeleteReq courseShareDeleteReq, @RequestParam String address) { + shareCourseService.deleteShareCourse(courseShareDeleteReq); + return BaseResponse.successWithData(null); + } +} diff --git a/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java b/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java new file mode 100644 index 00000000..31f895de --- /dev/null +++ b/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java @@ -0,0 +1,40 @@ +package com.dl.officialsite.sharing; + +import com.dl.officialsite.common.enums.CodeEnums; +import com.dl.officialsite.common.exception.BizException; +import com.dl.officialsite.course.Course; +import com.dl.officialsite.sharing.model.req.CourseShareAddReq; +import com.dl.officialsite.sharing.model.req.CourseShareDeleteReq; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +import static com.dl.officialsite.common.enums.CodeEnums.NOT_FOUND_COURSE; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/13 8:02 AM + **/ +@Service +@Slf4j +public class ShareCourseService { + + @Resource + private SharingRepository sharingRepository; + + public void addShareCourse(CourseShareAddReq courseShareAddReq){ + Long shareId = courseShareAddReq.getShareId(); + Long courseId = courseShareAddReq.getCourseId(); + Share share = sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg())); + share.setCourseId(courseId); + sharingRepository.save(share); + } + + public void deleteShareCourse(CourseShareDeleteReq courseShareDeleteReq){ + Long shareId = courseShareDeleteReq.getShareId(); +// sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg())); + sharingRepository.updateCourseIdToNull(shareId); + } +} diff --git a/src/main/java/com/dl/officialsite/sharing/SharingRepository.java b/src/main/java/com/dl/officialsite/sharing/SharingRepository.java index e86589ba..59b1d70b 100644 --- a/src/main/java/com/dl/officialsite/sharing/SharingRepository.java +++ b/src/main/java/com/dl/officialsite/sharing/SharingRepository.java @@ -5,9 +5,12 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import javax.transaction.Transactional; + public interface SharingRepository extends JpaRepository, JpaSpecificationExecutor { @Query(value = "select * from share limit :offset, :limit", nativeQuery = true) @@ -33,4 +36,10 @@ List findAllSharesByUidPaged(@Param("memberAddress") String memberAddress @Query(value = "SELECT presenter, COUNT(*) AS shareCount FROM share GROUP BY presenter ORDER" + " BY shareCount DESC LIMIT :rankNumber" ,nativeQuery = true) List findTopGroups(@Param("rankNumber") Integer rankNumber); + + //通过id将share表中的courseId字段置为null + @Modifying + @Transactional + @Query("update Share s set s.courseId = null where s.id = :id") + void updateCourseIdToNull(@Param("id") Long id); } diff --git a/src/main/java/com/dl/officialsite/sharing/SharingService.java b/src/main/java/com/dl/officialsite/sharing/SharingService.java index b48b7416..5432b39c 100644 --- a/src/main/java/com/dl/officialsite/sharing/SharingService.java +++ b/src/main/java/com/dl/officialsite/sharing/SharingService.java @@ -124,6 +124,7 @@ public void updateSharing(UpdateSharingReq req, String address) { sharing.setBilibiliLink(req.getBilibiliLink()); sharing.setYoutubeLink(req.getYoutubeLink()); sharing.setTag(req.getTag()); + sharing.setCourseId(req.getCourseId()); this.sharingRepository.save(sharing); /* SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); diff --git a/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareAddReq.java b/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareAddReq.java new file mode 100644 index 00000000..b472cbf5 --- /dev/null +++ b/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareAddReq.java @@ -0,0 +1,27 @@ +package com.dl.officialsite.sharing.model.req; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; +import lombok.Data; + +import java.util.List; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/13 7:59 AM + **/ +@Data +public class CourseShareAddReq { + + /** + * 课程id + */ +// @JsonSerialize(using = ToStringSerializer.class) + private Long courseId; + + /** + * 分享id列表 + */ + private Long shareId; +} diff --git a/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareDeleteReq.java b/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareDeleteReq.java new file mode 100644 index 00000000..b4d877a3 --- /dev/null +++ b/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareDeleteReq.java @@ -0,0 +1,17 @@ +package com.dl.officialsite.sharing.model.req; + +import lombok.Data; + +/** + * @Description + * @Author xiaoming + * @Date 2024/9/13 7:59 AM + **/ +@Data +public class CourseShareDeleteReq { + + /** + * 分享id列表 + */ + private Long shareId; +} diff --git a/src/main/java/com/dl/officialsite/sharing/model/req/UpdateSharingReq.java b/src/main/java/com/dl/officialsite/sharing/model/req/UpdateSharingReq.java index 7db99258..259f3349 100644 --- a/src/main/java/com/dl/officialsite/sharing/model/req/UpdateSharingReq.java +++ b/src/main/java/com/dl/officialsite/sharing/model/req/UpdateSharingReq.java @@ -5,6 +5,8 @@ import java.util.Date; import lombok.Data; +import javax.persistence.Column; + @Data public class UpdateSharingReq { @@ -78,4 +80,9 @@ public class UpdateSharingReq { * tag */ private String tag; + + /** + * 课程Id + */ + private Long courseId; } From 47c95bacb0f5aa590edc228ec42cc8831d6e52d5 Mon Sep 17 00:00:00 2001 From: xiaoming <31494286+gitdq1016@users.noreply.github.com> Date: Sun, 15 Sep 2024 15:31:03 +0800 Subject: [PATCH 2/5] add course --- .../java/com/dl/officialsite/sharing/ShareCourseService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java b/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java index 31f895de..2b2e5772 100644 --- a/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java +++ b/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java @@ -34,7 +34,7 @@ public void addShareCourse(CourseShareAddReq courseShareAddReq){ public void deleteShareCourse(CourseShareDeleteReq courseShareDeleteReq){ Long shareId = courseShareDeleteReq.getShareId(); -// sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg())); + sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg())); sharingRepository.updateCourseIdToNull(shareId); } } From 492508e25a91477a0f126f35c7ee1b14ad606af4 Mon Sep 17 00:00:00 2001 From: xiaoming <31494286+gitdq1016@users.noreply.github.com> Date: Sun, 15 Sep 2024 15:39:45 +0800 Subject: [PATCH 3/5] remove check --- .../java/com/dl/officialsite/course/CourseController.java | 4 +--- .../java/com/dl/officialsite/login/filter/LoginFilter.java | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/dl/officialsite/course/CourseController.java b/src/main/java/com/dl/officialsite/course/CourseController.java index 6b1739de..ca3b866d 100644 --- a/src/main/java/com/dl/officialsite/course/CourseController.java +++ b/src/main/java/com/dl/officialsite/course/CourseController.java @@ -1,7 +1,5 @@ package com.dl.officialsite.course; -import com.dl.officialsite.bounty.vo.BountySearchVo; -import com.dl.officialsite.bounty.vo.BountyVo; import com.dl.officialsite.common.base.BaseResponse; import com.dl.officialsite.course.vo.CourseAddVo; import com.dl.officialsite.course.vo.CourseQueryVo; @@ -46,7 +44,7 @@ public BaseResponse update(@RequestBody CourseUpdateVo courseUpdateVo, @RequestP } @PostMapping("/detail") - public BaseResponse detail(@RequestParam Long id, @RequestParam String address) { + public BaseResponse detail(@RequestParam Long id) { return BaseResponse.successWithData(courseService.detail(id)); } diff --git a/src/main/java/com/dl/officialsite/login/filter/LoginFilter.java b/src/main/java/com/dl/officialsite/login/filter/LoginFilter.java index 65ae66c2..6eb0dafa 100644 --- a/src/main/java/com/dl/officialsite/login/filter/LoginFilter.java +++ b/src/main/java/com/dl/officialsite/login/filter/LoginFilter.java @@ -71,6 +71,8 @@ public class LoginFilter extends OncePerRequestFilter { add("/share/search"); add("/share/query"); add("/sponsors/all"); + add("/course/detail"); + add("/course/list"); }} ; private Set noAddrCheckApis = new HashSet() {{ From 0cfb601c7c5bf64939c20fe551fbdaf9ed6351e4 Mon Sep 17 00:00:00 2001 From: xiaoming <31494286+gitdq1016@users.noreply.github.com> Date: Sun, 15 Sep 2024 16:59:54 +0800 Subject: [PATCH 4/5] modify course --- .../officialsite/course/CourseController.java | 9 ++- .../dl/officialsite/course/CourseService.java | 55 ++++++++++++++++++- .../officialsite/course/vo/CourseAddVo.java | 7 +++ .../officialsite/course/vo/CourseQueryVo.java | 4 ++ .../course/vo/CourseUpdateVo.java | 7 +++ .../sharing/ShareCourseController.java | 1 + .../sharing/ShareCourseService.java | 23 +++++--- .../sharing/SharingRepository.java | 11 ++++ .../sharing/model/req/CourseShareAddReq.java | 2 +- 9 files changed, 104 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/dl/officialsite/course/CourseController.java b/src/main/java/com/dl/officialsite/course/CourseController.java index ca3b866d..08b0d484 100644 --- a/src/main/java/com/dl/officialsite/course/CourseController.java +++ b/src/main/java/com/dl/officialsite/course/CourseController.java @@ -44,15 +44,18 @@ public BaseResponse update(@RequestBody CourseUpdateVo courseUpdateVo, @RequestP } @PostMapping("/detail") - public BaseResponse detail(@RequestParam Long id) { - return BaseResponse.successWithData(courseService.detail(id)); + public BaseResponse detail(@RequestParam Long id, @RequestParam(defaultValue = "1") Integer pageNumber, + @RequestParam(defaultValue = "10") Integer pageSize) { + Pageable pageable = PageRequest.of(pageNumber - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime")); + return BaseResponse.successWithData(courseService.detail(id, pageable)); } @PostMapping("/list") - public BaseResponse list(@RequestBody CourseSearchVo courseSearchVo, + public BaseResponse list( @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) { Pageable pageable = PageRequest.of(pageNumber - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime")); + CourseSearchVo courseSearchVo = new CourseSearchVo(); Page page = courseService.search(courseSearchVo, pageable); return BaseResponse.successWithData(page); } diff --git a/src/main/java/com/dl/officialsite/course/CourseService.java b/src/main/java/com/dl/officialsite/course/CourseService.java index 96863d4b..55fe6e72 100644 --- a/src/main/java/com/dl/officialsite/course/CourseService.java +++ b/src/main/java/com/dl/officialsite/course/CourseService.java @@ -1,23 +1,29 @@ package com.dl.officialsite.course; +import cn.hutool.core.collection.CollUtil; +import com.dl.officialsite.common.enums.CodeEnums; import com.dl.officialsite.common.exception.BizException; import com.dl.officialsite.course.constant.CourseStatusEnums; import com.dl.officialsite.course.vo.CourseAddVo; import com.dl.officialsite.course.vo.CourseQueryVo; import com.dl.officialsite.course.vo.CourseSearchVo; import com.dl.officialsite.course.vo.CourseUpdateVo; -import com.dl.officialsite.team.Team; +import com.dl.officialsite.sharing.Share; +import com.dl.officialsite.sharing.SharingRepository; +import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; - +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import javax.persistence.criteria.Predicate; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; import static com.dl.officialsite.common.enums.CodeEnums.NOT_FOUND_COURSE; @@ -32,6 +38,8 @@ public class CourseService { @Resource private CourseRepository courseRepository; + @Resource + private SharingRepository sharingRepository; public Course add(CourseAddVo courseAddVo, String address) { Course course = new Course(); BeanUtils.copyProperties(courseAddVo, course); @@ -39,14 +47,41 @@ public Course add(CourseAddVo courseAddVo, String address) { course.setUpdater(address); course.setStatus(CourseStatusEnums.ING.getCode()); courseRepository.save(course); + Long courseId = course.getId(); + + List shareIdList = courseAddVo.getShareIdList(); + shareIdList.forEach(shareId -> { + Share share = sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg())); + share.setCourseId(courseId); + sharingRepository.save(share); + }); return course; } + @Transactional(rollbackFor = Exception.class) public void update(CourseUpdateVo courseUpdateVo, String address) { + Long courseId = courseUpdateVo.getId(); Course course = courseRepository.findById(courseUpdateVo.getId()).orElseThrow(() -> new BizException(NOT_FOUND_COURSE.getCode(), NOT_FOUND_COURSE.getMsg())); BeanUtils.copyProperties(courseUpdateVo, course); course.setUpdater(address); courseRepository.save(course); + + List shareIdList = courseUpdateVo.getShareIdList(); + //先将分享的课程id设置为null + List oldShareList = sharingRepository.findByCourseId(courseId); + List oldIdList = oldShareList.stream().map(Share::getId).collect(Collectors.toList()); + if(CollUtil.isNotEmpty(oldShareList)){ + Collection subtract = CollUtil.subtract(oldIdList, shareIdList); + if(CollUtil.isNotEmpty(subtract)){ + sharingRepository.updateCourseIdToNull(Lists.newArrayList(subtract)); + } + } + //设置新的分享课程id + shareIdList.forEach(shareId -> { + Share share = sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg())); + share.setCourseId(courseId); + sharingRepository.save(share); + }); } public Page search(CourseSearchVo courseSearchVo, Pageable pageable) { @@ -73,15 +108,29 @@ public Page search(CourseSearchVo courseSearchVo, Pageable pageab }); } - public CourseQueryVo detail(Long id) { + public CourseQueryVo detail(Long id, Pageable pageable) { Course course = courseRepository.findById(id).orElseThrow(() -> new BizException(NOT_FOUND_COURSE.getCode(), NOT_FOUND_COURSE.getMsg())); CourseQueryVo courseQueryVo = new CourseQueryVo(); BeanUtils.copyProperties(course, courseQueryVo); + //通过courseId分页查找share + Page all = sharingRepository.findAll((root, query, criteriaBuilder) -> { + List predicates = new ArrayList<>(); + predicates.add(criteriaBuilder.equal(root.get("courseId"), id)); + return criteriaBuilder.and(predicates.toArray(new Predicate[0])); + }, pageable); + courseQueryVo.setSharePage(all); return courseQueryVo; } public void delete(Long id) { Course course = courseRepository.findById(id).orElseThrow(() -> new BizException(NOT_FOUND_COURSE.getCode(), NOT_FOUND_COURSE.getMsg())); courseRepository.delete(course); + + //先将分享的课程id设置为null + List oldShareList = sharingRepository.findByCourseId(id); + List oldIdList = oldShareList.stream().map(Share::getId).collect(Collectors.toList()); + if(CollUtil.isNotEmpty(oldShareList)){ + sharingRepository.updateCourseIdToNull(oldIdList); + } } } diff --git a/src/main/java/com/dl/officialsite/course/vo/CourseAddVo.java b/src/main/java/com/dl/officialsite/course/vo/CourseAddVo.java index cc255991..0df7ac69 100644 --- a/src/main/java/com/dl/officialsite/course/vo/CourseAddVo.java +++ b/src/main/java/com/dl/officialsite/course/vo/CourseAddVo.java @@ -2,6 +2,8 @@ import lombok.Data; +import java.util.List; + /** * @Description * @Author xiaoming @@ -16,4 +18,9 @@ public class CourseAddVo { private String remark; //合作社区 private String cooperateCommunity; + + /** + * 分享id列表 + */ + private List shareIdList; } diff --git a/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java b/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java index be015921..8fd6ef41 100644 --- a/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java +++ b/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java @@ -1,9 +1,11 @@ package com.dl.officialsite.course.vo; +import com.dl.officialsite.sharing.Share; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import lombok.Data; +import org.springframework.data.domain.Page; /** * @Description @@ -33,4 +35,6 @@ public class CourseQueryVo { private Long updateTime; //状态,0:进行中,1:已结束 private Integer status; + + private Page sharePage; } diff --git a/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java b/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java index c44b2439..7cddbd76 100644 --- a/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java +++ b/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import lombok.Data; +import java.util.List; + /** * @Description * @Author xiaoming @@ -23,4 +25,9 @@ public class CourseUpdateVo { private String cooperateCommunity; //课程状态 private Integer status; + + /** + * 分享id列表 + */ + private List shareIdList; } diff --git a/src/main/java/com/dl/officialsite/sharing/ShareCourseController.java b/src/main/java/com/dl/officialsite/sharing/ShareCourseController.java index d9367b8d..2fa04b2b 100644 --- a/src/main/java/com/dl/officialsite/sharing/ShareCourseController.java +++ b/src/main/java/com/dl/officialsite/sharing/ShareCourseController.java @@ -20,6 +20,7 @@ @RestController @RequestMapping("/shareCourse") @Slf4j +@Deprecated public class ShareCourseController { @Resource diff --git a/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java b/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java index 2b2e5772..20207101 100644 --- a/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java +++ b/src/main/java/com/dl/officialsite/sharing/ShareCourseService.java @@ -1,16 +1,15 @@ package com.dl.officialsite.sharing; +import cn.hutool.core.collection.CollUtil; import com.dl.officialsite.common.enums.CodeEnums; import com.dl.officialsite.common.exception.BizException; -import com.dl.officialsite.course.Course; import com.dl.officialsite.sharing.model.req.CourseShareAddReq; import com.dl.officialsite.sharing.model.req.CourseShareDeleteReq; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; - import javax.annotation.Resource; - -import static com.dl.officialsite.common.enums.CodeEnums.NOT_FOUND_COURSE; +import java.util.List; +import java.util.stream.Collectors; /** * @Description @@ -25,11 +24,19 @@ public class ShareCourseService { private SharingRepository sharingRepository; public void addShareCourse(CourseShareAddReq courseShareAddReq){ - Long shareId = courseShareAddReq.getShareId(); + List shareIdList = courseShareAddReq.getShareIdList(); Long courseId = courseShareAddReq.getCourseId(); - Share share = sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg())); - share.setCourseId(courseId); - sharingRepository.save(share); + //先将分享的课程id设置为null + List oldShareList = sharingRepository.findByCourseId(courseId); + List oldIdList = oldShareList.stream().map(Share::getId).collect(Collectors.toList()); + if(CollUtil.isNotEmpty(oldShareList)){ + sharingRepository.updateCourseIdToNull(oldIdList); + } + shareIdList.forEach(shareId -> { + Share share = sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg())); + share.setCourseId(courseId); + sharingRepository.save(share); + }); } public void deleteShareCourse(CourseShareDeleteReq courseShareDeleteReq){ diff --git a/src/main/java/com/dl/officialsite/sharing/SharingRepository.java b/src/main/java/com/dl/officialsite/sharing/SharingRepository.java index 59b1d70b..8af8f4d2 100644 --- a/src/main/java/com/dl/officialsite/sharing/SharingRepository.java +++ b/src/main/java/com/dl/officialsite/sharing/SharingRepository.java @@ -42,4 +42,15 @@ List findAllSharesByUidPaged(@Param("memberAddress") String memberAddress @Transactional @Query("update Share s set s.courseId = null where s.id = :id") void updateCourseIdToNull(@Param("id") Long id); + + + //通过idList将share表中的courseId字段置为null + @Modifying + @Transactional + @Query("update Share s set s.courseId = null where s.id in :idList") + void updateCourseIdToNull(@Param("idList") List idList); + + //通过courseId查询share表中的数据 + @Query(value = "select * from share s where s.course_id = :courseId", nativeQuery = true) + List findByCourseId(Long courseId); } diff --git a/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareAddReq.java b/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareAddReq.java index b472cbf5..d4d10274 100644 --- a/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareAddReq.java +++ b/src/main/java/com/dl/officialsite/sharing/model/req/CourseShareAddReq.java @@ -23,5 +23,5 @@ public class CourseShareAddReq { /** * 分享id列表 */ - private Long shareId; + private List shareIdList; } From 496b85328f780e338609ad17f5297f105dd38601 Mon Sep 17 00:00:00 2001 From: xiaoming <31494286+gitdq1016@users.noreply.github.com> Date: Sun, 15 Sep 2024 17:21:52 +0800 Subject: [PATCH 5/5] add req --- .../java/com/dl/officialsite/course/vo/CourseQueryVo.java | 4 ++-- .../java/com/dl/officialsite/course/vo/CourseUpdateVo.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java b/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java index 8fd6ef41..5de14668 100644 --- a/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java +++ b/src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java @@ -33,8 +33,8 @@ public class CourseQueryVo { //更新时间 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Long updateTime; - //状态,0:进行中,1:已结束 - private Integer status; +// //状态,0:进行中,1:已结束 +// private Integer status; private Page sharePage; } diff --git a/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java b/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java index 7cddbd76..2bab51af 100644 --- a/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java +++ b/src/main/java/com/dl/officialsite/course/vo/CourseUpdateVo.java @@ -23,8 +23,8 @@ public class CourseUpdateVo { private String remark; //合作社区 private String cooperateCommunity; - //课程状态 - private Integer status; +// //课程状态 +// private Integer status; /** * 分享id列表