-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from dragove/feature/copilot-set
feat: 作业集(收藏)功能
- Loading branch information
Showing
27 changed files
with
816 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/plus/maa/backend/common/model/CopilotSetType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package plus.maa.backend.common.model; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* @author dragove | ||
* create on 2024-01-01 | ||
*/ | ||
public interface CopilotSetType { | ||
|
||
List<Long> getCopilotIds(); | ||
|
||
default List<Long> getDistinctIdsAndCheck() { | ||
List<Long> copilotIds = getCopilotIds(); | ||
if (copilotIds == null) { | ||
return Collections.emptyList(); | ||
} | ||
if (copilotIds.isEmpty() || copilotIds.size() == 1) { | ||
return getCopilotIds(); | ||
} | ||
copilotIds = copilotIds.stream().distinct().toList(); | ||
Assert.state(copilotIds.size() <= 1000, "作业集总作业数量不能超过1000条"); | ||
return copilotIds; | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/plus/maa/backend/common/utils/IdComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package plus.maa.backend.common.utils; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
import org.springframework.stereotype.Component; | ||
import plus.maa.backend.repository.entity.CollectionMeta; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.function.Function; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class IdComponent { | ||
private final MongoTemplate mongoTemplate; | ||
private final Map<String, AtomicLong> CURRENT_ID_MAP = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* 获取id数据 | ||
* @param meta 集合元数据 | ||
* @return 新的id | ||
*/ | ||
public <T> long getId(CollectionMeta<T> meta) { | ||
Class<T> cls = meta.entityClass(); | ||
String collectionName = mongoTemplate.getCollectionName(cls); | ||
AtomicLong v = CURRENT_ID_MAP.get(collectionName); | ||
if (v == null) { | ||
synchronized (cls) { | ||
v = CURRENT_ID_MAP.get(collectionName); | ||
if (v == null) { | ||
v = new AtomicLong(getMax(cls, meta.idGetter(), meta.incIdField())); | ||
log.info("初始化获取 collection: {} 的最大 id,id: {}", collectionName, v.get()); | ||
CURRENT_ID_MAP.put(collectionName, v); | ||
} | ||
} | ||
} | ||
return v.incrementAndGet(); | ||
} | ||
|
||
private <T> Long getMax(Class<T> entityClass, Function<T, Long> idGetter, String fieldName) { | ||
return Optional.ofNullable(mongoTemplate.findOne( | ||
new Query().with(Sort.by(fieldName).descending()).limit(1), | ||
entityClass)) | ||
.map(idGetter) | ||
.orElse(20000L); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/plus/maa/backend/common/utils/converter/CopilotSetConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package plus.maa.backend.common.utils.converter; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import plus.maa.backend.controller.request.copilotset.CopilotSetCreateReq; | ||
import plus.maa.backend.controller.response.CopilotSetRes; | ||
import plus.maa.backend.controller.response.user.CopilotSetListRes; | ||
import plus.maa.backend.repository.entity.CopilotSet; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* @author dragove | ||
* create on 2024-01-01 | ||
*/ | ||
@Mapper(componentModel = "spring", imports = { | ||
LocalDateTime.class | ||
}) | ||
public interface CopilotSetConverter { | ||
|
||
@Mapping(target = "delete", ignore = true) | ||
@Mapping(target = "deleteTime", ignore = true) | ||
@Mapping(target = "copilotIds", expression = "java(createReq.getDistinctIdsAndCheck())") | ||
@Mapping(target = "createTime", expression = "java(LocalDateTime.now())") | ||
@Mapping(target = "updateTime", expression = "java(LocalDateTime.now())") | ||
CopilotSet convert(CopilotSetCreateReq createReq, long id, String creatorId); | ||
|
||
@Mapping(target = "creator", ignore = true) | ||
CopilotSetListRes convert(CopilotSet copilotSet, String creator); | ||
|
||
CopilotSetRes convertDetail(CopilotSet copilotSet, String creator); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/plus/maa/backend/controller/CopilotSetController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package plus.maa.backend.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import plus.maa.backend.config.SpringDocConfig; | ||
import plus.maa.backend.config.security.AuthenticationHelper; | ||
import plus.maa.backend.controller.request.CommonIdReq; | ||
import plus.maa.backend.controller.request.CopilotSetQuery; | ||
import plus.maa.backend.controller.request.CopilotSetUpdateReq; | ||
import plus.maa.backend.controller.request.copilotset.CopilotSetCreateReq; | ||
import plus.maa.backend.controller.request.copilotset.CopilotSetModCopilotsReq; | ||
import plus.maa.backend.controller.response.CopilotSetPageRes; | ||
import plus.maa.backend.controller.response.CopilotSetRes; | ||
import plus.maa.backend.controller.response.MaaResult; | ||
import plus.maa.backend.service.CopilotSetService; | ||
|
||
/** | ||
* @author dragove | ||
* create on 2024-01-01 | ||
*/ | ||
@Tag(name = "CopilotSet", description = "作业集相关接口") | ||
@RequestMapping("/set") | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class CopilotSetController { | ||
|
||
private final CopilotSetService service; | ||
private final AuthenticationHelper helper; | ||
|
||
@Operation(summary = "查询作业集列表") | ||
@ApiResponse(description = "作业集id") | ||
@PostMapping("/query") | ||
public MaaResult<CopilotSetPageRes> querySets( | ||
@Parameter(description = "作业集列表查询请求") @Valid @RequestBody CopilotSetQuery req) { | ||
return MaaResult.success(service.query(req)); | ||
} | ||
|
||
@Operation(summary = "查询作业集列表") | ||
@ApiResponse(description = "作业集id") | ||
@GetMapping("/get") | ||
public MaaResult<CopilotSetRes> getSet(@RequestParam @Parameter(description = "作业id") long id) { | ||
return MaaResult.success(service.get(id)); | ||
} | ||
|
||
|
||
@Operation(summary = "创建作业集") | ||
@ApiResponse(description = "作业集id") | ||
@SecurityRequirement(name = SpringDocConfig.SECURITY_SCHEME_NAME) | ||
@PostMapping("/create") | ||
public MaaResult<Long> createSet( | ||
@Parameter(description = "作业集新增请求") @Valid @RequestBody CopilotSetCreateReq req) { | ||
return MaaResult.success(service.create(req, helper.getUserId())); | ||
} | ||
|
||
@Operation(summary = "添加作业集作业列表") | ||
@SecurityRequirement(name = SpringDocConfig.SECURITY_SCHEME_NAME) | ||
@PostMapping("/add") | ||
public MaaResult<Void> addCopilotIds( | ||
@Parameter(description = "作业集中加入新作业请求") @Valid @RequestBody CopilotSetModCopilotsReq req) { | ||
service.addCopilotIds(req, helper.getUserId()); | ||
return MaaResult.success(); | ||
} | ||
|
||
@Operation(summary = "添加作业集作业列表") | ||
@SecurityRequirement(name = SpringDocConfig.SECURITY_SCHEME_NAME) | ||
@PostMapping("/remove") | ||
public MaaResult<Void> removeCopilotIds( | ||
@Parameter(description = "作业集中删除作业请求") @Valid @RequestBody CopilotSetModCopilotsReq req) { | ||
service.removeCopilotIds(req, helper.getUserId()); | ||
return MaaResult.success(); | ||
} | ||
|
||
@Operation(summary = "更新作业集信息") | ||
@SecurityRequirement(name = SpringDocConfig.SECURITY_SCHEME_NAME) | ||
@PostMapping("/update") | ||
public MaaResult<Void> updateCopilotSet( | ||
@Parameter(description = "更新作业集信息请求") @Valid @RequestBody CopilotSetUpdateReq req) { | ||
service.update(req, helper.getUserId()); | ||
return MaaResult.success(); | ||
} | ||
|
||
@Operation(summary = "删除作业集") | ||
@SecurityRequirement(name = SpringDocConfig.SECURITY_SCHEME_NAME) | ||
@PostMapping("/delete") | ||
public MaaResult<Void> deleteCopilotSet( | ||
@Parameter(description = "删除作业集信息请求") @Valid @RequestBody CommonIdReq<Long> req) { | ||
service.delete(req.getId(), helper.getUserId()); | ||
return MaaResult.success(); | ||
} | ||
|
||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/plus/maa/backend/controller/request/CommonIdReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package plus.maa.backend.controller.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* @author dragove | ||
* create on 2024-01-05 | ||
*/ | ||
@Getter | ||
@Setter | ||
public class CommonIdReq<T> { | ||
|
||
@NotNull(message = "id必填") | ||
private T id; | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/plus/maa/backend/controller/request/CopilotSetQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package plus.maa.backend.controller.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.Max; | ||
import jakarta.validation.constraints.Positive; | ||
import jakarta.validation.constraints.PositiveOrZero; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* @author dragove | ||
* create on 2024-01-06 | ||
*/ | ||
@Getter | ||
@Setter | ||
@Schema(title = "作业集列表查询接口参数") | ||
public class CopilotSetQuery { | ||
|
||
@Positive(message = "页码必须为大于0的数字") | ||
@Schema(title = "页码") | ||
private int page = 1; | ||
|
||
@Schema(title = "单页数据量") | ||
@PositiveOrZero(message = "单页数据量必须为大于等于0的数字") | ||
@Max(value = 50, message = "单页大小不得超过50") | ||
private int limit = 10; | ||
|
||
@Schema(title = "查询关键词") | ||
private String keyword; | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/plus/maa/backend/controller/request/CopilotSetUpdateReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package plus.maa.backend.controller.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import plus.maa.backend.service.model.CopilotSetStatus; | ||
|
||
/** | ||
* @author dragove | ||
* create on 2024-01-02 | ||
*/ | ||
@Getter | ||
@Setter | ||
@Schema(title = "作业集更新请求") | ||
public class CopilotSetUpdateReq { | ||
@NotNull(message = "作业集id不能为空") | ||
private long id; | ||
|
||
@Schema(title = "作业集名称") | ||
@NotBlank(message = "作业集名称不能为空") | ||
private String name; | ||
|
||
@Schema(title = "作业集额外描述") | ||
private String description; | ||
|
||
@NotNull(message = "作业集公开状态不能为null") | ||
@Schema(title = "作业集公开状态", enumAsRef = true) | ||
private CopilotSetStatus status; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.