returned lab1
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Chat struct {
|
||||
ID string
|
||||
Title string
|
||||
MemberIDs []string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package domain
|
||||
|
||||
import "errors"
|
||||
|
||||
type ErrorCode string
|
||||
|
||||
const (
|
||||
ErrorInvalid ErrorCode = "invalid_request"
|
||||
ErrorUnauthorized ErrorCode = "unauthorized"
|
||||
ErrorNotFound ErrorCode = "not_found"
|
||||
ErrorConflict ErrorCode = "conflict"
|
||||
)
|
||||
|
||||
type ApplicationError struct {
|
||||
Code ErrorCode
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *ApplicationError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func NewApplicationError(code ErrorCode, message string) error {
|
||||
return &ApplicationError{Code: code, Message: message}
|
||||
}
|
||||
|
||||
func AsApplicationError(err error) (*ApplicationError, bool) {
|
||||
var applicationError *ApplicationError
|
||||
ok := errors.As(err, &applicationError)
|
||||
return applicationError, ok
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Message struct {
|
||||
ID string
|
||||
ChatID string
|
||||
SenderID string
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Position struct {
|
||||
CreatedAt time.Time
|
||||
ID string
|
||||
}
|
||||
|
||||
type Pagination struct {
|
||||
After *Position
|
||||
Limit int
|
||||
}
|
||||
|
||||
type ChatPage struct {
|
||||
Items []Chat
|
||||
HasMore bool
|
||||
}
|
||||
|
||||
type MessagePage struct {
|
||||
Items []Message
|
||||
HasMore bool
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type User struct {
|
||||
ID string
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
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
|
||||
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),
|
||||
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 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.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 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
|
||||
}
|
||||
message := domain.Message{
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user