feat: port backend implementation from lab1

This commit is contained in:
Голиков Maximillian Антон
2026-09-30 22:32:04 +03:00
parent 65003b6119
commit 380d0ef710
34 changed files with 1795 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
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}
}