lab 3 done
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
chatusecase "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/usecase/chat"
|
||||
)
|
||||
|
||||
// Memory is the lab1 in-memory Repository implementation.
|
||||
type Memory struct {
|
||||
mu sync.RWMutex
|
||||
users map[string]domain.User
|
||||
hashes map[string]string
|
||||
userByUsername map[string]string
|
||||
chats map[string]domain.Chat
|
||||
messages map[string][]domain.Message
|
||||
}
|
||||
|
||||
var _ chatusecase.Repository = (*Memory)(nil)
|
||||
|
||||
func NewMemory() *Memory {
|
||||
return &Memory{
|
||||
users: make(map[string]domain.User),
|
||||
hashes: make(map[string]string),
|
||||
userByUsername: make(map[string]string),
|
||||
chats: make(map[string]domain.Chat),
|
||||
messages: make(map[string][]domain.Message),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Memory) CreateUser(_ context.Context, username, displayName, hash string) (domain.User, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if _, exists := m.userByUsername[username]; exists {
|
||||
return domain.User{}, chatusecase.ErrUsernameTaken
|
||||
}
|
||||
user := domain.User{
|
||||
ID: uuid.NewString(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: now(),
|
||||
}
|
||||
m.users[user.ID] = user
|
||||
m.hashes[user.ID] = hash
|
||||
m.userByUsername[user.Username] = user.ID
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (m *Memory) User(_ context.Context, id string) (domain.User, bool, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
user, ok := m.users[id]
|
||||
return user, ok, nil
|
||||
}
|
||||
|
||||
func (m *Memory) CreateChat(_ context.Context, title string, memberIDs []string) (domain.Chat, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
for _, memberID := range memberIDs {
|
||||
if _, exists := m.users[memberID]; !exists {
|
||||
return domain.Chat{}, chatusecase.ErrUserNotFound
|
||||
}
|
||||
}
|
||||
chat := domain.Chat{
|
||||
ID: uuid.NewString(),
|
||||
Title: title,
|
||||
MemberIDs: append([]string(nil), memberIDs...),
|
||||
CreatedAt: now(),
|
||||
}
|
||||
m.chats[chat.ID] = chat
|
||||
return cloneChat(chat), nil
|
||||
}
|
||||
|
||||
func (m *Memory) ChatForUser(_ context.Context, chatID, userID string) (domain.Chat, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
chat, exists := m.chats[chatID]
|
||||
if !exists {
|
||||
return domain.Chat{}, chatusecase.ErrChatNotFound
|
||||
}
|
||||
if !contains(chat.MemberIDs, userID) {
|
||||
return domain.Chat{}, chatusecase.ErrNotMember
|
||||
}
|
||||
return cloneChat(chat), nil
|
||||
}
|
||||
|
||||
func (m *Memory) ListChats(_ context.Context, userID string, after *domain.Position, limit int) ([]domain.Chat, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
result := make([]domain.Chat, 0)
|
||||
for _, chat := range m.chats {
|
||||
if contains(chat.MemberIDs, userID) && isAfter(chat.CreatedAt, chat.ID, after) {
|
||||
result = append(result, cloneChat(chat))
|
||||
}
|
||||
}
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
return less(result[i].CreatedAt, result[i].ID, result[j].CreatedAt, result[j].ID)
|
||||
})
|
||||
if len(result) > limit {
|
||||
result = result[:limit]
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (m *Memory) CreateMessage(_ context.Context, chatID, senderID, text, key string) (domain.Message, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
chat, exists := m.chats[chatID]
|
||||
if !exists {
|
||||
return domain.Message{}, chatusecase.ErrChatNotFound
|
||||
}
|
||||
if !contains(chat.MemberIDs, senderID) {
|
||||
return domain.Message{}, chatusecase.ErrNotMember
|
||||
}
|
||||
if key != "" {
|
||||
for _, previous := range m.messages[chatID] {
|
||||
if previous.SenderID == senderID && previous.IdempotencyKey == key {
|
||||
if previous.Text != text {
|
||||
return domain.Message{}, domain.NewApplicationError(domain.ErrorConflict, "Idempotency-Key уже использован с другим телом")
|
||||
}
|
||||
return previous, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
message := domain.Message{
|
||||
IdempotencyKey: key,
|
||||
ID: uuid.NewString(),
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: now(),
|
||||
}
|
||||
m.messages[chatID] = append(m.messages[chatID], message)
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (m *Memory) ListMessages(_ context.Context, chatID string, after *domain.Position, limit int) ([]domain.Message, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
values := m.messages[chatID]
|
||||
result := make([]domain.Message, 0, min(len(values), limit))
|
||||
for _, message := range values {
|
||||
if isAfter(message.CreatedAt, message.ID, after) {
|
||||
result = append(result, message)
|
||||
}
|
||||
}
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
return less(result[i].CreatedAt, result[i].ID, result[j].CreatedAt, result[j].ID)
|
||||
})
|
||||
if len(result) > limit {
|
||||
result = result[:limit]
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func cloneChat(chat domain.Chat) domain.Chat {
|
||||
chat.MemberIDs = append([]string(nil), chat.MemberIDs...)
|
||||
return chat
|
||||
}
|
||||
|
||||
func now() time.Time {
|
||||
return time.Now().UTC().Truncate(time.Second)
|
||||
}
|
||||
|
||||
func contains(values []string, target string) bool {
|
||||
for _, value := range values {
|
||||
if value == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isAfter(createdAt time.Time, id string, after *domain.Position) bool {
|
||||
return after == nil || createdAt.After(after.CreatedAt) || createdAt.Equal(after.CreatedAt) && id > after.ID
|
||||
}
|
||||
|
||||
func less(leftTime time.Time, leftID string, rightTime time.Time, rightID string) bool {
|
||||
if leftTime.Equal(rightTime) {
|
||||
return leftID < rightID
|
||||
}
|
||||
return leftTime.Before(rightTime)
|
||||
}
|
||||
|
||||
func (m *Memory) Ready(context.Context) error { return nil }
|
||||
func (m *Memory) UserByUsername(_ context.Context, username string) (domain.User, string, bool, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
id, ok := m.userByUsername[username]
|
||||
return m.users[id], m.hashes[id], ok, nil
|
||||
}
|
||||
Reference in New Issue
Block a user