Implement lab 3 level 4 failover and idempotency
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"highload/internal/database/sqlc"
|
||||
"highload/internal/model"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func NewUserRepository(queries *sqlc.Queries) *UserRepository {
|
||||
return &UserRepository{queries: queries}
|
||||
}
|
||||
|
||||
func (r *UserRepository) Create(ctx context.Context, username, displayName string, passwordHash string) (*model.User, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
sqlcUser, err := r.queries.CreateUser(dbCtx, sqlc.CreateUserParams{Username: username, DisplayName: displayName, PasswordHash: passwordHash})
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
user := &model.User{
|
||||
ID: sqlcUser.ID.String(),
|
||||
Username: sqlcUser.Username,
|
||||
DisplayName: sqlcUser.DisplayName,
|
||||
CreatedAt: sqlcUser.CreatedAt.Time,
|
||||
PasswordHash: sqlcUser.PasswordHash,
|
||||
}
|
||||
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetUserByID(ctx context.Context, id string) (*model.User, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
uuidID, err := getUUIDFromString(id)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
sqlcUser, err := r.queries.GetUserByID(dbCtx, *uuidID)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
user := &model.User{
|
||||
ID: sqlcUser.ID.String(),
|
||||
Username: sqlcUser.Username,
|
||||
DisplayName: sqlcUser.DisplayName,
|
||||
CreatedAt: sqlcUser.CreatedAt.Time,
|
||||
PasswordHash: sqlcUser.PasswordHash,
|
||||
}
|
||||
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetUserByUsername(ctx context.Context, username string) (*model.User, error) {
|
||||
dbCtx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
|
||||
sqlcUser, err := r.queries.GetUserByUsername(dbCtx, username)
|
||||
|
||||
if err != nil {
|
||||
return nil, convertDBError(err)
|
||||
}
|
||||
|
||||
user := &model.User{
|
||||
ID: sqlcUser.ID.String(),
|
||||
Username: sqlcUser.Username,
|
||||
DisplayName: sqlcUser.DisplayName,
|
||||
CreatedAt: sqlcUser.CreatedAt.Time,
|
||||
PasswordHash: sqlcUser.PasswordHash,
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user