chore: initial commit of HouseholdBudgetBook (backend, frontend, desktop)
This commit is contained in:
commit
4a5db3f1f9
167 files changed
+21732
No files matched your search
@@ -0,0 +1,205 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"household-book/model"
|
||||
)
|
||||
|
||||
type CategoryService struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewCategoryService(db *sql.DB) *CategoryService {
|
||||
return &CategoryService{db: db}
|
||||
}
|
||||
|
||||
func (s *CategoryService) List(ctx context.Context, typ string) ([]model.Category, error) {
|
||||
query := "SELECT id, name, type, parent_id, sort_order, COALESCE(keywords,''), is_active, created_at, updated_at FROM categories"
|
||||
var args []any
|
||||
|
||||
if typ != "" {
|
||||
query += " WHERE type = ?"
|
||||
args = append(args, typ)
|
||||
}
|
||||
query += " ORDER BY type, sort_order"
|
||||
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query categories: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var list []model.Category
|
||||
for rows.Next() {
|
||||
var c model.Category
|
||||
var isActive int
|
||||
if err := rows.Scan(&c.ID, &c.Name, &c.Type, &c.ParentID, &c.SortOrder, &c.Keywords, &isActive, &c.CreatedAt, &c.UpdatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan category: %w", err)
|
||||
}
|
||||
c.IsActive = isActive == 1
|
||||
list = append(list, c)
|
||||
}
|
||||
if list == nil {
|
||||
list = []model.Category{}
|
||||
}
|
||||
return list, rows.Err()
|
||||
}
|
||||
|
||||
func (s *CategoryService) GetByID(ctx context.Context, id int64) (*model.Category, error) {
|
||||
var c model.Category
|
||||
var isActive int
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
"SELECT id, name, type, parent_id, sort_order, COALESCE(keywords,''), is_active, created_at, updated_at FROM categories WHERE id = ?", id,
|
||||
).Scan(&c.ID, &c.Name, &c.Type, &c.ParentID, &c.SortOrder, &c.Keywords, &isActive, &c.CreatedAt, &c.UpdatedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query category: %w", err)
|
||||
}
|
||||
c.IsActive = isActive == 1
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (s *CategoryService) Create(ctx context.Context, req model.CreateCategoryRequest) (*model.Category, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
sortOrder := req.SortOrder
|
||||
if sortOrder <= 0 {
|
||||
sortOrder, err = s.nextSortOrder(ctx, tx, req.Type, req.ParentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
result, err := tx.ExecContext(ctx,
|
||||
"INSERT INTO categories (name, type, parent_id, sort_order, keywords) VALUES (?, ?, ?, ?, ?)",
|
||||
req.Name, req.Type, req.ParentID, sortOrder, req.Keywords)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("insert category: %w", err)
|
||||
}
|
||||
id, _ := result.LastInsertId()
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
|
||||
return s.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *CategoryService) nextSortOrder(ctx context.Context, tx *sql.Tx, typ string, parentID *int64) (int, error) {
|
||||
query := "SELECT COALESCE(MAX(sort_order), 0) + 1 FROM categories WHERE type = ?"
|
||||
args := []any{typ}
|
||||
if parentID == nil {
|
||||
query += " AND parent_id IS NULL"
|
||||
} else {
|
||||
query += " AND parent_id = ?"
|
||||
args = append(args, *parentID)
|
||||
}
|
||||
var next int
|
||||
if err := tx.QueryRowContext(ctx, query, args...).Scan(&next); err != nil {
|
||||
return 0, fmt.Errorf("query next category sort order: %w", err)
|
||||
}
|
||||
return next, nil
|
||||
}
|
||||
|
||||
func (s *CategoryService) Update(ctx context.Context, id int64, req model.UpdateCategoryRequest) (*model.Category, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
var sets []string
|
||||
var args []any
|
||||
|
||||
if req.Name != nil {
|
||||
sets = append(sets, "name = ?")
|
||||
args = append(args, *req.Name)
|
||||
}
|
||||
if req.ParentID.Set {
|
||||
sets = append(sets, "parent_id = ?")
|
||||
if req.ParentID.Valid {
|
||||
args = append(args, req.ParentID.Value)
|
||||
} else {
|
||||
args = append(args, nil)
|
||||
}
|
||||
}
|
||||
if req.SortOrder != nil {
|
||||
sets = append(sets, "sort_order = ?")
|
||||
args = append(args, *req.SortOrder)
|
||||
}
|
||||
if req.Keywords != nil {
|
||||
sets = append(sets, "keywords = ?")
|
||||
args = append(args, *req.Keywords)
|
||||
}
|
||||
if req.IsActive != nil {
|
||||
isActive := 0
|
||||
if *req.IsActive {
|
||||
isActive = 1
|
||||
}
|
||||
sets = append(sets, "is_active = ?")
|
||||
args = append(args, isActive)
|
||||
}
|
||||
|
||||
if len(sets) == 0 {
|
||||
return s.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
sets = append(sets, "updated_at = datetime('now')")
|
||||
args = append(args, id)
|
||||
|
||||
query := "UPDATE categories SET " + strings.Join(sets, ", ") + " WHERE id = ?"
|
||||
result, err := tx.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("update category: %w", err)
|
||||
}
|
||||
if affected, err := result.RowsAffected(); err != nil {
|
||||
return nil, fmt.Errorf("check updated category: %w", err)
|
||||
} else if affected == 0 {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
|
||||
return s.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *CategoryService) Delete(ctx context.Context, id int64) error {
|
||||
var count int
|
||||
if err := s.db.QueryRowContext(ctx,
|
||||
"SELECT COUNT(*) FROM transactions WHERE category_id = ?", id).Scan(&count); err != nil {
|
||||
return fmt.Errorf("check transactions: %w", err)
|
||||
}
|
||||
if count > 0 {
|
||||
return ErrHasTransactions
|
||||
}
|
||||
if err := s.db.QueryRowContext(ctx,
|
||||
"SELECT COUNT(*) FROM categories WHERE parent_id = ?", id).Scan(&count); err != nil {
|
||||
return fmt.Errorf("check children: %w", err)
|
||||
}
|
||||
if count > 0 {
|
||||
return ErrHasChildren
|
||||
}
|
||||
|
||||
result, err := s.db.ExecContext(ctx, "DELETE FROM categories WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected, err := result.RowsAffected(); err != nil {
|
||||
return fmt.Errorf("check deleted category: %w", err)
|
||||
} else if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user