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:
172
core-service/internal/api/handlers/items.go
Normal file
172
core-service/internal/api/handlers/items.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"erp-mvp/core-service/internal/api/middleware"
|
||||
"erp-mvp/core-service/internal/models"
|
||||
"erp-mvp/core-service/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ItemHandler struct {
|
||||
itemService service.ItemService
|
||||
validate *validator.Validate
|
||||
}
|
||||
|
||||
func NewItemHandler(itemService service.ItemService) *ItemHandler {
|
||||
return &ItemHandler{
|
||||
itemService: itemService,
|
||||
validate: validator.New(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetItems получает все товары организации
|
||||
func (h *ItemHandler) GetItems(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
items, err := h.itemService.GetItems(c.Request.Context(), claims.OrganizationID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get items"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, items)
|
||||
}
|
||||
|
||||
// CreateItem создает новый товар
|
||||
func (h *ItemHandler) CreateItem(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
var req models.CreateItemRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.validate.Struct(req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.itemService.CreateItem(c.Request.Context(), claims.OrganizationID, &req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create item"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, item)
|
||||
}
|
||||
|
||||
// GetItem получает товар по ID
|
||||
func (h *ItemHandler) GetItem(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
idStr := c.Param("id")
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"})
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.itemService.GetItem(c.Request.Context(), id, claims.OrganizationID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, item)
|
||||
}
|
||||
|
||||
// UpdateItem обновляет товар
|
||||
func (h *ItemHandler) UpdateItem(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
idStr := c.Param("id")
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var req models.CreateItemRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.validate.Struct(req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.itemService.UpdateItem(c.Request.Context(), id, claims.OrganizationID, &req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, item)
|
||||
}
|
||||
|
||||
// DeleteItem удаляет товар
|
||||
func (h *ItemHandler) DeleteItem(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
idStr := c.Param("id")
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.itemService.DeleteItem(c.Request.Context(), id, claims.OrganizationID); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNoContent, nil)
|
||||
}
|
||||
|
||||
// SearchItems ищет товары
|
||||
func (h *ItemHandler) SearchItems(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
query := c.Query("q")
|
||||
category := c.Query("category")
|
||||
|
||||
items, err := h.itemService.SearchItems(c.Request.Context(), claims.OrganizationID, query, category)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to search items"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, items)
|
||||
}
|
||||
176
core-service/internal/api/handlers/locations.go
Normal file
176
core-service/internal/api/handlers/locations.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"erp-mvp/core-service/internal/api/middleware"
|
||||
"erp-mvp/core-service/internal/models"
|
||||
"erp-mvp/core-service/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type LocationHandler struct {
|
||||
locationService service.LocationService
|
||||
validate *validator.Validate
|
||||
}
|
||||
|
||||
func NewLocationHandler(locationService service.LocationService) *LocationHandler {
|
||||
return &LocationHandler{
|
||||
locationService: locationService,
|
||||
validate: validator.New(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetLocations получает все места хранения организации
|
||||
func (h *LocationHandler) GetLocations(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
locations, err := h.locationService.GetLocations(c.Request.Context(), claims.OrganizationID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get locations"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, locations)
|
||||
}
|
||||
|
||||
// CreateLocation создает новое место хранения
|
||||
func (h *LocationHandler) CreateLocation(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
var req models.CreateLocationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.validate.Struct(req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
location, err := h.locationService.CreateLocation(c.Request.Context(), claims.OrganizationID, &req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create location"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, location)
|
||||
}
|
||||
|
||||
// GetLocation получает место хранения по ID
|
||||
func (h *LocationHandler) GetLocation(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
idStr := c.Param("id")
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid location ID"})
|
||||
return
|
||||
}
|
||||
|
||||
location, err := h.locationService.GetLocation(c.Request.Context(), id, claims.OrganizationID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Location not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, location)
|
||||
}
|
||||
|
||||
// UpdateLocation обновляет место хранения
|
||||
func (h *LocationHandler) UpdateLocation(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
idStr := c.Param("id")
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid location ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var req models.CreateLocationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.validate.Struct(req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
location, err := h.locationService.UpdateLocation(c.Request.Context(), id, claims.OrganizationID, &req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Location not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, location)
|
||||
}
|
||||
|
||||
// DeleteLocation удаляет место хранения
|
||||
func (h *LocationHandler) DeleteLocation(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
idStr := c.Param("id")
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid location ID"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.locationService.DeleteLocation(c.Request.Context(), id, claims.OrganizationID); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Location not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNoContent, nil)
|
||||
}
|
||||
|
||||
// GetChildren получает дочерние места хранения
|
||||
func (h *LocationHandler) GetChildren(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
parentIDStr := c.Param("id")
|
||||
parentID, err := uuid.Parse(parentIDStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid parent location ID"})
|
||||
return
|
||||
}
|
||||
|
||||
children, err := h.locationService.GetChildren(c.Request.Context(), parentID, claims.OrganizationID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Parent location not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, children)
|
||||
}
|
||||
227
core-service/internal/api/handlers/operations.go
Normal file
227
core-service/internal/api/handlers/operations.go
Normal file
@@ -0,0 +1,227 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"erp-mvp/core-service/internal/api/middleware"
|
||||
"erp-mvp/core-service/internal/models"
|
||||
"erp-mvp/core-service/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type OperationsHandler struct {
|
||||
operationsService service.OperationsService
|
||||
validate *validator.Validate
|
||||
}
|
||||
|
||||
func NewOperationsHandler(operationsService service.OperationsService) *OperationsHandler {
|
||||
return &OperationsHandler{
|
||||
operationsService: operationsService,
|
||||
validate: validator.New(),
|
||||
}
|
||||
}
|
||||
|
||||
// PlaceItem размещает товар в месте хранения
|
||||
func (h *OperationsHandler) PlaceItem(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
var req models.PlaceItemRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.validate.Struct(req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
placement, err := h.operationsService.PlaceItem(c.Request.Context(), claims.OrganizationID, &req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to place item"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, placement)
|
||||
}
|
||||
|
||||
// MoveItem перемещает товар в другое место хранения
|
||||
func (h *OperationsHandler) MoveItem(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
placementIDStr := c.Param("id")
|
||||
placementID, err := uuid.Parse(placementIDStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid placement ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
NewLocationID uuid.UUID `json:"new_location_id" validate:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.validate.Struct(req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.operationsService.MoveItem(c.Request.Context(), placementID, req.NewLocationID, claims.OrganizationID); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Placement not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Item moved successfully"})
|
||||
}
|
||||
|
||||
// GetItemPlacements получает все размещения товара
|
||||
func (h *OperationsHandler) GetItemPlacements(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
itemIDStr := c.Param("item_id")
|
||||
itemID, err := uuid.Parse(itemIDStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"})
|
||||
return
|
||||
}
|
||||
|
||||
placements, err := h.operationsService.GetItemPlacements(c.Request.Context(), itemID, claims.OrganizationID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Item not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, placements)
|
||||
}
|
||||
|
||||
// GetLocationPlacements получает все товары в месте хранения
|
||||
func (h *OperationsHandler) GetLocationPlacements(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
locationIDStr := c.Param("location_id")
|
||||
locationID, err := uuid.Parse(locationIDStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid location ID"})
|
||||
return
|
||||
}
|
||||
|
||||
placements, err := h.operationsService.GetLocationPlacements(c.Request.Context(), locationID, claims.OrganizationID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Location not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, placements)
|
||||
}
|
||||
|
||||
// UpdateQuantity обновляет количество товара в размещении
|
||||
func (h *OperationsHandler) UpdateQuantity(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
placementIDStr := c.Param("id")
|
||||
placementID, err := uuid.Parse(placementIDStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid placement ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Quantity int `json:"quantity" validate:"required,min=1"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.validate.Struct(req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Validation failed", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.operationsService.UpdateQuantity(c.Request.Context(), placementID, req.Quantity, claims.OrganizationID); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Placement not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Quantity updated successfully"})
|
||||
}
|
||||
|
||||
// DeletePlacement удаляет размещение товара
|
||||
func (h *OperationsHandler) DeletePlacement(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
placementIDStr := c.Param("id")
|
||||
placementID, err := uuid.Parse(placementIDStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid placement ID"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.operationsService.DeletePlacement(c.Request.Context(), placementID, claims.OrganizationID); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Placement not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNoContent, nil)
|
||||
}
|
||||
|
||||
// Search выполняет поиск товаров с местами размещения
|
||||
func (h *OperationsHandler) Search(c *gin.Context) {
|
||||
claims := middleware.GetClaims(c)
|
||||
if claims == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
var req models.SearchRequest
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"})
|
||||
return
|
||||
}
|
||||
|
||||
// Устанавливаем значения по умолчанию
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize <= 0 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
response, err := h.operationsService.Search(c.Request.Context(), claims.OrganizationID, &req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to search"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
Reference in New Issue
Block a user