[xingyu.guo] feat: 【创建学习残片】API

This commit is contained in:
guo
2024-09-22 10:21:54 +08:00
parent bbecc86df1
commit da6a25996d
6 changed files with 99 additions and 5 deletions
@@ -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();
}
}
@@ -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;
}
@@ -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;
/**
* 学习内容的描述
@@ -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);
}
@@ -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<StudyReportFragmentsEntity> {
void createFragments(CreateFragmentsRequest request) throws NotFindEntitiesException;
}
@@ -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<StudyReportFragmentsMapper, StudyReportFragmentsEntity>
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()));
}
}
}