init
This commit is contained in:
72
internal/core/service/auth.go
Normal file
72
internal/core/service/auth.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/aykhans/oh-my-chat/internal/core/domain"
|
||||
"github.com/aykhans/oh-my-chat/internal/core/port"
|
||||
"github.com/aykhans/oh-my-chat/internal/core/utils"
|
||||
)
|
||||
|
||||
type AuthService struct {
|
||||
userRepository port.UserRepository
|
||||
tokenService port.TokenService
|
||||
}
|
||||
|
||||
// NewAuthService creates a new auth service instance
|
||||
func NewAuthService(userRepository port.UserRepository, tokenService port.TokenService) *AuthService {
|
||||
return &AuthService{
|
||||
userRepository,
|
||||
tokenService,
|
||||
}
|
||||
}
|
||||
|
||||
func (authService *AuthService) LoginByEmail(
|
||||
ctx context.Context,
|
||||
email, password string,
|
||||
) (string, error) {
|
||||
user, err := authService.userRepository.GetUserByEmail(ctx, email)
|
||||
if err != nil {
|
||||
if err == domain.ErrDataNotFound {
|
||||
return "", domain.ErrInvalidEmailCredentials
|
||||
}
|
||||
return "", domain.ErrInternal
|
||||
}
|
||||
|
||||
err = utils.ComparePassword(password, user.Password)
|
||||
if err != nil {
|
||||
return "", domain.ErrInvalidEmailCredentials
|
||||
}
|
||||
|
||||
accessToken, err := authService.tokenService.CreateToken(user)
|
||||
if err != nil {
|
||||
return "", domain.ErrTokenCreation
|
||||
}
|
||||
|
||||
return accessToken, nil
|
||||
}
|
||||
|
||||
func (authService *AuthService) LoginByUsername(
|
||||
ctx context.Context,
|
||||
username, password string,
|
||||
) (string, error) {
|
||||
user, err := authService.userRepository.GetUserByEmail(ctx, username)
|
||||
if err != nil {
|
||||
if err == domain.ErrDataNotFound {
|
||||
return "", domain.ErrInvalidUsernameCredentials
|
||||
}
|
||||
return "", domain.ErrInternal
|
||||
}
|
||||
|
||||
err = utils.ComparePassword(password, user.Password)
|
||||
if err != nil {
|
||||
return "", domain.ErrInvalidUsernameCredentials
|
||||
}
|
||||
|
||||
accessToken, err := authService.tokenService.CreateToken(user)
|
||||
if err != nil {
|
||||
return "", domain.ErrTokenCreation
|
||||
}
|
||||
|
||||
return accessToken, nil
|
||||
}
|
55
internal/core/service/message.go
Normal file
55
internal/core/service/message.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/aykhans/oh-my-chat/internal/core/domain"
|
||||
"github.com/aykhans/oh-my-chat/internal/core/port"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type MessageService struct {
|
||||
producer port.MessageProducer
|
||||
consumer port.MessageConsumer
|
||||
repo port.MessageRepository
|
||||
}
|
||||
|
||||
func NewMessageService(
|
||||
producerService port.MessageProducer,
|
||||
consumerService port.MessageConsumer,
|
||||
repo port.MessageRepository,
|
||||
) *MessageService {
|
||||
return &MessageService{
|
||||
producerService,
|
||||
consumerService,
|
||||
repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (chatServie *MessageService) SendMessage(
|
||||
ctx context.Context,
|
||||
message *domain.Message,
|
||||
) error {
|
||||
message.ChatID = "chat_" + message.ChatID
|
||||
return chatServie.producer.ProduceMessage(ctx, message)
|
||||
}
|
||||
|
||||
func (chatServie *MessageService) ReceiveMessage(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
message chan<- *domain.StreamMessage,
|
||||
) error {
|
||||
return chatServie.consumer.ConsumeMessage(
|
||||
ctx,
|
||||
userID.String(),
|
||||
func() []string { return []string{"chat_1", "chat_5", "chat_9"} },
|
||||
message,
|
||||
)
|
||||
}
|
||||
|
||||
func (chatServie *MessageService) CreateMessage(
|
||||
ctx context.Context,
|
||||
message *domain.Message,
|
||||
) (*domain.Message, error) {
|
||||
return chatServie.repo.CreateMessage(ctx, message)
|
||||
}
|
49
internal/core/service/user.go
Normal file
49
internal/core/service/user.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/aykhans/oh-my-chat/internal/core/domain"
|
||||
"github.com/aykhans/oh-my-chat/internal/core/port"
|
||||
"github.com/aykhans/oh-my-chat/internal/core/utils"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
repo port.UserRepository
|
||||
}
|
||||
|
||||
func NewUserService(repo port.UserRepository) *UserService {
|
||||
return &UserService{repo: repo}
|
||||
}
|
||||
|
||||
func (userService *UserService) Register(
|
||||
ctx context.Context,
|
||||
user *domain.User,
|
||||
) (*domain.User, error) {
|
||||
if exists, err := userService.repo.IsUsernameExists(ctx, user.Username); err != nil {
|
||||
return nil, domain.ErrInternal
|
||||
} else if exists {
|
||||
return nil, domain.ErrUsernameExists
|
||||
}
|
||||
if exists, err := userService.repo.IsEmailExists(ctx, user.Email); err != nil {
|
||||
return nil, domain.ErrInternal
|
||||
} else if exists {
|
||||
return nil, domain.ErrEmailExists
|
||||
}
|
||||
|
||||
hashedPassword, err := utils.HashPassword(user.Password)
|
||||
if err != nil {
|
||||
return nil, domain.ErrInternal
|
||||
}
|
||||
user.Password = hashedPassword
|
||||
|
||||
user, err = userService.repo.CreateUser(ctx, user)
|
||||
if err != nil {
|
||||
if err == domain.ErrConflictingData {
|
||||
return nil, err
|
||||
}
|
||||
return nil, domain.ErrInternal
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
Reference in New Issue
Block a user