new endpoints
This commit is contained in:
@@ -1,31 +1,12 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"Engine/internal/model"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ── Domain types ──────────────────────────────────────────────────────────────
|
||||
|
||||
type Department struct {
|
||||
ID int `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
CostCenter string `json:"cost_center"`
|
||||
Active bool `json:"active"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type GLAccount struct {
|
||||
ID int `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Description string `json:"description"`
|
||||
Type string `json:"type"` // revenue | cogs | opex | capex | headcount
|
||||
FavourHigh bool `json:"favour_high"` // true = over-budget is good (revenue accounts)
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
// ── ReferenceRepo ─────────────────────────────────────────────────────────────
|
||||
|
||||
type ReferenceRepo struct {
|
||||
@@ -37,15 +18,14 @@ func NewReferenceRepo(db *sql.DB) *ReferenceRepo {
|
||||
}
|
||||
|
||||
// ── Department operations ─────────────────────────────────────────────────────
|
||||
|
||||
func (r *ReferenceRepo) CreateDepartment(ctx context.Context, d Department) (*Department, error) {
|
||||
func (r *ReferenceRepo) CreateDepartment(ctx context.Context, d model.CreateDepartmentRequest) (*model.Department, error) {
|
||||
res, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO departments (code, name, cost_center, active)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(code) DO UPDATE
|
||||
SET name = excluded.name,
|
||||
cost_center = excluded.cost_center,
|
||||
active = excluded.active`,
|
||||
INSERT INTO departments (code, name, cost_center, active)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(code) DO UPDATE
|
||||
SET name = excluded.name,
|
||||
cost_center = excluded.cost_center,
|
||||
active = excluded.active`,
|
||||
d.Code, d.Name, d.CostCenter, boolToInt(d.Active),
|
||||
)
|
||||
if err != nil {
|
||||
@@ -55,14 +35,14 @@ func (r *ReferenceRepo) CreateDepartment(ctx context.Context, d Department) (*De
|
||||
id, err := res.LastInsertId()
|
||||
if err != nil || id == 0 {
|
||||
// ON CONFLICT branch — fetch existing row
|
||||
return r.getDepartmentByCode(ctx, d.Code)
|
||||
return r.GetDepartmentByCode(ctx, d.Code)
|
||||
}
|
||||
d.ID = int(id)
|
||||
return &d, nil
|
||||
|
||||
return r.GetDepartmentByCode(ctx, d.Code)
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) getDepartmentByCode(ctx context.Context, code string) (*Department, error) {
|
||||
var d Department
|
||||
func (r *ReferenceRepo) GetDepartmentByCode(ctx context.Context, code string) (*model.Department, error) {
|
||||
var d model.Department
|
||||
var active int
|
||||
err := r.db.QueryRowContext(ctx,
|
||||
`SELECT id, code, name, cost_center, active, created_at FROM departments WHERE code = ?`, code,
|
||||
@@ -74,7 +54,54 @@ func (r *ReferenceRepo) getDepartmentByCode(ctx context.Context, code string) (*
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) ListDepartments(ctx context.Context) ([]Department, error) {
|
||||
func (r *ReferenceRepo) GetDepartmentByCostCenter(ctx context.Context, cc string) (*model.Department, error) {
|
||||
var d model.Department
|
||||
var active int
|
||||
err := r.db.QueryRowContext(ctx,
|
||||
`SELECT id, code, name, cost_center, active, created_at FROM departments WHERE cost_center = ?`, cc,
|
||||
).Scan(&d.ID, &d.Code, &d.Name, &d.CostCenter, &active, &d.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetch department by code: %w", err)
|
||||
}
|
||||
d.Active = active == 1
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) GetDepartmentByName(ctx context.Context, name string) (*model.Department, error) {
|
||||
var d model.Department
|
||||
var active int
|
||||
err := r.db.QueryRowContext(ctx,
|
||||
`SELECT id, code, name, cost_center, active, created_at FROM departments WHERE name = ?`, name,
|
||||
).Scan(&d.ID, &d.Code, &d.Name, &d.CostCenter, &active, &d.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetch department by code: %w", err)
|
||||
}
|
||||
d.Active = active == 1
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) SetDepartmentActivityByCode(ctx context.Context, code string, active bool) error {
|
||||
_, err := r.db.ExecContext(ctx,
|
||||
`UPDATE departments SET active = ? WHERE code = ?`,
|
||||
boolToInt(active), code)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) SetDepartmentActivityByName(ctx context.Context, name string, active bool) error {
|
||||
_, err := r.db.ExecContext(ctx,
|
||||
`UPDATE departments SET active = ? WHERE name = ?`,
|
||||
boolToInt(active), name)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) SetDepartmentActivityByCostCenter(ctx context.Context, cc string, active bool) error {
|
||||
_, err := r.db.ExecContext(ctx,
|
||||
`UPDATE departments SET active = ? WHERE cost_center = ?`,
|
||||
boolToInt(active), cc)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) ListDepartments(ctx context.Context) ([]model.Department, error) {
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
`SELECT id, code, name, cost_center, active, created_at
|
||||
FROM departments ORDER BY code`)
|
||||
@@ -83,9 +110,9 @@ func (r *ReferenceRepo) ListDepartments(ctx context.Context) ([]Department, erro
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var depts []Department
|
||||
var depts []model.Department
|
||||
for rows.Next() {
|
||||
var d Department
|
||||
var d model.Department
|
||||
var active int
|
||||
if err := rows.Scan(&d.ID, &d.Code, &d.Name, &d.CostCenter, &active, &d.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
@@ -103,7 +130,7 @@ func (r *ReferenceRepo) DeleteDepartment(ctx context.Context, id int) error {
|
||||
|
||||
// ── GL Account operations ─────────────────────────────────────────────────────
|
||||
|
||||
func (r *ReferenceRepo) CreateGLAccount(ctx context.Context, a GLAccount) (*GLAccount, error) {
|
||||
func (r *ReferenceRepo) CreateGLAccount(ctx context.Context, a model.GLAccount) (*model.GLAccount, error) {
|
||||
res, err := r.db.ExecContext(ctx, `
|
||||
INSERT INTO gl_accounts (code, description, type, favour_high, active)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
@@ -126,8 +153,8 @@ func (r *ReferenceRepo) CreateGLAccount(ctx context.Context, a GLAccount) (*GLAc
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) getGLAccountByCode(ctx context.Context, code string) (*GLAccount, error) {
|
||||
var a GLAccount
|
||||
func (r *ReferenceRepo) getGLAccountByCode(ctx context.Context, code string) (*model.GLAccount, error) {
|
||||
var a model.GLAccount
|
||||
var favourHigh, active int
|
||||
err := r.db.QueryRowContext(ctx,
|
||||
`SELECT id, code, description, type, favour_high, active FROM gl_accounts WHERE code = ?`, code,
|
||||
@@ -140,7 +167,7 @@ func (r *ReferenceRepo) getGLAccountByCode(ctx context.Context, code string) (*G
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) ListGLAccounts(ctx context.Context) ([]GLAccount, error) {
|
||||
func (r *ReferenceRepo) ListGLAccounts(ctx context.Context) ([]model.GLAccount, error) {
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
`SELECT id, code, description, type, favour_high, active
|
||||
FROM gl_accounts ORDER BY code`)
|
||||
@@ -149,9 +176,9 @@ func (r *ReferenceRepo) ListGLAccounts(ctx context.Context) ([]GLAccount, error)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var accts []GLAccount
|
||||
var accts []model.GLAccount
|
||||
for rows.Next() {
|
||||
var a GLAccount
|
||||
var a model.GLAccount
|
||||
var favourHigh, active int
|
||||
if err := rows.Scan(&a.ID, &a.Code, &a.Description, &a.Type, &favourHigh, &active); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user