108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package registry
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"reanimator/internal/domain"
|
|
)
|
|
|
|
type AssetRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewAssetRepository(db *sql.DB) *AssetRepository {
|
|
return &AssetRepository{db: db}
|
|
}
|
|
|
|
func (r *AssetRepository) Create(ctx context.Context, asset domain.Asset) (domain.Asset, error) {
|
|
var locationID interface{}
|
|
if asset.LocationID != nil {
|
|
locationID = *asset.LocationID
|
|
}
|
|
|
|
result, err := r.db.ExecContext(ctx,
|
|
`INSERT INTO assets (project_id, location_id, name, vendor, model, vendor_serial, asset_tag)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
asset.ProjectID,
|
|
locationID,
|
|
asset.Name,
|
|
asset.Vendor,
|
|
asset.Model,
|
|
asset.VendorSerial,
|
|
asset.AssetTag,
|
|
)
|
|
if err != nil {
|
|
return domain.Asset{}, classifyError(err)
|
|
}
|
|
|
|
id, err := result.LastInsertId()
|
|
if err != nil {
|
|
return domain.Asset{}, err
|
|
}
|
|
|
|
return r.Get(ctx, id)
|
|
}
|
|
|
|
func (r *AssetRepository) Get(ctx context.Context, id int64) (domain.Asset, error) {
|
|
var asset domain.Asset
|
|
var locationID sql.NullInt64
|
|
var vendor sql.NullString
|
|
var model sql.NullString
|
|
var assetTag sql.NullString
|
|
|
|
row := r.db.QueryRowContext(ctx,
|
|
`SELECT id, project_id, location_id, name, vendor, model, vendor_serial, asset_tag, created_at, updated_at
|
|
FROM assets WHERE id = ?`,
|
|
id,
|
|
)
|
|
if err := row.Scan(&asset.ID, &asset.ProjectID, &locationID, &asset.Name, &vendor, &model, &asset.VendorSerial, &assetTag, &asset.CreatedAt, &asset.UpdatedAt); err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return domain.Asset{}, ErrNotFound
|
|
}
|
|
return domain.Asset{}, err
|
|
}
|
|
|
|
asset.LocationID = nullInt64ToPtr(locationID)
|
|
asset.Vendor = nullStringToPtr(vendor)
|
|
asset.Model = nullStringToPtr(model)
|
|
asset.AssetTag = nullStringToPtr(assetTag)
|
|
|
|
return asset, nil
|
|
}
|
|
|
|
func (r *AssetRepository) List(ctx context.Context) ([]domain.Asset, error) {
|
|
rows, err := r.db.QueryContext(ctx,
|
|
`SELECT id, project_id, location_id, name, vendor, model, vendor_serial, asset_tag, created_at, updated_at
|
|
FROM assets ORDER BY id`,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
assets := make([]domain.Asset, 0)
|
|
for rows.Next() {
|
|
var asset domain.Asset
|
|
var locationID sql.NullInt64
|
|
var vendor sql.NullString
|
|
var model sql.NullString
|
|
var assetTag sql.NullString
|
|
|
|
if err := rows.Scan(&asset.ID, &asset.ProjectID, &locationID, &asset.Name, &vendor, &model, &asset.VendorSerial, &assetTag, &asset.CreatedAt, &asset.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
asset.LocationID = nullInt64ToPtr(locationID)
|
|
asset.Vendor = nullStringToPtr(vendor)
|
|
asset.Model = nullStringToPtr(model)
|
|
asset.AssetTag = nullStringToPtr(assetTag)
|
|
assets = append(assets, asset)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return assets, nil
|
|
}
|