All checks were successful
Build and Release / release (push) Successful in 1m13s
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"history-api/internal/dtos/request"
|
|
"history-api/internal/dtos/response"
|
|
"history-api/internal/gen/sqlc"
|
|
"history-api/internal/models"
|
|
"history-api/internal/repositories"
|
|
"history-api/pkg/convert"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
type EntityService interface {
|
|
GetEntityByID(ctx context.Context, id string) (*response.EntityResponse, *fiber.Error)
|
|
SearchEntities(ctx context.Context, req *request.SearchEntityDto) ([]*response.EntityResponse, *fiber.Error)
|
|
}
|
|
|
|
type entityService struct {
|
|
entityRepo repositories.EntityRepository
|
|
}
|
|
|
|
func NewEntityService(entityRepo repositories.EntityRepository) EntityService {
|
|
return &entityService{
|
|
entityRepo: entityRepo,
|
|
}
|
|
}
|
|
|
|
func (s *entityService) GetEntityByID(ctx context.Context, id string) (*response.EntityResponse, *fiber.Error) {
|
|
entityId, err := convert.StringToUUID(id)
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid entity ID format")
|
|
}
|
|
entity, err := s.entityRepo.GetByID(ctx, entityId)
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusNotFound, "Entity not found")
|
|
}
|
|
|
|
return entity.ToResponse(), nil
|
|
}
|
|
|
|
func (s *entityService) SearchEntities(ctx context.Context, req *request.SearchEntityDto) ([]*response.EntityResponse, *fiber.Error) {
|
|
limit := int32(25)
|
|
if req.Limit > 0 {
|
|
limit = int32(req.Limit)
|
|
}
|
|
|
|
params := sqlc.SearchEntitiesParams{
|
|
LimitCount: limit,
|
|
}
|
|
if req.Cursor != "" {
|
|
cursor, err := convert.StringToUUID(req.Cursor)
|
|
if err == nil {
|
|
params.CursorID = cursor
|
|
}
|
|
}
|
|
|
|
if req.Name != "" {
|
|
params.Name = convert.PtrToText(&req.Name)
|
|
}
|
|
|
|
if req.ProjectID != nil {
|
|
projectID, err := convert.StringToUUID(*req.ProjectID)
|
|
if err == nil {
|
|
params.ProjectID = projectID
|
|
}
|
|
}
|
|
|
|
entities, err := s.entityRepo.Search(ctx, params)
|
|
if err != nil {
|
|
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to search entities")
|
|
}
|
|
|
|
return models.EntitiesEntityToResponse(entities), nil
|
|
}
|