style: форматирование кода - убраны лишние пустые строки и исправлены импорты
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"erp-mvp/core-service/internal/models"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
@@ -31,7 +32,7 @@ func (r *itemRepository) Create(ctx context.Context, item *models.Item) error {
|
||||
INSERT INTO items (id, organization_id, name, description, category, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
`
|
||||
|
||||
|
||||
_, err := r.db.ExecContext(ctx, query,
|
||||
item.ID,
|
||||
item.OrganizationID,
|
||||
@@ -43,7 +44,7 @@ func (r *itemRepository) Create(ctx context.Context, item *models.Item) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create item: %w", err)
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -53,7 +54,7 @@ func (r *itemRepository) GetByID(ctx context.Context, id uuid.UUID, orgID uuid.U
|
||||
FROM items
|
||||
WHERE id = $1 AND organization_id = $2
|
||||
`
|
||||
|
||||
|
||||
item := &models.Item{}
|
||||
err := r.db.QueryRowContext(ctx, query, id, orgID).Scan(
|
||||
&item.ID,
|
||||
@@ -63,14 +64,14 @@ func (r *itemRepository) GetByID(ctx context.Context, id uuid.UUID, orgID uuid.U
|
||||
&item.Category,
|
||||
&item.CreatedAt,
|
||||
)
|
||||
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("item not found")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get item: %w", err)
|
||||
}
|
||||
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
||||
@@ -81,13 +82,13 @@ func (r *itemRepository) GetByOrganization(ctx context.Context, orgID uuid.UUID)
|
||||
WHERE organization_id = $1
|
||||
ORDER BY name
|
||||
`
|
||||
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, query, orgID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query items: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
||||
var items []*models.Item
|
||||
for rows.Next() {
|
||||
item := &models.Item{}
|
||||
@@ -102,14 +103,14 @@ func (r *itemRepository) GetByOrganization(ctx context.Context, orgID uuid.UUID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to scan item: %w", err)
|
||||
}
|
||||
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("error iterating items: %w", err)
|
||||
}
|
||||
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
@@ -119,7 +120,7 @@ func (r *itemRepository) Update(ctx context.Context, item *models.Item) error {
|
||||
SET name = $3, description = $4, category = $5
|
||||
WHERE id = $1 AND organization_id = $2
|
||||
`
|
||||
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query,
|
||||
item.ID,
|
||||
item.OrganizationID,
|
||||
@@ -130,16 +131,16 @@ func (r *itemRepository) Update(ctx context.Context, item *models.Item) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update item: %w", err)
|
||||
}
|
||||
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("item not found")
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -148,21 +149,21 @@ func (r *itemRepository) Delete(ctx context.Context, id uuid.UUID, orgID uuid.UU
|
||||
DELETE FROM items
|
||||
WHERE id = $1 AND organization_id = $2
|
||||
`
|
||||
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query, id, orgID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete item: %w", err)
|
||||
}
|
||||
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("item not found")
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -172,31 +173,31 @@ func (r *itemRepository) Search(ctx context.Context, orgID uuid.UUID, query stri
|
||||
FROM items
|
||||
WHERE organization_id = $1
|
||||
`
|
||||
|
||||
|
||||
var args []interface{}
|
||||
args = append(args, orgID)
|
||||
argIndex := 2
|
||||
|
||||
|
||||
if query != "" {
|
||||
baseQuery += fmt.Sprintf(" AND (name ILIKE $%d OR description ILIKE $%d)", argIndex, argIndex)
|
||||
args = append(args, "%"+query+"%")
|
||||
argIndex++
|
||||
}
|
||||
|
||||
|
||||
if category != "" {
|
||||
baseQuery += fmt.Sprintf(" AND category = $%d", argIndex)
|
||||
args = append(args, category)
|
||||
argIndex++
|
||||
}
|
||||
|
||||
|
||||
baseQuery += " ORDER BY name"
|
||||
|
||||
|
||||
rows, err := r.db.QueryContext(ctx, baseQuery, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to search items: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
|
||||
var items []*models.Item
|
||||
for rows.Next() {
|
||||
item := &models.Item{}
|
||||
@@ -211,13 +212,13 @@ func (r *itemRepository) Search(ctx context.Context, orgID uuid.UUID, query stri
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to scan item: %w", err)
|
||||
}
|
||||
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("error iterating search results: %w", err)
|
||||
}
|
||||
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user