fix: Code for category
This commit is contained in:
@@ -20,21 +20,21 @@ func NewCategoryRepository(pool *pgxpool.Pool) *CategoryRepository {
|
||||
return &CategoryRepository{pool: pool}
|
||||
}
|
||||
|
||||
const categoryColumns = `id, parent_id, name, description, created_at, updated_at`
|
||||
const categoryColumns = `id, parent_id, code, name, description, created_at, updated_at`
|
||||
|
||||
func scanCategory(row pgx.Row) (domain.Category, error) {
|
||||
var c domain.Category
|
||||
err := row.Scan(&c.ID, &c.ParentID, &c.Name, &c.Description, &c.CreatedAt, &c.UpdatedAt)
|
||||
err := row.Scan(&c.ID, &c.ParentID, &c.Code, &c.Name, &c.Description, &c.CreatedAt, &c.UpdatedAt)
|
||||
return c, err
|
||||
}
|
||||
|
||||
// Create inserts a new category and returns the stored row.
|
||||
func (r *CategoryRepository) Create(ctx context.Context, c domain.Category) (domain.Category, error) {
|
||||
row := r.pool.QueryRow(ctx,
|
||||
`INSERT INTO categories (parent_id, name, description)
|
||||
VALUES ($1, $2, $3)
|
||||
`INSERT INTO categories (parent_id, code, name, description)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING `+categoryColumns,
|
||||
c.ParentID, c.Name, c.Description,
|
||||
c.ParentID, c.Code, c.Name, c.Description,
|
||||
)
|
||||
out, err := scanCategory(row)
|
||||
return out, mapError(err)
|
||||
@@ -48,6 +48,14 @@ func (r *CategoryRepository) GetByID(ctx context.Context, id uuid.UUID) (domain.
|
||||
return out, mapError(err)
|
||||
}
|
||||
|
||||
// GetByCode returns the category with the given code, or domain.ErrNotFound.
|
||||
func (r *CategoryRepository) GetByCode(ctx context.Context, code string) (domain.Category, error) {
|
||||
row := r.pool.QueryRow(ctx,
|
||||
`SELECT `+categoryColumns+` FROM categories WHERE code = $1`, code)
|
||||
out, err := scanCategory(row)
|
||||
return out, mapError(err)
|
||||
}
|
||||
|
||||
// List returns categories ordered by name. When parentID is non-nil it filters
|
||||
// to that parent's direct children; otherwise it returns all categories.
|
||||
func (r *CategoryRepository) List(ctx context.Context, parentID *uuid.UUID) ([]domain.Category, error) {
|
||||
@@ -82,10 +90,10 @@ func (r *CategoryRepository) List(ctx context.Context, parentID *uuid.UUID) ([]d
|
||||
func (r *CategoryRepository) Update(ctx context.Context, c domain.Category) (domain.Category, error) {
|
||||
row := r.pool.QueryRow(ctx,
|
||||
`UPDATE categories
|
||||
SET parent_id = $2, name = $3, description = $4, updated_at = now()
|
||||
SET parent_id = $2, code = $3, name = $4, description = $5, updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING `+categoryColumns,
|
||||
c.ID, c.ParentID, c.Name, c.Description,
|
||||
c.ID, c.ParentID, c.Code, c.Name, c.Description,
|
||||
)
|
||||
out, err := scanCategory(row)
|
||||
return out, mapError(err)
|
||||
|
||||
Reference in New Issue
Block a user