ms-tester/internal/storage/user.go
2024-08-20 16:18:23 +05:00

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
}