forked from mtusi2026/mtusi2026lab6
58 lines
2.3 KiB
Go
58 lines
2.3 KiB
Go
package repository
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"github.com/charawein/mtusi2026lab6/internal/domain"
|
||
)
|
||
|
||
var ErrNotFound = errNotFound{}
|
||
|
||
type errNotFound struct{}
|
||
|
||
func (errNotFound) Error() string { return "not found" }
|
||
|
||
var ErrConflict = errConflict{}
|
||
|
||
type errConflict struct{}
|
||
|
||
func (errConflict) Error() string { return "conflict" }
|
||
|
||
// UserRepository
|
||
type UserRepository interface {
|
||
Create(ctx context.Context, user *domain.User, passwordHash string) error
|
||
GetByID(ctx context.Context, id string) (*domain.User, error)
|
||
GetByUsername(ctx context.Context, username string) (*domain.User, error)
|
||
GetPasswordHash(ctx context.Context, userID string) (string, error)
|
||
}
|
||
|
||
// ChatRepository
|
||
type ChatRepository interface {
|
||
Create(ctx context.Context, chat *domain.Chat) error
|
||
GetByID(ctx context.Context, id string) (*domain.Chat, error)
|
||
IsMember(ctx context.Context, chatID, userID string) (bool, error)
|
||
// MemberVersion — версия истории чата (растёт с каждым сообщением) и членство.
|
||
MemberVersion(ctx context.Context, chatID, userID string) (version int64, member bool, err error)
|
||
ListByMember(ctx context.Context, userID string, cursor time.Time, cursorID string, limit int) ([]*domain.Chat, error)
|
||
}
|
||
|
||
// MessageRepository
|
||
type MessageRepository interface {
|
||
Create(ctx context.Context, msg *domain.Message) error
|
||
// CreateIdempotent атомарно создаёт сообщение и запись ключа идемпотентности.
|
||
// Если ключ уже есть с тем же requestHash — возвращает ранее созданное сообщение
|
||
// и created=false; с другим requestHash — ErrConflict.
|
||
CreateIdempotent(ctx context.Context, msg *domain.Message, key, requestHash string, ttl time.Duration) (stored *domain.Message, created bool, err error)
|
||
DeleteExpiredKeys(ctx context.Context, ttl time.Duration) (int64, error)
|
||
ListByChat(ctx context.Context, chatID string, cursor time.Time, cursorID string, limit int) ([]*domain.Message, error)
|
||
}
|
||
|
||
// SessionRepository
|
||
type SessionRepository interface {
|
||
Create(ctx context.Context, s *domain.Session) error
|
||
GetByToken(ctx context.Context, token string) (*domain.Session, error)
|
||
Delete(ctx context.Context, token string) error
|
||
DeleteExpired(ctx context.Context) error
|
||
}
|