Skip to content

Commit

Permalink
feat: 作业集列表查询接口
Browse files Browse the repository at this point in the history
  • Loading branch information
dragove committed Jan 6, 2024
1 parent b825a02 commit ed4582a
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import plus.maa.backend.controller.request.copilotset.CopilotSetCreateReq;
import plus.maa.backend.controller.response.user.CopilotSetListRes;
import plus.maa.backend.repository.entity.CopilotSet;

import java.time.LocalDateTime;
Expand All @@ -16,9 +17,14 @@
})
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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class SecurityConfig {
"/swagger-ui/**",
"/arknights/level",
"/copilot/query",
"/set/query",
"/copilot/get/**",
"/copilot/rating",
"/comments/query",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
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.CopilotSetModCopilotsReq;
import plus.maa.backend.controller.request.copilotset.CopilotSetCreateReq;
import plus.maa.backend.controller.response.CopilotSetPageRes;
import plus.maa.backend.controller.response.MaaResult;
import plus.maa.backend.service.CopilotSetService;

Expand All @@ -33,6 +35,14 @@ 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")
@SecurityRequirement(name = SpringDocConfig.SECURITY_SCHEME_NAME)
Expand Down
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,30 @@
package plus.maa.backend.controller.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import plus.maa.backend.controller.response.user.CopilotSetListRes;

import java.util.List;

/**
* @author dragove
* create on 2024-01-06
*/
@Getter
@Setter
@Schema(title = "作业集分页返回数据")
@Accessors(chain = true)
public class CopilotSetPageRes {

@Schema(title = "是否有下一页")
private boolean hasNext;
@Schema(title = "当前页码")
private int page;
@Schema(title = "总数据量")
private long total;
@Schema(title = "作业集列表")
private List<CopilotSetListRes> data;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package plus.maa.backend.controller.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import plus.maa.backend.service.model.CopilotSetStatus;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author dragove
* create on 2024-01-06
*/
@Getter
@Setter
@Schema(title = "作业集响应")
public class CopilotSetRes {

@Schema(title = "作业集id")
private long id;

@Schema(title = "作业集名称")
private String name;

@Schema(title = "额外描述")
private String description;

@Schema(title = "作业id列表")
private List<Long> copilotIds;

@Schema(title = "上传者id")
private String creatorId;

@Schema(title = "上传者昵称")
private String creator;

@Schema(title = "创建时间")
private LocalDateTime createTime;

@Schema(title = "更新时间")
private LocalDateTime updateTime;

@Schema(title = "作业状态", enumAsRef = true)
private CopilotSetStatus status;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package plus.maa.backend.controller.response.user;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

/**
* @author dragove
* create on 2024-01-06
*/
@Getter
@Setter
@Schema(title = "作业集响应(列表)")
public class CopilotSetListRes {

@Schema(title = "作业集id")
private long id;

@Schema(title = "作业集名称")
private String name;

@Schema(title = "额外描述")
private String description;

@Schema(title = "上传者id")
private String creatorId;

@Schema(title = "上传者昵称")
private String creator;

@Schema(title = "创建时间")
private LocalDateTime createTime;

@Schema(title = "更新时间")
private LocalDateTime updateTime;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package plus.maa.backend.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import plus.maa.backend.repository.entity.CopilotSet;

/**
Expand All @@ -9,5 +12,14 @@
*/
public interface CopilotSetRepository extends MongoRepository<CopilotSet, Long> {

@Query("""
{
"$or": [
{"name": {'$regex': ?0 ,'$options':'i'}},
{"description": {'$regex': ?0,'$options':'i' }}
]
}
""")
Page<CopilotSet> findByKeyword(String keyword, Pageable pageable);

}
3 changes: 3 additions & 0 deletions src/main/java/plus/maa/backend/repository/entity/MaaUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import plus.maa.backend.controller.request.user.UserInfoUpdateDTO;
Expand All @@ -25,6 +26,8 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
@Document("maa_user")
public class MaaUser implements Serializable {
@Transient
public static final MaaUser UNKNOWN = new MaaUser().setUserName("未知用户:(");
@Id
private String userId;
private String userName;
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/plus/maa/backend/service/CommentsAreaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public class CommentsAreaService {

private final MaaCopilotProperties maaCopilotProperties;

private final MaaUser UNKNOWN_USER = new MaaUser().setUserName("未知用户:(");

private final CommentConverter commentConverter;


Expand Down Expand Up @@ -121,8 +119,8 @@ public void addComments(String userId, CommentsAddDTO commentsAddDTO) {
CommentNotification commentNotification = new CommentNotification();


String authorName = maaUserMap.getOrDefault(replyUserId, UNKNOWN_USER).getUserName();
String reName = maaUserMap.getOrDefault(userId, UNKNOWN_USER).getUserName();
String authorName = maaUserMap.getOrDefault(replyUserId, MaaUser.UNKNOWN).getUserName();
String reName = maaUserMap.getOrDefault(userId, MaaUser.UNKNOWN).getUserName();

String title = isCopilotAuthor ? copilot.getDoc().getTitle() : commentsArea.getMessage();

Expand Down Expand Up @@ -332,7 +330,7 @@ public CommentsAreaInfo queriesCommentsArea(CommentsQueriesDTO request) {
, (int) mainComment.getLikeCount()
, maaUserMap.getOrDefault(
mainComment.getUploaderId()
, UNKNOWN_USER
, MaaUser.UNKNOWN
)
);

Expand All @@ -348,7 +346,7 @@ public CommentsAreaInfo queriesCommentsArea(CommentsQueriesDTO request) {
//填充评论用户名
, maaUserMap.getOrDefault(
subComment.getUploaderId(),
UNKNOWN_USER
MaaUser.UNKNOWN
)
, subComment.isDelete()
)
Expand Down
46 changes: 42 additions & 4 deletions src/main/java/plus/maa/backend/service/CopilotSetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import cn.hutool.core.lang.Assert;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import plus.maa.backend.common.utils.IdComponent;
import plus.maa.backend.common.utils.converter.CopilotSetConverter;
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.repository.CopilotSetRepository;
import plus.maa.backend.repository.UserRepository;
import plus.maa.backend.repository.entity.CopilotSet;
import plus.maa.backend.repository.entity.MaaUser;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

/**
* @author dragove
Expand All @@ -28,10 +35,13 @@ public class CopilotSetService {
private final IdComponent idComponent;
private final CopilotSetConverter converter;
private final CopilotSetRepository repository;
private final UserRepository userRepository;
private final Sort DEFAULT_SORT = Sort.by("id").descending();

/**
* 创建作业集
* @param req 作业集创建请求
*
* @param req 作业集创建请求
* @param userId 创建者用户id
* @return 作业集id
*/
Expand Down Expand Up @@ -82,7 +92,8 @@ public void update(CopilotSetUpdateReq req, String userId) {

/**
* 删除作业集信息(逻辑删除,保留详情接口查询结果)
* @param id 作业集id
*
* @param id 作业集id
* @param userId 登陆用户id
*/
public void delete(long id, String userId) {
Expand All @@ -94,4 +105,31 @@ public void delete(long id, String userId) {
copilotSet.setDeleteTime(LocalDateTime.now());
repository.save(copilotSet);
}

public CopilotSetPageRes query(CopilotSetQuery req) {
PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getLimit(), DEFAULT_SORT);

String keyword = req.getKeyword();
Page<CopilotSet> copilotSets;
if (StringUtils.isBlank(keyword)) {
copilotSets = repository.findAll(pageRequest);
} else {
copilotSets = repository.findByKeyword(keyword, pageRequest);
}

List<String> userIds = copilotSets.stream().map(CopilotSet::getCreatorId)
.filter(Objects::nonNull)
.distinct()
.toList();
Map<String, MaaUser> userById = userRepository.findByUsersId(userIds);
return new CopilotSetPageRes()
.setPage(copilotSets.getNumber() + 1)
.setTotal(copilotSets.getTotalElements())
.setHasNext(copilotSets.getTotalPages() > req.getPage())
.setData(copilotSets.stream().map(cs -> {
MaaUser user = userById.getOrDefault(cs.getCreatorId(), MaaUser.UNKNOWN);
return converter.convert(cs, user.getUserName());
}).toList());

}
}

0 comments on commit ed4582a

Please sign in to comment.