Реализовать lab3 на оценку 4: балансировка и отказ API
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("resource not found")
|
||||
ErrInvalidMembers = errors.New("invalid chat members")
|
||||
)
|
||||
|
||||
func cloneChat(chat model.Chat) model.Chat {
|
||||
chat.MemberIDs = slices.Clone(chat.MemberIDs)
|
||||
return chat
|
||||
}
|
||||
|
||||
func (s *MemoryStore) CreateChat(
|
||||
actorID string,
|
||||
title string,
|
||||
memberIDs []string,
|
||||
) (model.Chat, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
seen := make(map[string]bool, len(memberIDs))
|
||||
|
||||
for _, id := range memberIDs {
|
||||
if seen[id] {
|
||||
return model.Chat{}, ErrInvalidMembers
|
||||
}
|
||||
|
||||
if _, exists := s.usersByID[id]; !exists {
|
||||
return model.Chat{}, ErrInvalidMembers
|
||||
}
|
||||
|
||||
seen[id] = true
|
||||
}
|
||||
|
||||
if !seen[actorID] {
|
||||
return model.Chat{}, ErrInvalidMembers
|
||||
}
|
||||
|
||||
id, err := newUUID()
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
for {
|
||||
if _, exists := s.chatsByID[id]; !exists {
|
||||
break
|
||||
}
|
||||
id, err = newUUID()
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
}
|
||||
|
||||
chat := model.Chat{
|
||||
ID: id,
|
||||
Title: title,
|
||||
MemberIDs: slices.Clone(memberIDs),
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
|
||||
s.chatsByID[id] = chat
|
||||
s.messagesByChat[id] = []model.Message{}
|
||||
|
||||
return cloneChat(chat), nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) GetChat(
|
||||
actorID string,
|
||||
chatID string,
|
||||
) (model.Chat, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
chat, exists := s.chatsByID[chatID]
|
||||
if !exists || !slices.Contains(chat.MemberIDs, actorID) {
|
||||
return model.Chat{}, ErrNotFound
|
||||
}
|
||||
|
||||
return cloneChat(chat), nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) ListChats(actorID string) []model.Chat {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
result := make([]model.Chat, 0)
|
||||
|
||||
for _, chat := range s.chatsByID {
|
||||
if slices.Contains(chat.MemberIDs, actorID) {
|
||||
result = append(result, cloneChat(chat))
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *MemoryStore) CreateMessage(
|
||||
actorID string,
|
||||
chatID string,
|
||||
text string,
|
||||
) (model.Message, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
chat, exists := s.chatsByID[chatID]
|
||||
if !exists || !slices.Contains(chat.MemberIDs, actorID) {
|
||||
return model.Message{}, ErrNotFound
|
||||
}
|
||||
|
||||
id, err := newUUID()
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
history := s.messagesByChat[chatID]
|
||||
|
||||
for slices.ContainsFunc(history, func(message model.Message) bool {
|
||||
return message.ID == id
|
||||
}) {
|
||||
id, err = newUUID()
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
}
|
||||
|
||||
createdAt := time.Now().UTC()
|
||||
|
||||
if len(history) > 0 {
|
||||
last := history[len(history)-1].CreatedAt
|
||||
if !createdAt.After(last) {
|
||||
createdAt = last.Add(time.Nanosecond)
|
||||
}
|
||||
}
|
||||
|
||||
message := model.Message{
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
SenderID: actorID,
|
||||
Text: text,
|
||||
CreatedAt: createdAt,
|
||||
}
|
||||
|
||||
s.messagesByChat[chatID] = append(history, message)
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) ListMessages(
|
||||
actorID string,
|
||||
chatID string,
|
||||
) ([]model.Message, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
chat, exists := s.chatsByID[chatID]
|
||||
if !exists || !slices.Contains(chat.MemberIDs, actorID) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
result := make([]model.Message, len(s.messagesByChat[chatID]))
|
||||
copy(result, s.messagesByChat[chatID])
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user