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)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"erp-mvp/core-service/internal/auth"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AuthMiddleware struct {
|
||||
@@ -55,3 +56,33 @@ func (m *AuthMiddleware) AuthRequired() gin.HandlerFunc {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// GetClaims получает claims из контекста Gin
|
||||
func GetClaims(c *gin.Context) *auth.Claims {
|
||||
userID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
orgID, exists := c.Get("organization_id")
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
email, exists := c.Get("email")
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
role, exists := c.Get("role")
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &auth.Claims{
|
||||
UserID: userID.(uuid.UUID),
|
||||
OrganizationID: orgID.(uuid.UUID),
|
||||
Email: email.(string),
|
||||
Role: role.(string),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,10 +23,16 @@ type Server struct {
|
||||
router *gin.Engine
|
||||
|
||||
// Services
|
||||
authService service.AuthService
|
||||
authService service.AuthService
|
||||
locationService service.LocationService
|
||||
itemService service.ItemService
|
||||
operationsService service.OperationsService
|
||||
|
||||
// Handlers
|
||||
authHandler *handlers.AuthHandler
|
||||
authHandler *handlers.AuthHandler
|
||||
locationHandler *handlers.LocationHandler
|
||||
itemHandler *handlers.ItemHandler
|
||||
operationsHandler *handlers.OperationsHandler
|
||||
|
||||
// Middleware
|
||||
authMiddleware *middleware.AuthMiddleware
|
||||
@@ -39,24 +45,39 @@ func NewServer(cfg *config.Config, db *sql.DB, log logger.Logger) *Server {
|
||||
// Инициализируем репозитории
|
||||
orgRepo := repository.NewOrganizationRepository(db)
|
||||
userRepo := repository.NewUserRepository(db)
|
||||
locationRepo := repository.NewLocationRepository(db)
|
||||
itemRepo := repository.NewItemRepository(db)
|
||||
operationsRepo := repository.NewOperationsRepository(db)
|
||||
|
||||
// Инициализируем сервисы
|
||||
authService := service.NewAuthService(orgRepo, userRepo, jwtService)
|
||||
locationService := service.NewLocationService(locationRepo)
|
||||
itemService := service.NewItemService(itemRepo)
|
||||
operationsService := service.NewOperationsService(operationsRepo, itemRepo, locationRepo)
|
||||
|
||||
// Инициализируем handlers
|
||||
authHandler := handlers.NewAuthHandler(authService)
|
||||
locationHandler := handlers.NewLocationHandler(locationService)
|
||||
itemHandler := handlers.NewItemHandler(itemService)
|
||||
operationsHandler := handlers.NewOperationsHandler(operationsService)
|
||||
|
||||
// Инициализируем middleware
|
||||
authMiddleware := middleware.NewAuthMiddleware(jwtService)
|
||||
|
||||
server := &Server{
|
||||
config: cfg,
|
||||
db: db,
|
||||
logger: log,
|
||||
router: gin.Default(),
|
||||
authService: authService,
|
||||
authHandler: authHandler,
|
||||
authMiddleware: authMiddleware,
|
||||
config: cfg,
|
||||
db: db,
|
||||
logger: log,
|
||||
router: gin.Default(),
|
||||
authService: authService,
|
||||
locationService: locationService,
|
||||
itemService: itemService,
|
||||
operationsService: operationsService,
|
||||
authHandler: authHandler,
|
||||
locationHandler: locationHandler,
|
||||
itemHandler: itemHandler,
|
||||
operationsHandler: operationsHandler,
|
||||
authMiddleware: authMiddleware,
|
||||
}
|
||||
|
||||
server.setupRoutes()
|
||||
@@ -86,23 +107,29 @@ func (s *Server) setupRoutes() {
|
||||
protected.PUT("/organizations/:id", s.updateOrganization)
|
||||
|
||||
// Locations
|
||||
protected.GET("/locations", s.getLocations)
|
||||
protected.POST("/locations", s.createLocation)
|
||||
protected.GET("/locations/:id", s.getLocation)
|
||||
protected.PUT("/locations/:id", s.updateLocation)
|
||||
protected.DELETE("/locations/:id", s.deleteLocation)
|
||||
protected.GET("/locations", s.locationHandler.GetLocations)
|
||||
protected.POST("/locations", s.locationHandler.CreateLocation)
|
||||
protected.GET("/locations/:id", s.locationHandler.GetLocation)
|
||||
protected.PUT("/locations/:id", s.locationHandler.UpdateLocation)
|
||||
protected.DELETE("/locations/:id", s.locationHandler.DeleteLocation)
|
||||
protected.GET("/locations/:id/children", s.locationHandler.GetChildren)
|
||||
|
||||
// Items
|
||||
protected.GET("/items", s.getItems)
|
||||
protected.POST("/items", s.createItem)
|
||||
protected.GET("/items/:id", s.getItem)
|
||||
protected.PUT("/items/:id", s.updateItem)
|
||||
protected.DELETE("/items/:id", s.deleteItem)
|
||||
protected.GET("/items", s.itemHandler.GetItems)
|
||||
protected.POST("/items", s.itemHandler.CreateItem)
|
||||
protected.GET("/items/:id", s.itemHandler.GetItem)
|
||||
protected.PUT("/items/:id", s.itemHandler.UpdateItem)
|
||||
protected.DELETE("/items/:id", s.itemHandler.DeleteItem)
|
||||
protected.GET("/items/search", s.itemHandler.SearchItems)
|
||||
|
||||
// Operations
|
||||
protected.POST("/operations/place-item", s.placeItem)
|
||||
protected.POST("/operations/move-item", s.moveItem)
|
||||
protected.GET("/operations/search", s.search)
|
||||
protected.POST("/operations/place-item", s.operationsHandler.PlaceItem)
|
||||
protected.POST("/operations/move-item/:id", s.operationsHandler.MoveItem)
|
||||
protected.GET("/operations/search", s.operationsHandler.Search)
|
||||
protected.GET("/operations/items/:item_id/placements", s.operationsHandler.GetItemPlacements)
|
||||
protected.GET("/operations/locations/:location_id/placements", s.operationsHandler.GetLocationPlacements)
|
||||
protected.PUT("/operations/placements/:id/quantity", s.operationsHandler.UpdateQuantity)
|
||||
protected.DELETE("/operations/placements/:id", s.operationsHandler.DeletePlacement)
|
||||
|
||||
// Templates
|
||||
protected.GET("/templates", s.getTemplates)
|
||||
@@ -127,58 +154,6 @@ func (s *Server) updateOrganization(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) getLocations(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) createLocation(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) getLocation(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) updateLocation(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) deleteLocation(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) getItems(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) createItem(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) getItem(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) updateItem(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) deleteItem(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) placeItem(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) moveItem(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) search(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
func (s *Server) getTemplates(c *gin.Context) {
|
||||
c.JSON(http.StatusNotImplemented, gin.H{"error": "Not implemented yet"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user