This commit is contained in:
68
internal/services/entityService.go
Normal file
68
internal/services/entityService.go
Normal file
@@ -0,0 +1,68 @@
|
||||
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, error)
|
||||
SearchEntities(ctx context.Context, req *request.SearchEntityDto) ([]*response.EntityResponse, 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, error) {
|
||||
entityId, err := convert.StringToUUID(id)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
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, 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 = req.Name
|
||||
}
|
||||
|
||||
entities, err := s.entityRepo.Search(ctx, params)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return models.EntitiesEntityToResponse(entities), nil
|
||||
}
|
||||
79
internal/services/geometryService.go
Normal file
79
internal/services/geometryService.go
Normal file
@@ -0,0 +1,79 @@
|
||||
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"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type GeometryService interface {
|
||||
GetGeometryByID(ctx context.Context, id string) (*response.GeometryResponse, error)
|
||||
SearchGeometries(ctx context.Context, req *request.SearchGeometryDto) ([]*response.GeometryResponse, error)
|
||||
}
|
||||
|
||||
type geometryService struct {
|
||||
geometryRepo repositories.GeometryRepository
|
||||
}
|
||||
|
||||
func NewGeometryService(geometryRepo repositories.GeometryRepository) GeometryService {
|
||||
return &geometryService{
|
||||
geometryRepo: geometryRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *geometryService) GetGeometryByID(ctx context.Context, id string) (*response.GeometryResponse, error) {
|
||||
geometryId, err := convert.StringToUUID(id)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
geometry, err := s.geometryRepo.GetByID(ctx, geometryId)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Geometry not found")
|
||||
}
|
||||
|
||||
return geometry.ToResponse(), nil
|
||||
}
|
||||
|
||||
func (s *geometryService) SearchGeometries(ctx context.Context, req *request.SearchGeometryDto) ([]*response.GeometryResponse, error) {
|
||||
params := sqlc.SearchGeometriesParams{}
|
||||
|
||||
if req.MinLng != nil && req.MinLat != nil && req.MaxLng != nil && req.MaxLat != nil {
|
||||
if *req.MaxLng < *req.MinLng || *req.MaxLat < *req.MinLat {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid bounding box")
|
||||
}
|
||||
params.SearchMinLng = pgtype.Float8{Float64: *req.MinLng, Valid: true}
|
||||
params.SearchMinLat = pgtype.Float8{Float64: *req.MinLat, Valid: true}
|
||||
params.SearchMaxLng = pgtype.Float8{Float64: *req.MaxLng, Valid: true}
|
||||
params.SearchMaxLat = pgtype.Float8{Float64: *req.MaxLat, Valid: true}
|
||||
} else {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Must provid Bounding box!")
|
||||
}
|
||||
|
||||
if req.TimePoint != nil {
|
||||
if *req.TimePoint < 0 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Time point must be non-negative!")
|
||||
}
|
||||
params.TimePoint = pgtype.Int4{Int32: *req.TimePoint, Valid: true}
|
||||
}
|
||||
|
||||
if req.EntityID != nil {
|
||||
entityId, err := convert.StringToUUID(*req.EntityID)
|
||||
if err == nil {
|
||||
params.EntityID = entityId
|
||||
}
|
||||
}
|
||||
|
||||
geometries, err := s.geometryRepo.Search(ctx, params)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return models.GeometriesEntityToResponse(geometries), nil
|
||||
}
|
||||
@@ -64,8 +64,16 @@ func (u *userService) ChangePassword(ctx context.Context, userId string, dto *re
|
||||
return fiber.NewError(fiber.StatusNotFound, "User not found")
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(dto.OldPassword)); err != nil {
|
||||
return fiber.NewError(fiber.StatusUnauthorized, "Invalid identity or password!")
|
||||
if user.PasswordHash != "" {
|
||||
if dto.OldPassword == "" {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Old password required")
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(dto.OldPassword)); err != nil {
|
||||
return fiber.NewError(fiber.StatusUnauthorized, "Invalid password!")
|
||||
}
|
||||
} else if user.PasswordHash == "" && dto.OldPassword != "" {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request")
|
||||
}
|
||||
|
||||
hashPassword, err := bcrypt.GenerateFromPassword([]byte(dto.NewPassword), bcrypt.DefaultCost)
|
||||
|
||||
74
internal/services/wikiService.go
Normal file
74
internal/services/wikiService.go
Normal file
@@ -0,0 +1,74 @@
|
||||
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 WikiService interface {
|
||||
GetWikiByID(ctx context.Context, id string) (*response.WikiResponse, error)
|
||||
SearchWikis(ctx context.Context, req *request.SearchWikiDto) ([]*response.WikiResponse, error)
|
||||
}
|
||||
|
||||
type wikiService struct {
|
||||
wikiRepo repositories.WikiRepository
|
||||
}
|
||||
|
||||
func NewWikiService(wikiRepo repositories.WikiRepository) WikiService {
|
||||
return &wikiService{
|
||||
wikiRepo: wikiRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *wikiService) GetWikiByID(ctx context.Context, id string) (*response.WikiResponse, error) {
|
||||
wikiId, err := convert.StringToUUID(id)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
wiki, err := s.wikiRepo.GetByID(ctx, wikiId)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Wiki not found")
|
||||
}
|
||||
|
||||
return wiki.ToResponse(), nil
|
||||
}
|
||||
|
||||
func (s *wikiService) SearchWikis(ctx context.Context, req *request.SearchWikiDto) ([]*response.WikiResponse, error) {
|
||||
limit := int32(25)
|
||||
if req.Limit > 0 {
|
||||
limit = int32(req.Limit)
|
||||
}
|
||||
|
||||
params := sqlc.SearchWikisParams{
|
||||
LimitCount: limit,
|
||||
}
|
||||
if req.Cursor != "" {
|
||||
cursor, err := convert.StringToUUID(req.Cursor)
|
||||
if err == nil {
|
||||
params.CursorID = cursor
|
||||
}
|
||||
}
|
||||
if req.Title != "" {
|
||||
params.Title = req.Title
|
||||
}
|
||||
if req.EntityID != "" {
|
||||
entityId, err := convert.StringToUUID(req.EntityID)
|
||||
if err == nil {
|
||||
params.EntityID = entityId
|
||||
}
|
||||
}
|
||||
|
||||
wikis, err := s.wikiRepo.Search(ctx, params)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return models.WikisEntityToResponse(wikis), nil
|
||||
}
|
||||
Reference in New Issue
Block a user