feat: завершён этап 3 - API структура Core Service
- Созданы репозитории для locations, items, operations - Реализованы сервисы с бизнес-логикой - Созданы HTTP handlers для всех API endpoints - Добавлена функция GetClaims в middleware - Обновлён server.go для интеграции всех компонентов - Поддержка JSON полей в PostgreSQL - Organization-scope фильтрация во всех операциях - Валидация запросов через validator Готово для этапа 4 - Шаблоны помещений
This commit is contained in:
126
core-service/internal/service/item_service.go
Normal file
126
core-service/internal/service/item_service.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"erp-mvp/core-service/internal/models"
|
||||
"erp-mvp/core-service/internal/repository"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ItemService interface {
|
||||
CreateItem(ctx context.Context, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error)
|
||||
GetItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.Item, error)
|
||||
GetItems(ctx context.Context, orgID uuid.UUID) ([]*models.Item, error)
|
||||
UpdateItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error)
|
||||
DeleteItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
||||
SearchItems(ctx context.Context, orgID uuid.UUID, query string, category string) ([]*models.Item, error)
|
||||
}
|
||||
|
||||
type itemService struct {
|
||||
itemRepo repository.ItemRepository
|
||||
logger *logrus.Logger
|
||||
}
|
||||
|
||||
func NewItemService(itemRepo repository.ItemRepository) ItemService {
|
||||
return &itemService{
|
||||
itemRepo: itemRepo,
|
||||
logger: logrus.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *itemService) CreateItem(ctx context.Context, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error) {
|
||||
s.logger.Info("Creating item for organization: ", orgID)
|
||||
|
||||
item := &models.Item{
|
||||
ID: uuid.New(),
|
||||
OrganizationID: orgID,
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
Category: req.Category,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := s.itemRepo.Create(ctx, item); err != nil {
|
||||
s.logger.Error("Failed to create item: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("Item created successfully: ", item.ID)
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (s *itemService) GetItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.Item, error) {
|
||||
s.logger.Info("Getting item: ", id, " for organization: ", orgID)
|
||||
|
||||
item, err := s.itemRepo.GetByID(ctx, id, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get item: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (s *itemService) GetItems(ctx context.Context, orgID uuid.UUID) ([]*models.Item, error) {
|
||||
s.logger.Info("Getting all items for organization: ", orgID)
|
||||
|
||||
items, err := s.itemRepo.GetByOrganization(ctx, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get items: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *itemService) UpdateItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateItemRequest) (*models.Item, error) {
|
||||
s.logger.Info("Updating item: ", id, " for organization: ", orgID)
|
||||
|
||||
// Сначала получаем существующий товар
|
||||
item, err := s.itemRepo.GetByID(ctx, id, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get item for update: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Обновляем поля
|
||||
item.Name = req.Name
|
||||
item.Description = req.Description
|
||||
item.Category = req.Category
|
||||
|
||||
if err := s.itemRepo.Update(ctx, item); err != nil {
|
||||
s.logger.Error("Failed to update item: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("Item updated successfully: ", item.ID)
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (s *itemService) DeleteItem(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error {
|
||||
s.logger.Info("Deleting item: ", id, " for organization: ", orgID)
|
||||
|
||||
if err := s.itemRepo.Delete(ctx, id, orgID); err != nil {
|
||||
s.logger.Error("Failed to delete item: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Item deleted successfully: ", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *itemService) SearchItems(ctx context.Context, orgID uuid.UUID, query string, category string) ([]*models.Item, error) {
|
||||
s.logger.Info("Searching items for organization: ", orgID, " query: ", query, " category: ", category)
|
||||
|
||||
items, err := s.itemRepo.Search(ctx, orgID, query, category)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to search items: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
130
core-service/internal/service/location_service.go
Normal file
130
core-service/internal/service/location_service.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"erp-mvp/core-service/internal/models"
|
||||
"erp-mvp/core-service/internal/repository"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type LocationService interface {
|
||||
CreateLocation(ctx context.Context, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error)
|
||||
GetLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.StorageLocation, error)
|
||||
GetLocations(ctx context.Context, orgID uuid.UUID) ([]*models.StorageLocation, error)
|
||||
UpdateLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error)
|
||||
DeleteLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error
|
||||
GetChildren(ctx context.Context, parentID uuid.UUID, orgID uuid.UUID) ([]*models.StorageLocation, error)
|
||||
}
|
||||
|
||||
type locationService struct {
|
||||
locationRepo repository.LocationRepository
|
||||
logger *logrus.Logger
|
||||
}
|
||||
|
||||
func NewLocationService(locationRepo repository.LocationRepository) LocationService {
|
||||
return &locationService{
|
||||
locationRepo: locationRepo,
|
||||
logger: logrus.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *locationService) CreateLocation(ctx context.Context, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error) {
|
||||
s.logger.Info("Creating location for organization: ", orgID)
|
||||
|
||||
location := &models.StorageLocation{
|
||||
ID: uuid.New(),
|
||||
OrganizationID: orgID,
|
||||
ParentID: req.ParentID,
|
||||
Name: req.Name,
|
||||
Address: req.Address,
|
||||
Type: req.Type,
|
||||
Coordinates: req.Coordinates,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := s.locationRepo.Create(ctx, location); err != nil {
|
||||
s.logger.Error("Failed to create location: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("Location created successfully: ", location.ID)
|
||||
return location, nil
|
||||
}
|
||||
|
||||
func (s *locationService) GetLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) (*models.StorageLocation, error) {
|
||||
s.logger.Info("Getting location: ", id, " for organization: ", orgID)
|
||||
|
||||
location, err := s.locationRepo.GetByID(ctx, id, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get location: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return location, nil
|
||||
}
|
||||
|
||||
func (s *locationService) GetLocations(ctx context.Context, orgID uuid.UUID) ([]*models.StorageLocation, error) {
|
||||
s.logger.Info("Getting all locations for organization: ", orgID)
|
||||
|
||||
locations, err := s.locationRepo.GetByOrganization(ctx, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get locations: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return locations, nil
|
||||
}
|
||||
|
||||
func (s *locationService) UpdateLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID, req *models.CreateLocationRequest) (*models.StorageLocation, error) {
|
||||
s.logger.Info("Updating location: ", id, " for organization: ", orgID)
|
||||
|
||||
// Сначала получаем существующую локацию
|
||||
location, err := s.locationRepo.GetByID(ctx, id, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get location for update: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Обновляем поля
|
||||
location.ParentID = req.ParentID
|
||||
location.Name = req.Name
|
||||
location.Address = req.Address
|
||||
location.Type = req.Type
|
||||
location.Coordinates = req.Coordinates
|
||||
|
||||
if err := s.locationRepo.Update(ctx, location); err != nil {
|
||||
s.logger.Error("Failed to update location: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("Location updated successfully: ", location.ID)
|
||||
return location, nil
|
||||
}
|
||||
|
||||
func (s *locationService) DeleteLocation(ctx context.Context, id uuid.UUID, orgID uuid.UUID) error {
|
||||
s.logger.Info("Deleting location: ", id, " for organization: ", orgID)
|
||||
|
||||
if err := s.locationRepo.Delete(ctx, id, orgID); err != nil {
|
||||
s.logger.Error("Failed to delete location: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Location deleted successfully: ", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *locationService) GetChildren(ctx context.Context, parentID uuid.UUID, orgID uuid.UUID) ([]*models.StorageLocation, error) {
|
||||
s.logger.Info("Getting children for location: ", parentID, " in organization: ", orgID)
|
||||
|
||||
children, err := s.locationRepo.GetChildren(ctx, parentID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get children: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return children, nil
|
||||
}
|
||||
192
core-service/internal/service/operations_service.go
Normal file
192
core-service/internal/service/operations_service.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"erp-mvp/core-service/internal/models"
|
||||
"erp-mvp/core-service/internal/repository"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type OperationsService interface {
|
||||
PlaceItem(ctx context.Context, orgID uuid.UUID, req *models.PlaceItemRequest) (*models.ItemPlacement, error)
|
||||
MoveItem(ctx context.Context, placementID uuid.UUID, newLocationID uuid.UUID, orgID uuid.UUID) error
|
||||
GetItemPlacements(ctx context.Context, itemID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error)
|
||||
GetLocationPlacements(ctx context.Context, locationID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error)
|
||||
UpdateQuantity(ctx context.Context, placementID uuid.UUID, quantity int, orgID uuid.UUID) error
|
||||
DeletePlacement(ctx context.Context, placementID uuid.UUID, orgID uuid.UUID) error
|
||||
Search(ctx context.Context, orgID uuid.UUID, req *models.SearchRequest) (*models.SearchResponse, error)
|
||||
}
|
||||
|
||||
type operationsService struct {
|
||||
operationsRepo repository.OperationsRepository
|
||||
itemRepo repository.ItemRepository
|
||||
locationRepo repository.LocationRepository
|
||||
logger *logrus.Logger
|
||||
}
|
||||
|
||||
func NewOperationsService(operationsRepo repository.OperationsRepository, itemRepo repository.ItemRepository, locationRepo repository.LocationRepository) OperationsService {
|
||||
return &operationsService{
|
||||
operationsRepo: operationsRepo,
|
||||
itemRepo: itemRepo,
|
||||
locationRepo: locationRepo,
|
||||
logger: logrus.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *operationsService) PlaceItem(ctx context.Context, orgID uuid.UUID, req *models.PlaceItemRequest) (*models.ItemPlacement, error) {
|
||||
s.logger.Info("Placing item: ", req.ItemID, " in location: ", req.LocationID, " for organization: ", orgID)
|
||||
|
||||
// Проверяем, что товар существует и принадлежит организации
|
||||
_, err := s.itemRepo.GetByID(ctx, req.ItemID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Item not found or not accessible: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Проверяем, что место хранения существует и принадлежит организации
|
||||
_, err = s.locationRepo.GetByID(ctx, req.LocationID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Location not found or not accessible: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
placement := &models.ItemPlacement{
|
||||
ID: uuid.New(),
|
||||
OrganizationID: orgID,
|
||||
ItemID: req.ItemID,
|
||||
LocationID: req.LocationID,
|
||||
Quantity: req.Quantity,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := s.operationsRepo.PlaceItem(ctx, placement); err != nil {
|
||||
s.logger.Error("Failed to place item: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("Item placed successfully: ", placement.ID)
|
||||
return placement, nil
|
||||
}
|
||||
|
||||
func (s *operationsService) MoveItem(ctx context.Context, placementID uuid.UUID, newLocationID uuid.UUID, orgID uuid.UUID) error {
|
||||
s.logger.Info("Moving item placement: ", placementID, " to location: ", newLocationID, " for organization: ", orgID)
|
||||
|
||||
// Проверяем, что размещение существует и принадлежит организации
|
||||
placement, err := s.operationsRepo.GetByID(ctx, placementID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Item placement not found or not accessible: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Проверяем, что новое место хранения существует и принадлежит организации
|
||||
_, err = s.locationRepo.GetByID(ctx, newLocationID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("New location not found or not accessible: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.operationsRepo.MoveItem(ctx, placementID, newLocationID, orgID); err != nil {
|
||||
s.logger.Error("Failed to move item: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Item moved successfully from location: ", placement.LocationID, " to: ", newLocationID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *operationsService) GetItemPlacements(ctx context.Context, itemID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error) {
|
||||
s.logger.Info("Getting placements for item: ", itemID, " in organization: ", orgID)
|
||||
|
||||
// Проверяем, что товар существует и принадлежит организации
|
||||
_, err := s.itemRepo.GetByID(ctx, itemID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Item not found or not accessible: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
placements, err := s.operationsRepo.GetByItem(ctx, itemID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get item placements: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return placements, nil
|
||||
}
|
||||
|
||||
func (s *operationsService) GetLocationPlacements(ctx context.Context, locationID uuid.UUID, orgID uuid.UUID) ([]*models.ItemPlacement, error) {
|
||||
s.logger.Info("Getting placements for location: ", locationID, " in organization: ", orgID)
|
||||
|
||||
// Проверяем, что место хранения существует и принадлежит организации
|
||||
_, err := s.locationRepo.GetByID(ctx, locationID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Location not found or not accessible: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
placements, err := s.operationsRepo.GetByLocation(ctx, locationID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get location placements: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return placements, nil
|
||||
}
|
||||
|
||||
func (s *operationsService) UpdateQuantity(ctx context.Context, placementID uuid.UUID, quantity int, orgID uuid.UUID) error {
|
||||
s.logger.Info("Updating quantity for placement: ", placementID, " to: ", quantity, " in organization: ", orgID)
|
||||
|
||||
// Проверяем, что размещение существует и принадлежит организации
|
||||
_, err := s.operationsRepo.GetByID(ctx, placementID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Item placement not found or not accessible: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.operationsRepo.UpdateQuantity(ctx, placementID, quantity, orgID); err != nil {
|
||||
s.logger.Error("Failed to update quantity: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Quantity updated successfully for placement: ", placementID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *operationsService) DeletePlacement(ctx context.Context, placementID uuid.UUID, orgID uuid.UUID) error {
|
||||
s.logger.Info("Deleting placement: ", placementID, " for organization: ", orgID)
|
||||
|
||||
// Проверяем, что размещение существует и принадлежит организации
|
||||
_, err := s.operationsRepo.GetByID(ctx, placementID, orgID)
|
||||
if err != nil {
|
||||
s.logger.Error("Item placement not found or not accessible: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.operationsRepo.Delete(ctx, placementID, orgID); err != nil {
|
||||
s.logger.Error("Failed to delete placement: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("Placement deleted successfully: ", placementID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *operationsService) Search(ctx context.Context, orgID uuid.UUID, req *models.SearchRequest) (*models.SearchResponse, error) {
|
||||
s.logger.Info("Searching items with locations for organization: ", orgID, " query: ", req.Query)
|
||||
|
||||
results, err := s.operationsRepo.Search(ctx, orgID, req.Query, req.Category, req.Address)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to search items with locations: ", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response := &models.SearchResponse{
|
||||
Items: results,
|
||||
TotalCount: len(results),
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
Reference in New Issue
Block a user