52 lines
947 B
Go
52 lines
947 B
Go
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 len(d.MemberIDs) == 0 {
|
|
return fmt.Errorf("member_ids must not be empty: %w", ErrValidation)
|
|
}
|
|
|
|
seen := make(map[uuid.UUID]struct{}, len(d.MemberIDs))
|
|
for _, id := range d.MemberIDs {
|
|
if _, ok := seen[id]; ok {
|
|
return fmt.Errorf("member_ids contains duplicates: %w", ErrValidation)
|
|
}
|
|
|
|
seen[id] = struct{}{}
|
|
}
|
|
|
|
if _, ok := seen[d.CallerID]; !ok {
|
|
return fmt.Errorf("member_ids must contain the caller: %w", ErrValidation)
|
|
}
|
|
|
|
return nil
|
|
}
|