feat: add global search api
This commit is contained in:
parent
59570a9ee6
commit
e416006793
|
@ -182,4 +182,8 @@ public interface Routes {
|
||||||
String GET_SQL_MOCK_DATA = BASE + "/groups/{groupId}/projects/{projectId}/mock_data/sql";
|
String GET_SQL_MOCK_DATA = BASE + "/groups/{groupId}/projects/{projectId}/mock_data/sql";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Search {
|
||||||
|
String SEARCH = BASE + "/search";
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.databasir.api;
|
||||||
|
|
||||||
|
import com.databasir.common.JsonData;
|
||||||
|
import com.databasir.core.domain.search.SearchService;
|
||||||
|
import com.databasir.core.domain.search.data.SearchResponse;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Validated
|
||||||
|
@Tag(name = "SearchController", description = "全局搜索 API")
|
||||||
|
public class SearchController {
|
||||||
|
|
||||||
|
private final SearchService searchService;
|
||||||
|
|
||||||
|
@GetMapping(Routes.Search.SEARCH)
|
||||||
|
@Operation(summary = "搜索")
|
||||||
|
public JsonData<SearchResponse> search(@RequestParam(name = "query") String keyword) {
|
||||||
|
return JsonData.ok(searchService.search(keyword));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.databasir.core.domain.search;
|
||||||
|
|
||||||
|
import com.databasir.core.domain.search.converter.SearchResponseConverter;
|
||||||
|
import com.databasir.core.domain.search.data.SearchResponse;
|
||||||
|
import com.databasir.dao.impl.GroupDao;
|
||||||
|
import com.databasir.dao.impl.ProjectDao;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SearchService {
|
||||||
|
|
||||||
|
private final ProjectDao projectDao;
|
||||||
|
|
||||||
|
private final GroupDao groupDao;
|
||||||
|
|
||||||
|
private final SearchResponseConverter searchResponseConverter;
|
||||||
|
|
||||||
|
public SearchResponse search(String query) {
|
||||||
|
var groupPojoList = groupDao.selectByName(query);
|
||||||
|
var groupResults = searchResponseConverter.toGroupResults(groupPojoList);
|
||||||
|
var projectList = projectDao.selectByProjectNameOrDatabaseOrSchemaOrGroup(query);
|
||||||
|
var projectResults = searchResponseConverter.toProjectResults(projectList);
|
||||||
|
|
||||||
|
// build response
|
||||||
|
SearchResponse response = new SearchResponse();
|
||||||
|
response.setGroups(groupResults);
|
||||||
|
response.setProjects(projectResults);
|
||||||
|
// TODO support Table search
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.databasir.core.domain.search.converter;
|
||||||
|
|
||||||
|
import com.databasir.core.domain.search.data.SearchResponse;
|
||||||
|
import com.databasir.dao.tables.pojos.GroupPojo;
|
||||||
|
import com.databasir.dao.value.ProjectQueryPojo;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper(componentModel = "spring")
|
||||||
|
public interface SearchResponseConverter {
|
||||||
|
|
||||||
|
List<SearchResponse.GroupSearchResult> toGroupResults(List<GroupPojo> groups);
|
||||||
|
|
||||||
|
List<SearchResponse.ProjectSearchResult> toProjectResults(List<ProjectQueryPojo> projects);
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.databasir.core.domain.search.data;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class SearchResponse {
|
||||||
|
|
||||||
|
@Builder.Default
|
||||||
|
private List<GroupSearchResult> groups = Collections.emptyList();
|
||||||
|
|
||||||
|
@Builder.Default
|
||||||
|
private List<ProjectSearchResult> projects = Collections.emptyList();
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class GroupSearchResult {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private String description;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class ProjectSearchResult {
|
||||||
|
|
||||||
|
private Integer projectId;
|
||||||
|
|
||||||
|
private Integer groupId;
|
||||||
|
|
||||||
|
private String groupName;
|
||||||
|
|
||||||
|
private String projectName;
|
||||||
|
|
||||||
|
private String projectDescription;
|
||||||
|
|
||||||
|
private String databaseName;
|
||||||
|
|
||||||
|
private String schemaName;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -69,4 +69,12 @@ public class GroupDao extends BaseDao<GroupPojo> {
|
||||||
.where(GROUP.ID.in(ids))
|
.where(GROUP.ID.in(ids))
|
||||||
.fetchInto(GroupPojo.class);
|
.fetchInto(GroupPojo.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<GroupPojo> selectByName(String nameContains) {
|
||||||
|
return getDslContext()
|
||||||
|
.select(GROUP.fields()).from(GROUP)
|
||||||
|
.where(GROUP.NAME.contains(nameContains))
|
||||||
|
.and(GROUP.DELETED.eq(false))
|
||||||
|
.fetchInto(GroupPojo.class);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@ package com.databasir.dao.impl;
|
||||||
|
|
||||||
import com.databasir.dao.tables.pojos.ProjectPojo;
|
import com.databasir.dao.tables.pojos.ProjectPojo;
|
||||||
import com.databasir.dao.value.GroupProjectCountPojo;
|
import com.databasir.dao.value.GroupProjectCountPojo;
|
||||||
|
import com.databasir.dao.value.ProjectQueryPojo;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.jooq.Condition;
|
import org.jooq.Condition;
|
||||||
import org.jooq.DSLContext;
|
import org.jooq.DSLContext;
|
||||||
|
@ -19,8 +20,7 @@ import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static com.databasir.dao.Tables.DATA_SOURCE;
|
import static com.databasir.dao.Tables.*;
|
||||||
import static com.databasir.dao.Tables.PROJECT;
|
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class ProjectDao extends BaseDao<ProjectPojo> {
|
public class ProjectDao extends BaseDao<ProjectPojo> {
|
||||||
|
@ -126,4 +126,28 @@ public class ProjectDao extends BaseDao<ProjectPojo> {
|
||||||
.where(PROJECT.ID.in(projectIds))
|
.where(PROJECT.ID.in(projectIds))
|
||||||
.fetchMap(PROJECT.ID, PROJECT.GROUP_ID);
|
.fetchMap(PROJECT.ID, PROJECT.GROUP_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<ProjectQueryPojo> selectByProjectNameOrDatabaseOrSchemaOrGroup(String query) {
|
||||||
|
return getDslContext()
|
||||||
|
.select(
|
||||||
|
PROJECT.ID.as("project_id"),
|
||||||
|
PROJECT.NAME.as("project_name"),
|
||||||
|
PROJECT.DESCRIPTION.as("project_description"),
|
||||||
|
DATA_SOURCE.DATABASE_NAME,
|
||||||
|
DATA_SOURCE.SCHEMA_NAME,
|
||||||
|
GROUP.NAME.as("group_name"),
|
||||||
|
GROUP.ID.as("group_id")
|
||||||
|
)
|
||||||
|
.from(PROJECT)
|
||||||
|
.leftJoin(DATA_SOURCE).on(DATA_SOURCE.PROJECT_ID.eq(PROJECT.ID))
|
||||||
|
.leftJoin(GROUP).on(GROUP.ID.eq(PROJECT.GROUP_ID))
|
||||||
|
.where(PROJECT.DELETED.eq(false)
|
||||||
|
.and(GROUP.DELETED.eq(false))
|
||||||
|
.and(PROJECT.NAME.contains(query)
|
||||||
|
.or(DATA_SOURCE.DATABASE_NAME.contains(query))
|
||||||
|
.or(DATA_SOURCE.SCHEMA_NAME.contains(query))
|
||||||
|
.or(GROUP.NAME.contains(query)))
|
||||||
|
)
|
||||||
|
.fetchInto(ProjectQueryPojo.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.databasir.dao.value;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ProjectQueryPojo {
|
||||||
|
|
||||||
|
private Integer projectId;
|
||||||
|
|
||||||
|
private Integer groupId;
|
||||||
|
|
||||||
|
private String groupName;
|
||||||
|
|
||||||
|
private String projectName;
|
||||||
|
|
||||||
|
private String projectDescription;
|
||||||
|
|
||||||
|
private String databaseName;
|
||||||
|
|
||||||
|
private String schemaName;
|
||||||
|
}
|
Loading…
Reference in New Issue