All checks were successful
Dev Build / build-test (pull_request) Successful in 2m39s
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// MaterialFinishRepository handles database queries for material finishes.
|
|
type MaterialFinishRepository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
// NewMaterialFinishRepository creates a MaterialFinishRepository backed by the given pool.
|
|
func NewMaterialFinishRepository(pool *pgxpool.Pool) *MaterialFinishRepository {
|
|
return &MaterialFinishRepository{pool: pool}
|
|
}
|
|
|
|
// GetAll returns all material finishes ordered by name.
|
|
func (r *MaterialFinishRepository) GetAll(ctx context.Context) ([]models.MaterialFinish, error) {
|
|
rows, err := r.pool.Query(ctx, `
|
|
SELECT id, name, description, created_at, updated_at
|
|
FROM material_finishes
|
|
ORDER BY name
|
|
`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var finishes []models.MaterialFinish
|
|
for rows.Next() {
|
|
var f models.MaterialFinish
|
|
if err := rows.Scan(
|
|
&f.ID, &f.Name, &f.Description,
|
|
&f.CreatedAt, &f.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
finishes = append(finishes, f)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if finishes == nil {
|
|
finishes = []models.MaterialFinish{}
|
|
}
|
|
return finishes, nil
|
|
}
|