geometries: add /geometries/entity search by entity name
This commit is contained in:
@@ -80,6 +80,37 @@ WHERE g.is_deleted = false
|
|||||||
)
|
)
|
||||||
ORDER BY g.id DESC;
|
ORDER BY g.id DESC;
|
||||||
|
|
||||||
|
-- name: SearchGeometriesByEntityName :many
|
||||||
|
WITH matched_entities AS (
|
||||||
|
SELECT
|
||||||
|
e.id,
|
||||||
|
e.name,
|
||||||
|
e.description
|
||||||
|
FROM entities e
|
||||||
|
WHERE e.is_deleted = false
|
||||||
|
AND (sqlc.narg('name')::text IS NULL OR e.name ILIKE '%' || sqlc.narg('name')::text || '%')
|
||||||
|
AND (sqlc.narg('cursor_id')::uuid IS NULL OR e.id < sqlc.narg('cursor_id')::uuid)
|
||||||
|
ORDER BY e.id DESC
|
||||||
|
LIMIT sqlc.arg('limit_count')
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
me.id AS entity_id,
|
||||||
|
me.name AS entity_name,
|
||||||
|
me.description AS entity_description,
|
||||||
|
g.id AS geometry_id,
|
||||||
|
g.geo_type,
|
||||||
|
g.draw_geometry,
|
||||||
|
g.binding,
|
||||||
|
g.time_start,
|
||||||
|
g.time_end
|
||||||
|
FROM matched_entities me
|
||||||
|
LEFT JOIN entity_geometries eg
|
||||||
|
ON eg.entity_id = me.id
|
||||||
|
LEFT JOIN geometries g
|
||||||
|
ON g.id = eg.geometry_id
|
||||||
|
AND g.is_deleted = false
|
||||||
|
ORDER BY me.id DESC, g.id DESC;
|
||||||
|
|
||||||
-- name: BulkDeleteEntityGeometriesByEntityId :many
|
-- name: BulkDeleteEntityGeometriesByEntityId :many
|
||||||
DELETE FROM entity_geometries
|
DELETE FROM entity_geometries
|
||||||
WHERE entity_id = $1
|
WHERE entity_id = $1
|
||||||
|
|||||||
@@ -81,3 +81,40 @@ func (h *GeometryController) SearchGeometries(c fiber.Ctx) error {
|
|||||||
Data: res,
|
Data: res,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SearchGeometriesByEntityName handles searching entities by name and returning geometries linked to those entities.
|
||||||
|
// Cursor pagination is based on entity ID (uuid).
|
||||||
|
// @Summary Search geometries by entity name
|
||||||
|
// @Description Search entities by name (cursor pagination) and return their linked geometries
|
||||||
|
// @Tags Geometries
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Param query query request.SearchGeometriesByEntityNameDto false "Search Query"
|
||||||
|
// @Success 200 {object} response.CommonResponse
|
||||||
|
// @Failure 500 {object} response.CommonResponse
|
||||||
|
// @Router /geometries/entity [get]
|
||||||
|
func (h *GeometryController) SearchGeometriesByEntityName(c fiber.Ctx) error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
dto := &request.SearchGeometriesByEntityNameDto{}
|
||||||
|
if err := validator.ValidateQueryDto(c, dto); err != nil {
|
||||||
|
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||||
|
Status: false,
|
||||||
|
Errors: err,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := h.service.SearchGeometriesByEntityName(ctx, dto)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(err.Code).JSON(response.CommonResponse{
|
||||||
|
Status: false,
|
||||||
|
Message: err.Message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
|
||||||
|
Status: true,
|
||||||
|
Data: res,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,3 +9,11 @@ type SearchGeometryDto struct {
|
|||||||
EntityID *string `json:"entity_id" query:"entity_id" validate:"omitempty,uuid"`
|
EntityID *string `json:"entity_id" query:"entity_id" validate:"omitempty,uuid"`
|
||||||
ProjectID *string `json:"project_id" query:"project_id" validate:"omitempty,uuid"`
|
ProjectID *string `json:"project_id" query:"project_id" validate:"omitempty,uuid"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SearchGeometriesByEntityNameDto struct {
|
||||||
|
// Entity name keyword for searching. FE will render the result list by entity name.
|
||||||
|
Name string `json:"name" query:"name" validate:"required,max=255"`
|
||||||
|
// Cursor is entity UUID (id < cursor).
|
||||||
|
Cursor string `json:"cursor" query:"cursor" validate:"omitempty,uuid"`
|
||||||
|
Limit int `json:"limit" query:"limit" validate:"omitempty,min=1,max=100"`
|
||||||
|
}
|
||||||
|
|||||||
26
internal/dtos/response/geometryEntitySearch.go
Normal file
26
internal/dtos/response/geometryEntitySearch.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package response
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
// SearchGeometriesByEntityNameResponse groups geometries by matched entities.
|
||||||
|
// Cursor pagination is based on entity_id (descending).
|
||||||
|
type SearchGeometriesByEntityNameResponse struct {
|
||||||
|
Items []*EntityGeometriesSearchItem `json:"items"`
|
||||||
|
NextCursor string `json:"next_cursor,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type EntityGeometriesSearchItem struct {
|
||||||
|
EntityID string `json:"entity_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Geometries []*EntityGeometrySearchGeo `json:"geometries"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type EntityGeometrySearchGeo struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
GeoType int16 `json:"geo_type"`
|
||||||
|
DrawGeometry json.RawMessage `json:"draw_geometry"`
|
||||||
|
Binding json.RawMessage `json:"binding,omitempty"`
|
||||||
|
TimeStart *int32 `json:"time_start,omitempty"`
|
||||||
|
TimeEnd *int32 `json:"time_end,omitempty"`
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ type GeometryRepository interface {
|
|||||||
GetByID(ctx context.Context, id pgtype.UUID) (*models.GeometryEntity, error)
|
GetByID(ctx context.Context, id pgtype.UUID) (*models.GeometryEntity, error)
|
||||||
GetByIDs(ctx context.Context, ids []string) ([]*models.GeometryEntity, error)
|
GetByIDs(ctx context.Context, ids []string) ([]*models.GeometryEntity, error)
|
||||||
Search(ctx context.Context, params sqlc.SearchGeometriesParams) ([]*models.GeometryEntity, error)
|
Search(ctx context.Context, params sqlc.SearchGeometriesParams) ([]*models.GeometryEntity, error)
|
||||||
|
SearchByEntityName(ctx context.Context, name string, cursorID *pgtype.UUID, limit int32) ([]EntityGeometriesSearchRow, error)
|
||||||
Create(ctx context.Context, params sqlc.CreateGeometryParams) (*models.GeometryEntity, error)
|
Create(ctx context.Context, params sqlc.CreateGeometryParams) (*models.GeometryEntity, error)
|
||||||
Update(ctx context.Context, params sqlc.UpdateGeometryParams) (*models.GeometryEntity, error)
|
Update(ctx context.Context, params sqlc.UpdateGeometryParams) (*models.GeometryEntity, error)
|
||||||
Delete(ctx context.Context, id pgtype.UUID) error
|
Delete(ctx context.Context, id pgtype.UUID) error
|
||||||
@@ -37,12 +38,14 @@ type GeometryRepository interface {
|
|||||||
type geometryRepository struct {
|
type geometryRepository struct {
|
||||||
q *sqlc.Queries
|
q *sqlc.Queries
|
||||||
c cache.Cache
|
c cache.Cache
|
||||||
|
db sqlc.DBTX
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGeometryRepository(db sqlc.DBTX, c cache.Cache) GeometryRepository {
|
func NewGeometryRepository(db sqlc.DBTX, c cache.Cache) GeometryRepository {
|
||||||
return &geometryRepository{
|
return &geometryRepository{
|
||||||
q: sqlc.New(db),
|
q: sqlc.New(db),
|
||||||
c: c,
|
c: c,
|
||||||
|
db: db,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,9 +53,24 @@ func (r *geometryRepository) WithTx(tx pgx.Tx) GeometryRepository {
|
|||||||
return &geometryRepository{
|
return &geometryRepository{
|
||||||
q: r.q.WithTx(tx),
|
q: r.q.WithTx(tx),
|
||||||
c: r.c,
|
c: r.c,
|
||||||
|
db: tx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EntityGeometriesSearchRow is a "flattened" row shape for searching geometries by entity name.
|
||||||
|
// FE will display entity list by entity_name, while still having geometries in the same response.
|
||||||
|
type EntityGeometriesSearchRow struct {
|
||||||
|
EntityID string `json:"entity_id"`
|
||||||
|
EntityName string `json:"name"`
|
||||||
|
EntityDescription string `json:"description"`
|
||||||
|
GeometryID string `json:"id"`
|
||||||
|
GeoType int16 `json:"geo_type"`
|
||||||
|
DrawGeometry json.RawMessage `json:"draw_geometry"`
|
||||||
|
Binding json.RawMessage `json:"binding,omitempty"`
|
||||||
|
TimeStart *int32 `json:"time_start,omitempty"`
|
||||||
|
TimeEnd *int32 `json:"time_end,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
func (r *geometryRepository) generateQueryKey(prefix string, params any) string {
|
func (r *geometryRepository) generateQueryKey(prefix string, params any) string {
|
||||||
b, _ := json.Marshal(params)
|
b, _ := json.Marshal(params)
|
||||||
hash := fmt.Sprintf("%x", md5.Sum(b))
|
hash := fmt.Sprintf("%x", md5.Sum(b))
|
||||||
@@ -377,3 +395,122 @@ func (r *geometryRepository) DeleteEntityGeometry(ctx context.Context, entityID
|
|||||||
GeometryID: geometryID,
|
GeometryID: geometryID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *geometryRepository) SearchByEntityName(
|
||||||
|
ctx context.Context,
|
||||||
|
name string,
|
||||||
|
cursorID *pgtype.UUID,
|
||||||
|
limit int32,
|
||||||
|
) ([]EntityGeometriesSearchRow, error) {
|
||||||
|
// Cursor is entity_id (not geometry_id) so FE can render entities as the primary list.
|
||||||
|
const q = `
|
||||||
|
WITH matched_entities AS (
|
||||||
|
SELECT
|
||||||
|
e.id,
|
||||||
|
e.name,
|
||||||
|
e.description
|
||||||
|
FROM entities e
|
||||||
|
WHERE e.is_deleted = false
|
||||||
|
AND ($1::text = '' OR e.name ILIKE '%' || $1::text || '%')
|
||||||
|
AND ($2::uuid IS NULL OR e.id < $2::uuid)
|
||||||
|
ORDER BY e.id DESC
|
||||||
|
LIMIT $3
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
me.id AS entity_id,
|
||||||
|
me.name AS entity_name,
|
||||||
|
COALESCE(me.description, '') AS entity_description,
|
||||||
|
g.id AS geometry_id,
|
||||||
|
g.geo_type,
|
||||||
|
g.draw_geometry,
|
||||||
|
g.binding,
|
||||||
|
g.time_start,
|
||||||
|
g.time_end
|
||||||
|
FROM matched_entities me
|
||||||
|
LEFT JOIN entity_geometries eg
|
||||||
|
ON eg.entity_id = me.id
|
||||||
|
LEFT JOIN geometries g
|
||||||
|
ON g.id = eg.geometry_id
|
||||||
|
AND g.is_deleted = false
|
||||||
|
ORDER BY me.id DESC, g.id DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
var cursor pgtype.UUID
|
||||||
|
if cursorID != nil {
|
||||||
|
cursor = *cursorID
|
||||||
|
} else {
|
||||||
|
cursor = pgtype.UUID{Valid: false}
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := r.db.Query(ctx, q, name, cursor, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
out := make([]EntityGeometriesSearchRow, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
var entityID pgtype.UUID
|
||||||
|
var entityName pgtype.Text
|
||||||
|
var entityDesc pgtype.Text
|
||||||
|
var geoID pgtype.UUID
|
||||||
|
var geoType pgtype.Int2
|
||||||
|
var draw json.RawMessage
|
||||||
|
var binding json.RawMessage
|
||||||
|
var timeStart pgtype.Int4
|
||||||
|
var timeEnd pgtype.Int4
|
||||||
|
|
||||||
|
if err := rows.Scan(
|
||||||
|
&entityID,
|
||||||
|
&entityName,
|
||||||
|
&entityDesc,
|
||||||
|
&geoID,
|
||||||
|
&geoType,
|
||||||
|
&draw,
|
||||||
|
&binding,
|
||||||
|
&timeStart,
|
||||||
|
&timeEnd,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var ts *int32
|
||||||
|
if timeStart.Valid {
|
||||||
|
v := timeStart.Int32
|
||||||
|
ts = &v
|
||||||
|
}
|
||||||
|
var te *int32
|
||||||
|
if timeEnd.Valid {
|
||||||
|
v := timeEnd.Int32
|
||||||
|
te = &v
|
||||||
|
}
|
||||||
|
|
||||||
|
row := EntityGeometriesSearchRow{
|
||||||
|
EntityID: convert.UUIDToString(entityID),
|
||||||
|
EntityName: entityName.String,
|
||||||
|
EntityDescription: entityDesc.String,
|
||||||
|
GeometryID: convert.UUIDToString(geoID),
|
||||||
|
GeoType: geoType.Int16,
|
||||||
|
DrawGeometry: draw,
|
||||||
|
Binding: binding,
|
||||||
|
TimeStart: ts,
|
||||||
|
TimeEnd: te,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entity with no geometry will have NULL geometry_id.
|
||||||
|
if !geoID.Valid {
|
||||||
|
row.GeometryID = ""
|
||||||
|
row.GeoType = 0
|
||||||
|
row.DrawGeometry = nil
|
||||||
|
row.Binding = nil
|
||||||
|
row.TimeStart = nil
|
||||||
|
row.TimeEnd = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
out = append(out, row)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,5 +9,6 @@ import (
|
|||||||
func GeometryRoutes(router fiber.Router, geometryController *controllers.GeometryController) {
|
func GeometryRoutes(router fiber.Router, geometryController *controllers.GeometryController) {
|
||||||
geometry := router.Group("/geometries")
|
geometry := router.Group("/geometries")
|
||||||
geometry.Get("/", geometryController.SearchGeometries)
|
geometry.Get("/", geometryController.SearchGeometries)
|
||||||
|
geometry.Get("/entity", geometryController.SearchGeometriesByEntityName)
|
||||||
geometry.Get("/:id", geometryController.GetGeometryById)
|
geometry.Get("/:id", geometryController.GetGeometryById)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import (
|
|||||||
type GeometryService interface {
|
type GeometryService interface {
|
||||||
GetGeometryByID(ctx context.Context, id string) (*response.GeometryResponse, *fiber.Error)
|
GetGeometryByID(ctx context.Context, id string) (*response.GeometryResponse, *fiber.Error)
|
||||||
SearchGeometries(ctx context.Context, req *request.SearchGeometryDto) ([]*response.GeometryResponse, *fiber.Error)
|
SearchGeometries(ctx context.Context, req *request.SearchGeometryDto) ([]*response.GeometryResponse, *fiber.Error)
|
||||||
|
SearchGeometriesByEntityName(ctx context.Context, req *request.SearchGeometriesByEntityNameDto) (*response.SearchGeometriesByEntityNameResponse, *fiber.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type geometryService struct {
|
type geometryService struct {
|
||||||
@@ -75,3 +76,74 @@ func (s *geometryService) SearchGeometries(ctx context.Context, req *request.Sea
|
|||||||
|
|
||||||
return models.GeometriesEntityToResponse(geometries), nil
|
return models.GeometriesEntityToResponse(geometries), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *geometryService) SearchGeometriesByEntityName(
|
||||||
|
ctx context.Context,
|
||||||
|
req *request.SearchGeometriesByEntityNameDto,
|
||||||
|
) (*response.SearchGeometriesByEntityNameResponse, *fiber.Error) {
|
||||||
|
limit := int32(req.Limit)
|
||||||
|
if limit <= 0 {
|
||||||
|
limit = 20
|
||||||
|
}
|
||||||
|
|
||||||
|
var cursorID *pgtype.UUID
|
||||||
|
if req.Cursor != "" {
|
||||||
|
id, err := convert.StringToUUID(req.Cursor)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid cursor format")
|
||||||
|
}
|
||||||
|
cursorID = &id
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.geometryRepo.SearchByEntityName(ctx, req.Name, cursorID, limit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to search geometries by entity name")
|
||||||
|
}
|
||||||
|
|
||||||
|
byEntity := make(map[string]*response.EntityGeometriesSearchItem)
|
||||||
|
order := make([]string, 0)
|
||||||
|
|
||||||
|
for _, row := range rows {
|
||||||
|
item := byEntity[row.EntityID]
|
||||||
|
if item == nil {
|
||||||
|
item = &response.EntityGeometriesSearchItem{
|
||||||
|
EntityID: row.EntityID,
|
||||||
|
Name: row.EntityName,
|
||||||
|
Description: row.EntityDescription,
|
||||||
|
Geometries: make([]*response.EntityGeometrySearchGeo, 0),
|
||||||
|
}
|
||||||
|
byEntity[row.EntityID] = item
|
||||||
|
order = append(order, row.EntityID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LEFT JOIN: entity might have no geometry.
|
||||||
|
if row.GeometryID == "" || len(row.DrawGeometry) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
item.Geometries = append(item.Geometries, &response.EntityGeometrySearchGeo{
|
||||||
|
ID: row.GeometryID,
|
||||||
|
GeoType: row.GeoType,
|
||||||
|
DrawGeometry: row.DrawGeometry,
|
||||||
|
Binding: row.Binding,
|
||||||
|
TimeStart: row.TimeStart,
|
||||||
|
TimeEnd: row.TimeEnd,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
items := make([]*response.EntityGeometriesSearchItem, 0, len(order))
|
||||||
|
for _, entityID := range order {
|
||||||
|
items = append(items, byEntity[entityID])
|
||||||
|
}
|
||||||
|
|
||||||
|
nextCursor := ""
|
||||||
|
if len(order) > 0 {
|
||||||
|
// Use last entity id in the current page as the cursor for the next page (id < cursor).
|
||||||
|
nextCursor = order[len(order)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return &response.SearchGeometriesByEntityNameResponse{
|
||||||
|
Items: items,
|
||||||
|
NextCursor: nextCursor,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user