forked from mtusi2026/mtusi2026lab6
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/validate"
|
|
)
|
|
|
|
var (
|
|
_ ChatsService = (*Chats)(nil)
|
|
)
|
|
|
|
type Chats struct {
|
|
store store.ChatStore
|
|
}
|
|
|
|
func NewChats(s store.ChatStore) *Chats {
|
|
return &Chats{store: s}
|
|
}
|
|
|
|
func (c *Chats) Create(ctx context.Context, callerID, title string, memberIDs []string) (domain.Chat, error) {
|
|
if err := validate.ChatTitle(title); err != nil {
|
|
return domain.Chat{}, err
|
|
}
|
|
memberIDs, err := validate.MemberIDs(memberIDs, callerID)
|
|
if err != nil {
|
|
return domain.Chat{}, err
|
|
}
|
|
return c.store.CreateChat(ctx, callerID, title, memberIDs)
|
|
}
|
|
|
|
func (c *Chats) Get(ctx context.Context, callerID, chatID string) (domain.Chat, error) {
|
|
chatID, err := validate.CanonicalUUID("chat_id", chatID)
|
|
if err != nil {
|
|
return domain.Chat{}, err
|
|
}
|
|
return c.store.ChatByID(ctx, callerID, chatID)
|
|
}
|
|
|
|
func (c *Chats) List(ctx context.Context, callerID, cursor string, limit int) (domain.Page[domain.Chat], error) {
|
|
if err := validate.Limit(limit); err != nil {
|
|
return domain.Page[domain.Chat]{}, err
|
|
}
|
|
return c.store.ListChats(ctx, callerID, cursor, limit)
|
|
}
|