feat(domain):lab4 add attachments, status, errors

This commit is contained in:
Голиков Maximillian Антон
2026-10-19 10:25:52 +03:00
parent 4441156621
commit 200615f5ba
4 changed files with 61 additions and 8 deletions
+29 -5
View File
@@ -22,11 +22,12 @@ type Chat struct {
}
type Message struct {
ID string
ChatID string
SenderID string
Text string
CreatedAt time.Time
ID string
ChatID string
SenderID string
Text string
AttachmentIDs []string
CreatedAt time.Time
}
type Session struct {
@@ -35,3 +36,26 @@ type Session struct {
ExpiresAt time.Time
CreatedAt time.Time
}
type AttachmentStatus string
const (
AttachmentStatusPending AttachmentStatus = "pending"
AttachmentStatusReady AttachmentStatus = "ready"
AttachmentStatusFailed AttachmentStatus = "failed"
)
type Attachment struct {
ID string
ChatID string
OwnerID string
Filename string
ContentType string
SizeBytes int64
SHA256 []byte
ObjectKey string
Status AttachmentStatus
ErrorCode *string
CreatedAt time.Time
UpdatedAt time.Time
}
+20 -3
View File
@@ -15,6 +15,7 @@ const (
CodeUnsupportedMediaType ErrorCode = "unsupported_media_type"
CodeInternalError ErrorCode = "internal_error"
CodeUnavailable ErrorCode = "unavailable"
CodePayloadTooLarge ErrorCode = "payload_too_large"
)
var (
@@ -24,6 +25,7 @@ var (
ErrUnauthorized = errors.New("требуется аутентификация")
ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого")
ErrUnavailable = errors.New("сервис недоступен")
ErrPayloadTooLarge = errors.New("превышен допустимый размер")
)
type NotFoundError struct {
@@ -94,12 +96,27 @@ func (e *UnsupportedMediaTypeError) Error() string {
func (e *UnsupportedMediaTypeError) Unwrap() error { return ErrUnsupportedMediaType }
func (e *UnsupportedMediaTypeError) Code() ErrorCode { return CodeUnsupportedMediaType }
type UnavailableError struct {
Reason string // для логов; в JSON не попадает
}
type UnavailableError struct{ Reason string }
func (e *UnavailableError) Error() string {
return "сервис временно недоступен"
}
func (e *UnavailableError) Unwrap() error { return ErrUnavailable }
func (e *UnavailableError) Code() ErrorCode { return CodeUnavailable }
// PayloadTooLargeError - 413. Лимит кладём в структуру, чтобы его можно
// было переиспользовать в тексте ошибки, если Message не задан.
// Message опционален: если пуст — формируется из Limit.
type PayloadTooLargeError struct {
Limit int64
Message string
}
func (e *PayloadTooLargeError) Error() string {
if e.Message != "" {
return e.Message
}
return fmt.Sprintf("превышен лимит %d байт", e.Limit)
}
func (e *PayloadTooLargeError) Unwrap() error { return ErrPayloadTooLarge }
func (e *PayloadTooLargeError) Code() ErrorCode { return CodePayloadTooLarge }
+10
View File
@@ -35,3 +35,13 @@ type SessionRepository interface {
GetByTokenHash(ctx context.Context, tokenHash []byte) (*Session, error)
DeleteByTokenHash(ctx context.Context, tokenHash []byte) error
}
type AttachmentRepository interface {
Create(ctx context.Context, a *Attachment) error
GetByID(ctx context.Context, id string) (*Attachment, error)
MarkReady(ctx context.Context, id string) error
MarkFailed(ctx context.Context, id string, errorCode string) error
ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]Attachment, error)
ExistingKeys(ctx context.Context, keys []string) ([]string, error)
DeletePending(ctx context.Context, id string) error
}
+2
View File
@@ -76,6 +76,8 @@ func StatusFromError(err error) int {
return 415
case errors.Is(err, domain.ErrUnavailable):
return 503
case errors.Is(err, domain.ErrPayloadTooLarge):
return 413
default:
return 500
}