chat_back_go/internal/service/service.go

91 lines
2.8 KiB
Go

package service
import (
"context"
"encoding"
"github.com/golang-jwt/jwt/v5"
"github.com/redis/go-redis/v9"
"net/http"
"time"
"git.urec56.ru/urec/chat_back_go/config"
"git.urec56.ru/urec/chat_back_go/internal/domain"
"git.urec56.ru/urec/chat_back_go/internal/logger"
"git.urec56.ru/urec/chat_back_go/internal/repository"
)
//go:generate mockgen -source=service.go -destination=mocks/mock.go
type User interface {
Get(userID int, isFast bool) (domain.User, error)
GetVerificated(userID int) (domain.User, error)
GetAll(username string) ([]domain.User, error)
FindOne(username, email string) (domain.User, error)
Register(userData domain.UserRegister) (string, error)
}
type Auth interface {
ExtractAuthToken(r *http.Request) (string, error)
DecodeAuthToken(token string) (int, error)
EncodeAuthToken(userID int) (string, error)
HashPassword(p string) (string, error)
VerifyHashedPassword(p, hp string) bool
}
type Chat interface{}
type Messages interface {
AddAvatarImageAndUsernameToMessage(msgRaw domain.RawMessage) (domain.Message, error)
AddAvatarImageAndUsernameToMessages(msgsRaw []domain.RawMessage) []domain.Message
GetRaw(ID domain.UUID) (domain.RawMessage, error)
GetMessage(ID domain.UUID) (domain.Message, error)
GetPinned(chatID int) ([]domain.Message, error)
GetSome(chatID, offset, limit int) ([]domain.Message, error)
SendMessage(userID, chatID int, message domain.SendMessage) (domain.Message, error)
Delete(ID domain.UUID) error
Edit(ID domain.UUID, newMessage, newImageUrl string) error
Pin(chatID, userID int, messageID domain.UUID) (domain.Message, error)
Unpin(chatID int, messageID domain.UUID) error
}
type Cache interface {
Get(key string, v encoding.BinaryUnmarshaler, isFast bool) error
GetCtx(ctx context.Context, key string, v encoding.BinaryUnmarshaler, isFast bool) error
Set(key string, v encoding.BinaryMarshaler, ex time.Duration) error
SetCtx(ctx context.Context, key string, v encoding.BinaryMarshaler, ex time.Duration) error
}
type Parser interface {
Parse(tokenString string, keyFunc jwt.Keyfunc) (*jwt.Token, error)
ParseWithClaims(tokenString string, claims jwt.Claims, keyFunc jwt.Keyfunc) (*jwt.Token, error)
ParseUnverified(tokenString string, claims jwt.Claims) (token *jwt.Token, parts []string, err error)
DecodeSegment(seg string) ([]byte, error)
}
type Claims interface{ jwt.Claims }
type Service struct {
User
Auth
Chat
Messages
}
type Serv interface {
User
Auth
Chat
Messages
}
func NewService(repo repository.Repo, cfg config.Config, l *logger.Logger, rdb redis.UniversalClient) *Service {
c := newCacheService(rdb, l)
auth := newAuthService(cfg.JWT, l)
user := newUserService(repo, auth, l, c)
return &Service{
User: user,
Auth: auth,
Chat: newChatService(repo, l),
Messages: newMessageService(repo, user, l),
}
}