diff --git a/src/main/java/com/guo/learningprogresstracker/controller/reportFragmentsController.java b/src/main/java/com/guo/learningprogresstracker/controller/reportFragmentsController.java new file mode 100644 index 0000000..40f4b2b --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/controller/reportFragmentsController.java @@ -0,0 +1,35 @@ +package com.guo.learningprogresstracker.controller; + +import com.guo.learningprogresstracker.dto.request.CreateFragmentsRequest; +import com.guo.learningprogresstracker.entity.CommonResult; +import com.guo.learningprogresstracker.exception.NotFindEntitiesException; +import com.guo.learningprogresstracker.service.impl.StudyReportFragmentsServiceImpl; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.validation.annotation.Validated; +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.RestController; + +/** + * 学习残片控制层 + */ +@RequestMapping("/report-fragments") +@RestController +@Validated +@RequiredArgsConstructor +public class reportFragmentsController { + private final StudyReportFragmentsServiceImpl studyReportFragmentsServiceImpl; + + /** + * 创建学习残片 + * @param request + * @return + */ + @PostMapping + public CommonResult createFragments(@Valid @RequestBody CreateFragmentsRequest request) throws NotFindEntitiesException { + studyReportFragmentsServiceImpl.createFragments(request); + return CommonResult.success(); + } +} diff --git a/src/main/java/com/guo/learningprogresstracker/dto/request/CreateFragmentsRequest.java b/src/main/java/com/guo/learningprogresstracker/dto/request/CreateFragmentsRequest.java new file mode 100644 index 0000000..53756b9 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/dto/request/CreateFragmentsRequest.java @@ -0,0 +1,21 @@ +package com.guo.learningprogresstracker.dto.request; + +import jakarta.validation.constraints.NotBlank; +import lombok.Data; + +/** + * @author guo + */ +@Data +public class CreateFragmentsRequest { + /** + * 会话编号 + */ + private String sessionNum; + + /** + * 残片内容,学习内容的描述 + */ + @NotBlank(message = "啊?无字天书?") + private String content; +} diff --git a/src/main/java/com/guo/learningprogresstracker/entity/StudyReportFragmentsEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/StudyReportFragmentsEntity.java index be35614..72b3bc5 100644 --- a/src/main/java/com/guo/learningprogresstracker/entity/StudyReportFragmentsEntity.java +++ b/src/main/java/com/guo/learningprogresstracker/entity/StudyReportFragmentsEntity.java @@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; -import java.time.LocalDateTime; + import lombok.Data; /** @@ -18,14 +18,14 @@ public class StudyReportFragmentsEntity extends BaseEntity implements Serializab /** * */ - @TableId(value = "fragment_id", type = IdType.AUTO) - private Integer fragmentId; + @TableId(value = "id", type = IdType.AUTO) + private Integer id; /** * */ - @TableField(value = "session_id") - private Integer sessionId; + @TableField(value = "session_num") + private String sessionNum; /** * 学习内容的描述 diff --git a/src/main/java/com/guo/learningprogresstracker/mapStruct/FragmentsConvert.java b/src/main/java/com/guo/learningprogresstracker/mapStruct/FragmentsConvert.java new file mode 100644 index 0000000..896b723 --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/mapStruct/FragmentsConvert.java @@ -0,0 +1,14 @@ +package com.guo.learningprogresstracker.mapStruct; + +import com.guo.learningprogresstracker.dto.request.CreateFragmentsRequest; +import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; +import org.mapstruct.factory.Mappers; + +@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface FragmentsConvert { + FragmentsConvert MAPPER = Mappers.getMapper(FragmentsConvert.class); + + StudyReportFragmentsEntity toFragmentsEntity(CreateFragmentsRequest request); +} diff --git a/src/main/java/com/guo/learningprogresstracker/service/StudyReportFragmentsService.java b/src/main/java/com/guo/learningprogresstracker/service/StudyReportFragmentsService.java index f81d3fc..6e9b5f8 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/StudyReportFragmentsService.java +++ b/src/main/java/com/guo/learningprogresstracker/service/StudyReportFragmentsService.java @@ -1,7 +1,9 @@ package com.guo.learningprogresstracker.service; +import com.guo.learningprogresstracker.dto.request.CreateFragmentsRequest; import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity; import com.baomidou.mybatisplus.extension.service.IService; +import com.guo.learningprogresstracker.exception.NotFindEntitiesException; /** * @author guo @@ -10,4 +12,5 @@ import com.baomidou.mybatisplus.extension.service.IService; */ public interface StudyReportFragmentsService extends IService { + void createFragments(CreateFragmentsRequest request) throws NotFindEntitiesException; } diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java index dd9cc26..5bc0c9e 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/StudyReportFragmentsServiceImpl.java @@ -1,7 +1,12 @@ package com.guo.learningprogresstracker.service.impl; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.guo.learningprogresstracker.dto.request.CreateFragmentsRequest; import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity; +import com.guo.learningprogresstracker.entity.StudySessionsEntity; +import com.guo.learningprogresstracker.exception.NotFindEntitiesException; +import com.guo.learningprogresstracker.mapStruct.FragmentsConvert; import com.guo.learningprogresstracker.service.StudyReportFragmentsService; import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper; import org.springframework.stereotype.Service; @@ -15,6 +20,22 @@ import org.springframework.stereotype.Service; public class StudyReportFragmentsServiceImpl extends ServiceImpl implements StudyReportFragmentsService{ + private final StudySessionsServiceImpl studySessionsServiceImpl; + + public StudyReportFragmentsServiceImpl(StudySessionsServiceImpl studySessionsServiceImpl) { + this.studySessionsServiceImpl = studySessionsServiceImpl; + } + + @Override + public void createFragments(CreateFragmentsRequest request) throws NotFindEntitiesException { + StudyReportFragmentsEntity entity = FragmentsConvert.MAPPER.toFragmentsEntity(request); + if (studySessionsServiceImpl.exists(Wrappers.lambdaQuery(StudySessionsEntity.class) + .eq(StudySessionsEntity::getSessionNum,entity.getSessionNum()))) { + this.save(entity); + }else { + throw new NotFindEntitiesException(String.format("未能找到学习会话sessionNum[%s]",entity.getSessionNum())); + } + } }