ms-tester/internal/storage/user.go

43 lines
843 B
Go
Raw Normal View History

2024-08-20 11:18:23 +00:00
package storage
import (
"context"
2024-08-25 16:52:00 +00:00
"git.sch9.ru/new_gate/ms-tester/pkg/models"
2024-08-20 11:18:23 +00:00
"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
}