forked from mtusi2026/mtusi2026lab2
Начало реализации сервиса.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package inmemory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
)
|
||||
|
||||
const UserCacheName = "USERS_CACHE"
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
type UserCache interface {
|
||||
Create(ctx context.Context, input dto.CreateUser) (dto.User, error)
|
||||
GetByID(ctx context.Context, id uuid.UUID) (dto.User, error)
|
||||
GetByUsername(ctx context.Context, username string) (dto.User, error)
|
||||
}
|
||||
|
||||
type userCache struct {
|
||||
mu sync.RWMutex
|
||||
store *CopyAndWriteCache[uuid.UUID, dto.User]
|
||||
byUsername map[string]uuid.UUID
|
||||
}
|
||||
|
||||
func NewUserCache() UserCache {
|
||||
return &userCache{
|
||||
store: NewCopyAndWriteCache[uuid.UUID, dto.User](UserCacheName),
|
||||
byUsername: make(map[string]uuid.UUID),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *userCache) Create(ctx context.Context, input dto.CreateUser) (dto.User, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return dto.User{}, err
|
||||
}
|
||||
if !usernamePattern.MatchString(input.Username) ||
|
||||
strings.TrimSpace(input.DisplayName) == "" ||
|
||||
utf8.RuneCountInString(input.DisplayName) > 100 {
|
||||
return dto.User{}, apperr.New(apperr.CodeInvalidRequest, "invalid user fields")
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if _, exists := c.byUsername[input.Username]; exists {
|
||||
return dto.User{}, apperr.New(apperr.CodeConflict, "username already exists")
|
||||
}
|
||||
|
||||
id, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return dto.User{}, apperr.Wrap(err, apperr.CodeInternalError, "generate user ID")
|
||||
}
|
||||
user := dto.User{
|
||||
ID: id,
|
||||
Username: input.Username,
|
||||
DisplayName: input.DisplayName,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
if !c.store.Store(user.ID, user) {
|
||||
return dto.User{}, apperr.New(apperr.CodeInternalError, "store user")
|
||||
}
|
||||
c.byUsername[user.Username] = user.ID
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (c *userCache) GetByID(ctx context.Context, id uuid.UUID) (dto.User, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return dto.User{}, err
|
||||
}
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
user, ok := c.store.Get(id)
|
||||
if !ok {
|
||||
return dto.User{}, apperr.New(apperr.CodeNotFound, "user not found")
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (c *userCache) GetByUsername(ctx context.Context, username string) (dto.User, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return dto.User{}, err
|
||||
}
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
id, ok := c.byUsername[username]
|
||||
if !ok {
|
||||
return dto.User{}, apperr.New(apperr.CodeNotFound, "user not found")
|
||||
}
|
||||
user, ok := c.store.Get(id)
|
||||
if !ok {
|
||||
return dto.User{}, apperr.New(apperr.CodeInternalError, "user index is inconsistent")
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user