feat: complete API-driven projects with infrastructure and business acceptance
This commit is contained in:
41
examples/userService/internal/repository/user/store.go
Normal file
41
examples/userService/internal/repository/user/store.go
Normal 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())
|
||||
}
|
||||
Reference in New Issue
Block a user