lab1
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Chat struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
MemberIDs []uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (c Chat) HasMember(userID uuid.UUID) bool {
|
||||
return slices.Contains(c.MemberIDs, userID)
|
||||
}
|
||||
|
||||
type ChatDraft struct {
|
||||
Title string
|
||||
MemberIDs []uuid.UUID
|
||||
CallerID uuid.UUID
|
||||
}
|
||||
|
||||
func (d ChatDraft) Validate() error {
|
||||
if err := validateNotBlank("title", d.Title); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !slices.Contains(d.MemberIDs, d.CallerID) {
|
||||
return fmt.Errorf("member_ids must contain the caller: %w", ErrValidation)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Cursor struct {
|
||||
Scope uuid.UUID
|
||||
CreatedAt time.Time
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (c Cursor) BelongsTo(scope uuid.UUID) bool {
|
||||
return c.Scope == scope
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package domain
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrUsernameTaken = errors.New("username already taken")
|
||||
ErrUnknownMember = errors.New("member does not exist")
|
||||
ErrValidation = errors.New("validation failed")
|
||||
ErrInvalidCursor = errors.New("invalid cursor")
|
||||
)
|
||||
@@ -0,0 +1,25 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type MessageDraft struct {
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
}
|
||||
|
||||
func (d MessageDraft) Validate() error {
|
||||
return validateNotBlank("text", d.Text)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package domain
|
||||
|
||||
const DefaultPageLimit = 50
|
||||
|
||||
type Page[T any] struct {
|
||||
Items []T
|
||||
Next *Cursor
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func validateNotBlank(field, value string) error {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fmt.Errorf("%s consists of whitespace only: %w", field, ErrValidation)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type UserDraft struct {
|
||||
Username string
|
||||
DisplayName string
|
||||
}
|
||||
|
||||
func (d UserDraft) Validate() error {
|
||||
return validateNotBlank("display_name", d.DisplayName)
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package domain_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
)
|
||||
|
||||
// Длины, regex и uniqueItems проверяет сгенерированный слой по contracts/openapi.json.
|
||||
// Домен закрывает только то, что схема выразить не может.
|
||||
|
||||
func TestUserDraftRejectsBlankDisplayName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, blank := range []string{" ", " ", "\t", "\n", "\u00a0"} {
|
||||
draft := domain.UserDraft{Username: "alice", DisplayName: blank}
|
||||
require.ErrorIs(t, draft.Validate(), domain.ErrValidation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserDraftAcceptsVisibleName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
draft := domain.UserDraft{Username: "alice", DisplayName: "Алиса"}
|
||||
assert.NoError(t, draft.Validate())
|
||||
}
|
||||
|
||||
func TestChatDraftRequiresCallerAmongMembers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
caller := uuid.New()
|
||||
|
||||
outside := domain.ChatDraft{
|
||||
Title: "Проект",
|
||||
MemberIDs: []uuid.UUID{uuid.New(), uuid.New()},
|
||||
CallerID: caller,
|
||||
}
|
||||
require.ErrorIs(t, outside.Validate(), domain.ErrValidation)
|
||||
|
||||
inside := domain.ChatDraft{
|
||||
Title: "Проект",
|
||||
MemberIDs: []uuid.UUID{uuid.New(), caller},
|
||||
CallerID: caller,
|
||||
}
|
||||
assert.NoError(t, inside.Validate())
|
||||
}
|
||||
|
||||
func TestChatDraftRejectsBlankTitle(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
caller := uuid.New()
|
||||
|
||||
draft := domain.ChatDraft{
|
||||
Title: " ",
|
||||
MemberIDs: []uuid.UUID{caller},
|
||||
CallerID: caller,
|
||||
}
|
||||
require.ErrorIs(t, draft.Validate(), domain.ErrValidation)
|
||||
}
|
||||
|
||||
func TestMessageDraftRejectsBlankText(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
draft := domain.MessageDraft{
|
||||
ChatID: uuid.New(),
|
||||
SenderID: uuid.New(),
|
||||
Text: strings.Repeat(" ", 10),
|
||||
}
|
||||
require.ErrorIs(t, draft.Validate(), domain.ErrValidation)
|
||||
}
|
||||
|
||||
func TestMessageDraftAcceptsTextWithSurroundingSpaces(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
draft := domain.MessageDraft{
|
||||
ChatID: uuid.New(),
|
||||
SenderID: uuid.New(),
|
||||
Text: " привет ",
|
||||
}
|
||||
assert.NoError(t, draft.Validate(), "контракт запрещает только полностью пробельный текст")
|
||||
}
|
||||
Reference in New Issue
Block a user