trepa/internal/accounts/repository.go
2025-02-25 20:55:12 +00:00

48 lines
786 B
Go

package accounts
import (
"database/sql"
"github.com/jmoiron/sqlx"
)
type Repository struct {
db *sqlx.DB
}
func NewRepository(db *sqlx.DB) Repository {
return Repository{db: db}
}
func (r *Repository) Insert(account *Account) (int, error) {
var id int
stmt, err := r.db.Prepare(SqlInsert)
if err != nil {
return id, err
}
defer stmt.Close()
err = stmt.QueryRow(account.Username, account.Password, account.RoleId).Scan(&id)
if err != nil {
return id, err
}
return id, nil
}
func (r *Repository) FetchOneByUsername(username string) (*Account, error) {
var account Account
err := r.db.Get(&account, SqlFetchOneByUsername, username)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
} else {
return nil, err
}
}
return &account, nil
}