feat: port backend implementation from lab1

This commit is contained in:
Голиков Maximillian Антон
2026-09-30 22:32:04 +03:00
parent 65003b6119
commit 380d0ef710
34 changed files with 1795 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
package domain
import "time"
type Health struct {
Status string
}
type User struct {
ID string
Username string
DisplayName string
CreatedAt time.Time
}
type Chat struct {
ID string
Title string
MemberIDs []string
CreatedAt time.Time
}
type Message struct {
ID string
ChatID string
SenderID string
Text string
CreatedAt time.Time
}
+82
View File
@@ -0,0 +1,82 @@
package domain
import (
"errors"
"fmt"
)
type ErrorCode string
const (
CodeInvalidRequest ErrorCode = "invalid_request"
CodeUnauthorized ErrorCode = "unauthorized"
CodeNotFound ErrorCode = "not_found"
CodeConflict ErrorCode = "conflict"
CodeUnsupportedMediaType ErrorCode = "unsupported_media_type"
CodeInternalError ErrorCode = "internal_error"
)
var (
ErrNotFound = errors.New("не найдено")
ErrAlreadyExists = errors.New("уже существует")
ErrInvalidInput = errors.New("некорректные данные")
ErrForbidden = errors.New("доступ запрещён")
ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого")
)
type NotFoundError struct {
Entity string
ID string
}
func (e *NotFoundError) Error() string {
return fmt.Sprintf("%s с id %q не найден", e.Entity, e.ID)
}
func (e *NotFoundError) Unwrap() error { return ErrNotFound }
func (e *NotFoundError) Code() ErrorCode { return CodeNotFound }
type AlreadyExistsError struct {
Entity string
Field string
Value string
}
func (e *AlreadyExistsError) Error() string {
return fmt.Sprintf("%s с %s %q уже существует", e.Entity, e.Field, e.Value)
}
func (e *AlreadyExistsError) Unwrap() error { return ErrAlreadyExists }
func (e *AlreadyExistsError) Code() ErrorCode { return CodeConflict }
type ValidationError struct {
Field string
Message string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("ошибка валидации поля %q: %s", e.Field, e.Message)
}
func (e *ValidationError) Unwrap() error { return ErrInvalidInput }
func (e *ValidationError) Code() ErrorCode { return CodeInvalidRequest }
type ForbiddenError struct {
Reason string
}
func (e *ForbiddenError) Error() string {
return fmt.Sprintf("доступ запрещён: %s", e.Reason)
}
func (e *ForbiddenError) Unwrap() error { return ErrForbidden }
func (e *ForbiddenError) Code() ErrorCode { return CodeUnauthorized }
type UnsupportedMediaTypeError struct {
MediaType string
}
func (e *UnsupportedMediaTypeError) Error() string {
if e.MediaType == "" {
return "требуется Content-Type: application/json"
}
return fmt.Sprintf("неподдерживаемый Content-Type %q; требуется application/json", e.MediaType)
}
func (e *UnsupportedMediaTypeError) Unwrap() error { return ErrUnsupportedMediaType }
func (e *UnsupportedMediaTypeError) Code() ErrorCode { return CodeUnsupportedMediaType }
+20
View File
@@ -0,0 +1,20 @@
package domain
import "context"
type UserRepository interface {
Create(ctx context.Context, user *User) error
GetByID(ctx context.Context, id string) (*User, error)
GetByUsername(ctx context.Context, username string) (*User, error)
}
type ChatRepository interface {
Create(ctx context.Context, chat *Chat) error
GetByID(ctx context.Context, id string) (*Chat, error)
ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]Chat, *string, error)
}
type MessageRepository interface {
Create(ctx context.Context, message *Message) error
ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]Message, *string, error)
}