43 lines
848 B
Go
43 lines
848 B
Go
|
package storage
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
||
|
"github.com/jmoiron/sqlx"
|
||
|
)
|
||
|
|
||
|
type UserStorage struct {
|
||
|
db *sqlx.DB
|
||
|
}
|
||
|
|
||
|
func NewUserStorage(db *sqlx.DB) *UserStorage {
|
||
|
return &UserStorage{
|
||
|
db: db,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (storage *UserStorage) CreateUser(ctx context.Context, user *models.User) error {
|
||
|
query := storage.db.Rebind("INSERT INTO users (user_id, role) VALUES (?, ?)")
|
||
|
_, err := storage.db.ExecContext(ctx, query, user.UserId, user.Role)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (storage *UserStorage) ReadUserById(ctx context.Context, userId int32) (*models.User, error) {
|
||
|
query := storage.db.Rebind(`
|
||
|
SELECT *
|
||
|
FROM users
|
||
|
WHERE user_id = ?
|
||
|
LIMIT 1;
|
||
|
`)
|
||
|
|
||
|
var user models.User
|
||
|
err := storage.db.GetContext(ctx, &user, query, userId)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &user, nil
|
||
|
}
|