import lab3 files

This commit is contained in:
Голиков Maximillian Антон
2026-10-18 21:14:01 +03:00
parent ec540a1a72
commit 76322040e5
64 changed files with 4446 additions and 0 deletions
@@ -0,0 +1,102 @@
package memory
import (
"context"
"slices"
"high-loud-apps-labs/internal/domain"
)
func (r *ChatRepo) Create(ctx context.Context, chat *domain.Chat) error {
if err := ctx.Err(); err != nil {
return err
}
r.mu.Lock()
defer r.mu.Unlock()
stored := *chat
stored.MemberIDs = append([]string(nil), chat.MemberIDs...)
r.chats[chat.ID] = &stored
return nil
}
func (r *ChatRepo) GetByID(ctx context.Context, id string) (*domain.Chat, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
r.mu.RLock()
defer r.mu.RUnlock()
c, ok := r.chats[id]
if !ok {
return nil, &domain.NotFoundError{Entity: "chat", ID: id}
}
copy := *c
copy.MemberIDs = append([]string(nil), c.MemberIDs...)
return &copy, nil
}
func (r *ChatRepo) ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) {
if err := ctx.Err(); err != nil {
return nil, nil, err
}
r.mu.RLock()
defer r.mu.RUnlock()
scope := "chats:" + userID
all := make([]domain.Chat, 0)
for _, c := range r.chats {
for _, m := range c.MemberIDs {
if m == userID {
copy := *c
copy.MemberIDs = append([]string(nil), c.MemberIDs...)
all = append(all, copy)
break
}
}
}
slices.SortFunc(all, func(a, b domain.Chat) int {
if lessThan(a.CreatedAt, a.ID, b.CreatedAt, b.ID) {
return -1
}
if lessThan(b.CreatedAt, b.ID, a.CreatedAt, a.ID) {
return 1
}
return 0
})
start := 0
if cursor != nil {
t, id, err := decodeCursor(*cursor, scope)
if err != nil {
return nil, nil, err
}
start = len(all)
for i, c := range all {
if lessThan(t, id, c.CreatedAt, c.ID) {
start = i
break
}
}
}
if start >= len(all) {
return []domain.Chat{}, nil, nil
}
end := start + limit
if end > len(all) {
end = len(all)
}
page := all[start:end]
var next *string
if end < len(all) {
last := page[len(page)-1]
c := encodeCursor(scope, last.CreatedAt, last.ID)
next = &c
}
return page, next, nil
}
@@ -0,0 +1,47 @@
package memory
import (
"encoding/base64"
"strings"
"time"
"high-loud-apps-labs/internal/domain"
)
func encodeCursor(scope string, createdAt time.Time, id string) string {
raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id
return base64.RawURLEncoding.EncodeToString([]byte(raw))
}
func decodeCursor(cursor, expectedScope string) (time.Time, string, error) {
data, err := base64.RawURLEncoding.DecodeString(cursor)
if err != nil {
return time.Time{}, "", badCursor()
}
parts := strings.SplitN(string(data), "|", 3)
if len(parts) != 3 {
return time.Time{}, "", badCursor()
}
if parts[0] != expectedScope {
return time.Time{}, "", badCursor()
}
t, err := time.Parse(time.RFC3339Nano, parts[1])
if err != nil {
return time.Time{}, "", badCursor()
}
return t, parts[2], nil
}
func badCursor() error {
return &domain.ValidationError{
Field: "cursor",
Message: "некорректный формат",
}
}
func lessThan(aTime time.Time, aID string, bTime time.Time, bID string) bool {
if aTime.Equal(bTime) {
return aID < bID
}
return aTime.Before(bTime)
}
@@ -0,0 +1,93 @@
package memory
import (
"context"
"slices"
"time"
"high-loud-apps-labs/internal/domain"
)
func (r *MessageRepo) Create(ctx context.Context, message *domain.Message) error {
if err := ctx.Err(); err != nil {
return err
}
r.mu.Lock()
defer r.mu.Unlock()
stored := *message
r.messages[message.ChatID] = append(r.messages[message.ChatID], stored)
return nil
}
func (r *MessageRepo) ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) {
if err := ctx.Err(); err != nil {
return nil, nil, err
}
r.mu.RLock()
defer r.mu.RUnlock()
scope := "messages:" + chatID
var (
afterTime time.Time
afterID string
hasCursor bool
)
if cursor != nil {
t, id, err := decodeCursor(*cursor, scope)
if err != nil {
return nil, nil, err
}
afterTime = t
afterID = id
hasCursor = true
}
src := r.messages[chatID]
if len(src) == 0 {
return []domain.Message{}, nil, nil
}
all := make([]domain.Message, len(src))
copy(all, src)
slices.SortFunc(all, func(a, b domain.Message) int {
if lessThan(a.CreatedAt, a.ID, b.CreatedAt, b.ID) {
return -1
}
if lessThan(b.CreatedAt, b.ID, a.CreatedAt, a.ID) {
return 1
}
return 0
})
start := 0
if hasCursor {
start = len(all)
for i, m := range all {
if lessThan(afterTime, afterID, m.CreatedAt, m.ID) {
start = i
break
}
}
}
if start >= len(all) {
return []domain.Message{}, nil, nil
}
end := start + limit
if end > len(all) {
end = len(all)
}
page := all[start:end]
var next *string
if end < len(all) {
last := page[len(page)-1]
c := encodeCursor(scope, last.CreatedAt, last.ID)
next = &c
}
return page, next, nil
}
@@ -0,0 +1,34 @@
package memory
import (
"sync"
"high-loud-apps-labs/internal/domain"
)
type Storage struct {
mu sync.RWMutex
users map[string]*domain.User
chats map[string]*domain.Chat
messages map[string][]domain.Message
}
func New() *Storage {
return &Storage{
users: make(map[string]*domain.User),
chats: make(map[string]*domain.Chat),
messages: make(map[string][]domain.Message),
}
}
func (s *Storage) Users() *UserRepo { return &UserRepo{s} }
func (s *Storage) Chats() *ChatRepo { return &ChatRepo{s} }
func (s *Storage) Messages() *MessageRepo { return &MessageRepo{s} }
type UserRepo struct{ *Storage }
type ChatRepo struct{ *Storage }
type MessageRepo struct{ *Storage }
var _ domain.UserRepository = (*UserRepo)(nil)
var _ domain.ChatRepository = (*ChatRepo)(nil)
var _ domain.MessageRepository = (*MessageRepo)(nil)
@@ -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}
}