feat: add isFavorite to project list api

This commit is contained in:
vran 2022-02-25 19:14:02 +08:00
parent decd76dd42
commit fdbb3242ea
5 changed files with 32 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package com.databasir.api;
import com.databasir.api.config.security.DatabasirUserDetails;
import com.databasir.api.validator.CronExpressionValidator;
import com.databasir.common.JsonData;
import com.databasir.core.domain.log.annotation.Operation;
@ -11,6 +12,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -71,7 +73,11 @@ public class ProjectController {
public JsonData<Page<ProjectSimpleResponse>> list(@PageableDefault(sort = "id", direction = Sort.Direction.DESC)
Pageable page,
ProjectListCondition condition) {
return JsonData.ok(projectService.list(page, condition));
DatabasirUserDetails user = (DatabasirUserDetails) SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
Integer userId = user.getUserPojo().getId();
return JsonData.ok(projectService.list(userId, page, condition));
}
@PostMapping(Routes.GroupProject.TEST_CONNECTION)

View File

@ -33,5 +33,6 @@ public interface ProjectResponseConverter {
@Mapping(target = "createAt", source = "project.createAt")
ProjectSimpleResponse toSimple(ProjectPojo project,
DataSourcePojo dataSource,
ProjectSyncRulePojo syncRule);
ProjectSyncRulePojo syncRule,
Boolean isFavorite);
}

View File

@ -19,6 +19,8 @@ public class ProjectSimpleResponse {
private Boolean isAutoSync;
private Boolean isFavorite;
private String autoSyncCron;
private LocalDateTime createAt;

View File

@ -17,10 +17,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -38,6 +35,8 @@ public class ProjectService {
private final DataSourcePropertyDao dataSourcePropertyDao;
private final UserFavoriteProjectDao userFavoriteProjectDao;
private final DataSourcePojoConverter dataSourcePojoConverter;
private final ProjectPojoConverter projectPojoConverter;
@ -128,7 +127,7 @@ public class ProjectService {
projectSyncRuleDao.disableAutoSyncByProjectId(projectId);
}
public Page<ProjectSimpleResponse> list(Pageable page, ProjectListCondition condition) {
public Page<ProjectSimpleResponse> list(Integer userId, Pageable page, ProjectListCondition condition) {
Page<ProjectPojo> pageData = projectDao.selectByCondition(page, condition.toCondition());
List<Integer> projectIds = pageData.getContent()
.stream()
@ -140,10 +139,15 @@ public class ProjectService {
Map<Integer, ProjectSyncRulePojo> syncRuleMapByProjectId = projectSyncRuleDao.selectInProjectIds(projectIds)
.stream()
.collect(Collectors.toMap(ProjectSyncRulePojo::getProjectId, Function.identity()));
Set<Integer> favoriteProjectIds = userFavoriteProjectDao.selectByUserIdAndProjectIds(userId, projectIds)
.stream()
.map(UserFavoriteProjectPojo::getProjectId)
.collect(Collectors.toSet());
return pageData.map(project -> {
DataSourcePojo dataSource = dataSourceMapByProjectId.get(project.getId());
ProjectSyncRulePojo syncRule = syncRuleMapByProjectId.get(project.getId());
return projectResponseConverter.toSimple(project, dataSource, syncRule);
Boolean isFavorite = favoriteProjectIds.contains(project.getId());
return projectResponseConverter.toSimple(project, dataSource, syncRule, isFavorite);
});
}

View File

@ -10,6 +10,7 @@ import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import java.util.Collections;
import java.util.List;
import static com.databasir.dao.Tables.*;
@ -59,4 +60,14 @@ public class UserFavoriteProjectDao extends BaseDao<UserFavoriteProjectPojo> {
.fetchInto(UserFavoriteProjectPojo.class);
return new PageImpl<>(data, request, total);
}
public List<UserFavoriteProjectPojo> selectByUserIdAndProjectIds(Integer userId, List<Integer> projectIds) {
if (projectIds == null || projectIds.isEmpty()) {
return Collections.emptyList();
}
return this.getDslContext()
.select(USER_FAVORITE_PROJECT.fields()).from(USER_FAVORITE_PROJECT)
.where(USER_FAVORITE_PROJECT.USER_ID.eq(userId).and(USER_FAVORITE_PROJECT.PROJECT_ID.in(projectIds)))
.fetchInto(UserFavoriteProjectPojo.class);
}
}