Skip to content

Commit

Permalink
Merge pull request #157 from dragove/feature/copilot-set
Browse files Browse the repository at this point in the history
feat: 作业集(收藏)功能
  • Loading branch information
dragove authored Jan 14, 2024
2 parents 022e48b + 9583a17 commit 3da616c
Show file tree
Hide file tree
Showing 27 changed files with 816 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-cache'
//springdoc相关依赖没有被自动管理,必须保留版本号
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'com.github.therapi:therapi-runtime-javadoc:0.13.0'

implementation 'com.squareup.okhttp3:okhttp:4.10.0'
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/plus/maa/backend/common/model/CopilotSetType.java
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 src/main/java/plus/maa/backend/common/utils/IdComponent.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public interface CopilotConverter {
@Mapping(target = "uploadTime", source = "now")
@Mapping(target = "firstUploadTime", source = "now")
@Mapping(target = "uploaderId", source = "userId")
Copilot toCopilot(CopilotDTO copilotDto, String userId, LocalDateTime now, Long copilotId, String content);
Copilot toCopilot(CopilotDTO copilotDto, Long copilotId, String userId, LocalDateTime now, String content);

@Mapping(target = "ratingType", ignore = true)
@Mapping(target = "ratingRatio", ignore = true)
Expand Down
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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class SecurityConfig {
"/swagger-ui/**",
"/arknights/level",
"/copilot/query",
"/set/query",
"/set/get",
"/copilot/get/**",
"/copilot/rating",
"/comments/query",
Expand Down
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 src/main/java/plus/maa/backend/controller/request/CommonIdReq.java
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;

}
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;

}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* @author LoMu
* Date 2022-12-26 2:48
Expand All @@ -25,6 +27,7 @@ public class CopilotQueriesRequest {
private boolean desc = true;
private String orderBy;
private String language;
private List<Long> copilotIds;

/*
* 这里为了正确接收前端的下划线风格,手动写了三个 setter 用于起别名
Expand All @@ -33,17 +36,27 @@ public class CopilotQueriesRequest {
* (吐槽一下,同样是Get请求,怎么CommentsQueries是驼峰命名,到了CopilotQueries就成了下划线命名)
*/
@JsonIgnore
@SuppressWarnings("unused")
public void setLevel_keyword(String levelKeyword) {
this.levelKeyword = levelKeyword;
}

@JsonIgnore
@SuppressWarnings("unused")
public void setUploader_id(String uploaderId) {
this.uploaderId = uploaderId;
}

@JsonIgnore
@SuppressWarnings("unused")
public void setOrder_by(String orderBy) {
this.orderBy = orderBy;
}

@JsonIgnore
@SuppressWarnings("unused")
public void setCopilot_ids(List<Long> copilotIds) {
this.copilotIds = copilotIds;
}

}
Loading

0 comments on commit 3da616c

Please sign in to comment.