package mysql import ( "context" "database/sql" "secunda-test/internal/domain" "secunda-test/internal/service" ) type UserRepository struct{ db *sql.DB } func NewUserRepository(db *sql.DB) *UserRepository { return &UserRepository{db: db} } func (r *UserRepository) Create(ctx context.Context, email, passwordHash, name string) (domain.User, error) { res, err := r.db.ExecContext(ctx, `INSERT INTO users(email,password_hash,name) VALUES(?,?,?)`, email, passwordHash, name) if err != nil { return domain.User{}, err } id, _ := res.LastInsertId() return r.FindByID(ctx, id) } func (r *UserRepository) FindByEmail(ctx context.Context, email string) (domain.User, error) { return r.scan(r.db.QueryRowContext(ctx, `SELECT id,email,password_hash,name,created_at FROM users WHERE email=?`, email)) } func (r *UserRepository) FindByID(ctx context.Context, id int64) (domain.User, error) { return r.scan(r.db.QueryRowContext(ctx, `SELECT id,email,password_hash,name,created_at FROM users WHERE id=?`, id)) } func (r *UserRepository) scan(row *sql.Row) (domain.User, error) { var u domain.User err := row.Scan(&u.ID, &u.Email, &u.PasswordHash, &u.Name, &u.CreatedAt) if err == sql.ErrNoRows { return domain.User{}, service.ErrNotFound } return u, err }