Files
mtusi2026lab5/backend/internal/storage/_legacy/memory/user.go
T

60 lines
1.1 KiB
Go

package memory
import (
"context"
"high-loud-apps-labs/internal/domain"
)
func (r *UserRepo) Create(ctx context.Context, user *domain.User) error {
if err := ctx.Err(); err != nil {
return err
}
r.mu.Lock()
defer r.mu.Unlock()
for _, u := range r.users {
if u.Username == user.Username {
return &domain.AlreadyExistsError{
Entity: "user",
Field: "username",
Value: user.Username,
}
}
}
stored := *user
r.users[user.ID] = &stored
return nil
}
func (r *UserRepo) GetByID(ctx context.Context, id string) (*domain.User, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
r.mu.RLock()
defer r.mu.RUnlock()
u, ok := r.users[id]
if !ok {
return nil, &domain.NotFoundError{Entity: "user", ID: id}
}
copy := *u
return &copy, nil
}
func (r *UserRepo) GetByUsername(ctx context.Context, username string) (*domain.User, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
r.mu.RLock()
defer r.mu.RUnlock()
for _, u := range r.users {
if u.Username == username {
copy := *u
return &copy, nil
}
}
return nil, &domain.NotFoundError{Entity: "user", ID: username}
}