84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"smart-search-back/internal/repository"
|
|
"smart-search-back/pkg/crypto"
|
|
)
|
|
|
|
type userService struct {
|
|
userRepo repository.UserRepository
|
|
requestRepo repository.RequestRepository
|
|
cryptoHelper *crypto.Crypto
|
|
}
|
|
|
|
type UserInfo struct {
|
|
Email string
|
|
Name string
|
|
Phone string
|
|
CompanyName string
|
|
PaymentStatus string
|
|
}
|
|
|
|
type Statistics struct {
|
|
RequestsCount int
|
|
SuppliersCount int
|
|
CreatedTZ int
|
|
}
|
|
|
|
func NewUserService(userRepo repository.UserRepository, requestRepo repository.RequestRepository, cryptoSecret string) UserService {
|
|
return &userService{
|
|
userRepo: userRepo,
|
|
requestRepo: requestRepo,
|
|
cryptoHelper: crypto.NewCrypto(cryptoSecret),
|
|
}
|
|
}
|
|
|
|
func (s *userService) GetInfo(ctx context.Context, userID int) (*UserInfo, error) {
|
|
user, err := s.userRepo.FindByID(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
email, err := s.cryptoHelper.Decrypt(user.Email)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
phone, err := s.cryptoHelper.Decrypt(user.Phone)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userName, err := s.cryptoHelper.Decrypt(user.UserName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &UserInfo{
|
|
Email: email,
|
|
Name: userName,
|
|
Phone: phone,
|
|
CompanyName: user.CompanyName,
|
|
PaymentStatus: user.PaymentStatus,
|
|
}, nil
|
|
}
|
|
|
|
func (s *userService) GetBalance(ctx context.Context, userID int) (float64, error) {
|
|
return s.userRepo.GetBalance(ctx, userID)
|
|
}
|
|
|
|
func (s *userService) GetStatistics(ctx context.Context, userID int) (*Statistics, error) {
|
|
requestsCount, suppliersCount, createdTZ, err := s.requestRepo.GetUserStatistics(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Statistics{
|
|
RequestsCount: requestsCount,
|
|
SuppliersCount: suppliersCount,
|
|
CreatedTZ: createdTZ,
|
|
}, nil
|
|
}
|