|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/google/uuid" |
| 6 | + "github.com/multi-tenants-cms-golang/cms-sys/internal/repository" |
| 7 | + "github.com/multi-tenants-cms-golang/cms-sys/internal/types" |
| 8 | + "github.com/multi-tenants-cms-golang/cms-sys/pkg/utils" |
| 9 | + "github.com/sirupsen/logrus" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type OwnerService interface { |
| 14 | + Create(req types.OwnerCreateRequest) (*types.OwnerResponse, error) |
| 15 | + Update(id string, req types.OwnerUpdateRequest) (*types.OwnerResponse, error) |
| 16 | +} |
| 17 | + |
| 18 | +type OwnerServiceImpl struct { |
| 19 | + log *logrus.Logger |
| 20 | + repo repository.OwnerRepository |
| 21 | + authRepo repository.AuthRepository |
| 22 | +} |
| 23 | + |
| 24 | +var _ OwnerService = (*OwnerServiceImpl)(nil) |
| 25 | + |
| 26 | +func NewOwnerService(log *logrus.Logger, repo repository.OwnerRepository, authRepo repository.AuthRepository) OwnerService { |
| 27 | + return &OwnerServiceImpl{ |
| 28 | + log: log, |
| 29 | + repo: repo, |
| 30 | + authRepo: authRepo, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (os *OwnerServiceImpl) Create(req types.OwnerCreateRequest) (*types.OwnerResponse, error) { |
| 35 | + exists, err := os.authRepo.EmailExists(req.Email) |
| 36 | + if err != nil { |
| 37 | + os.log.WithError(err).Error("Failed to check email exists!") |
| 38 | + } |
| 39 | + |
| 40 | + if exists { |
| 41 | + return nil, errors.New("email already exists") |
| 42 | + } |
| 43 | + |
| 44 | + hashedPassword, err := utils.HashPassword(req.Password) |
| 45 | + if err != nil { |
| 46 | + os.log.WithError(err).Error("Failed to hash password!") |
| 47 | + return nil, errors.New("filed to process password") |
| 48 | + } |
| 49 | + |
| 50 | + owner := &types.CMSUser{ |
| 51 | + CMSUserID: uuid.New(), |
| 52 | + CMSUserName: req.Name, |
| 53 | + CMSUserEmail: req.Email, |
| 54 | + CMSNameSpace: &req.NameSpace, // TODO : Gotta fix it later as business logic |
| 55 | + Password: hashedPassword, |
| 56 | + CMSUserRole: string(types.CMSCustomer), |
| 57 | + Verified: false, |
| 58 | + CreatedAt: time.Now(), |
| 59 | + UpdatedAt: time.Now(), |
| 60 | + } |
| 61 | + |
| 62 | + if err := os.repo.CreateOwner(owner); err != nil { |
| 63 | + os.log.WithError(err).Error("Failed to create owner") |
| 64 | + return nil, errors.New("failed to create owner") |
| 65 | + } |
| 66 | + |
| 67 | + return &types.OwnerResponse{ |
| 68 | + ID: owner.CMSUserID, |
| 69 | + Name: owner.CMSUserName, |
| 70 | + Email: owner.CMSUserEmail, |
| 71 | + Role: owner.CMSUserRole, |
| 72 | + NameSpace: *owner.CMSNameSpace, |
| 73 | + Verified: owner.Verified, |
| 74 | + }, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (os *OwnerServiceImpl) Update(id string, req types.OwnerUpdateRequest) (*types.OwnerResponse, error) { |
| 78 | + owner, err := os.repo.GeyById(id) |
| 79 | + if err != nil { |
| 80 | + os.log.WithError(err).Error("Failed to get owner with id ", id) |
| 81 | + return nil, errors.New("failed to get owner") |
| 82 | + } |
| 83 | + owner.CMSUserName = req.Name |
| 84 | + // TODO : Gotta fix it later as business logic |
| 85 | + owner.CMSNameSpace = &req.NameSpace |
| 86 | + if err := os.repo.UpdateOwner(owner); err != nil { |
| 87 | + os.log.WithError(err).Error("Failed to update owner") |
| 88 | + return nil, errors.New("failed to update owner") |
| 89 | + } |
| 90 | + |
| 91 | + return &types.OwnerResponse{ |
| 92 | + ID: owner.CMSUserID, |
| 93 | + Name: owner.CMSUserName, |
| 94 | + Email: owner.CMSUserEmail, |
| 95 | + Role: owner.CMSUserRole, |
| 96 | + NameSpace: *owner.CMSNameSpace, |
| 97 | + Verified: owner.Verified, |
| 98 | + }, nil |
| 99 | +} |
0 commit comments