feat: complete API-driven projects with infrastructure and business acceptance

This commit is contained in:
2026-09-23 17:33:50 +08:00
parent 04a1f7999d
commit 2c74dc0859
137 changed files with 11926 additions and 105 deletions

View File

@@ -0,0 +1,41 @@
package user
import (
"context"
"database/sql"
"errors"
"git.yuntuo.hk/k2safe/go-omni/database"
"time"
)
type UnitOfWork interface {
UserRepository
WithTx(context.Context, func(UserRepository, func(string, int64, string) error) error) error
}
type Store struct {
*MySQLUsers
db *database.DB
}
func NewStore(db *database.DB) *Store { return &Store{MySQLUsers: NewMySQLUsers(db.DB), db: db} }
func (s *Store) WithTx(ctx context.Context, work func(UserRepository, func(string, int64, string) error) error) error {
return s.db.InTx(ctx, nil, func(tx *sql.Tx) error {
audit := func(operation string, id int64, requestID string) error {
_, err := tx.ExecContext(ctx, "INSERT INTO user_audit (user_id,operation,request_id,created_at) VALUES (?,?,?,?)", id, operation, requestID, time.Now().UTC())
return err
}
return work(NewMySQLUsers(tx), audit)
})
}
// CheckSchema validates columns; it never mutates database schema.
func (s *Store) CheckSchema(ctx context.Context) error {
if err := s.MySQLUsers.CheckSchema(ctx); err != nil {
return err
}
rows, err := s.db.QueryContext(ctx, "SELECT id,user_id,operation,request_id,created_at FROM user_audit LIMIT 0")
if err != nil {
return err
}
return errors.Join(rows.Err(), rows.Close())
}