feat: add isFavorite to project list api
This commit is contained in:
parent
decd76dd42
commit
fdbb3242ea
|
@ -1,5 +1,6 @@
|
||||||
package com.databasir.api;
|
package com.databasir.api;
|
||||||
|
|
||||||
|
import com.databasir.api.config.security.DatabasirUserDetails;
|
||||||
import com.databasir.api.validator.CronExpressionValidator;
|
import com.databasir.api.validator.CronExpressionValidator;
|
||||||
import com.databasir.common.JsonData;
|
import com.databasir.common.JsonData;
|
||||||
import com.databasir.core.domain.log.annotation.Operation;
|
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.domain.Sort;
|
||||||
import org.springframework.data.web.PageableDefault;
|
import org.springframework.data.web.PageableDefault;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
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)
|
public JsonData<Page<ProjectSimpleResponse>> list(@PageableDefault(sort = "id", direction = Sort.Direction.DESC)
|
||||||
Pageable page,
|
Pageable page,
|
||||||
ProjectListCondition condition) {
|
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)
|
@PostMapping(Routes.GroupProject.TEST_CONNECTION)
|
||||||
|
|
|
@ -33,5 +33,6 @@ public interface ProjectResponseConverter {
|
||||||
@Mapping(target = "createAt", source = "project.createAt")
|
@Mapping(target = "createAt", source = "project.createAt")
|
||||||
ProjectSimpleResponse toSimple(ProjectPojo project,
|
ProjectSimpleResponse toSimple(ProjectPojo project,
|
||||||
DataSourcePojo dataSource,
|
DataSourcePojo dataSource,
|
||||||
ProjectSyncRulePojo syncRule);
|
ProjectSyncRulePojo syncRule,
|
||||||
|
Boolean isFavorite);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,8 @@ public class ProjectSimpleResponse {
|
||||||
|
|
||||||
private Boolean isAutoSync;
|
private Boolean isAutoSync;
|
||||||
|
|
||||||
|
private Boolean isFavorite;
|
||||||
|
|
||||||
private String autoSyncCron;
|
private String autoSyncCron;
|
||||||
|
|
||||||
private LocalDateTime createAt;
|
private LocalDateTime createAt;
|
||||||
|
|
|
@ -17,10 +17,7 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -38,6 +35,8 @@ public class ProjectService {
|
||||||
|
|
||||||
private final DataSourcePropertyDao dataSourcePropertyDao;
|
private final DataSourcePropertyDao dataSourcePropertyDao;
|
||||||
|
|
||||||
|
private final UserFavoriteProjectDao userFavoriteProjectDao;
|
||||||
|
|
||||||
private final DataSourcePojoConverter dataSourcePojoConverter;
|
private final DataSourcePojoConverter dataSourcePojoConverter;
|
||||||
|
|
||||||
private final ProjectPojoConverter projectPojoConverter;
|
private final ProjectPojoConverter projectPojoConverter;
|
||||||
|
@ -128,7 +127,7 @@ public class ProjectService {
|
||||||
projectSyncRuleDao.disableAutoSyncByProjectId(projectId);
|
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());
|
Page<ProjectPojo> pageData = projectDao.selectByCondition(page, condition.toCondition());
|
||||||
List<Integer> projectIds = pageData.getContent()
|
List<Integer> projectIds = pageData.getContent()
|
||||||
.stream()
|
.stream()
|
||||||
|
@ -140,10 +139,15 @@ public class ProjectService {
|
||||||
Map<Integer, ProjectSyncRulePojo> syncRuleMapByProjectId = projectSyncRuleDao.selectInProjectIds(projectIds)
|
Map<Integer, ProjectSyncRulePojo> syncRuleMapByProjectId = projectSyncRuleDao.selectInProjectIds(projectIds)
|
||||||
.stream()
|
.stream()
|
||||||
.collect(Collectors.toMap(ProjectSyncRulePojo::getProjectId, Function.identity()));
|
.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 -> {
|
return pageData.map(project -> {
|
||||||
DataSourcePojo dataSource = dataSourceMapByProjectId.get(project.getId());
|
DataSourcePojo dataSource = dataSourceMapByProjectId.get(project.getId());
|
||||||
ProjectSyncRulePojo syncRule = syncRuleMapByProjectId.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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.springframework.data.domain.PageImpl;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.databasir.dao.Tables.*;
|
import static com.databasir.dao.Tables.*;
|
||||||
|
@ -59,4 +60,14 @@ public class UserFavoriteProjectDao extends BaseDao<UserFavoriteProjectPojo> {
|
||||||
.fetchInto(UserFavoriteProjectPojo.class);
|
.fetchInto(UserFavoriteProjectPojo.class);
|
||||||
return new PageImpl<>(data, request, total);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue