From 3eea2c0d1d90f225d7b5ef117a6aa08b5f57941b Mon Sep 17 00:00:00 2001 From: Iman Mirazimi Date: Fri, 5 Jan 2024 15:43:47 +0330 Subject: [PATCH] feat(niki): add skelton for admin | super-admin | staff | benefactor services --- entity/admin.go | 5 ++- entity/admin_role.go | 38 +++++++++++++++++ entity/benefactor.go | 2 +- entity/gender.go | 38 +++++++++++++++++ entity/staff.go | 18 +++++++++ entity/staff_status.go | 38 +++++++++++++++++ entity/user_gender.go | 38 ----------------- param/admin/admin/get.go | 1 + param/admin/admin/get_all_benefactor.go | 1 + param/admin/admin/get_all_staff.go | 1 + param/admin/admin/get_benefactor.go | 1 + param/admin/admin/get_staff.go | 1 + param/admin/admin/login.go | 1 + param/admin/admin/register_benefactor.go | 1 + param/admin/admin/register_staff.go | 1 + param/admin/admin/update_benefactor.go | 1 + param/admin/admin/update_staff.go | 1 + param/admin/super_admin/get_admin.go | 1 + param/admin/super_admin/get_all_admin.go | 1 + param/admin/super_admin/login.go | 1 + param/admin/super_admin/register_admin.go | 1 + param/admin/super_admin/update_admin.go | 1 + param/benefactor/benefactor/get.go | 7 ++++ param/benefactor/benefactor/login.go | 7 ++++ param/benefactor/benefactor/register.go | 9 +++++ param/benefactor/benefactor/update.go | 1 + param/staff/staff/get.go | 1 + param/staff/staff/login.go | 1 + service/admin/admin/get.go | 1 + service/admin/admin/get_all_benefactor.go | 1 + service/admin/admin/get_all_staff.go | 1 + service/admin/admin/get_benefactor.go | 1 + service/admin/admin/get_staff.go | 1 + service/admin/admin/login.go | 1 + service/admin/admin/register_benefactor.go | 1 + service/admin/admin/register_staff.go | 1 + service/admin/admin/service.go | 1 + service/admin/admin/update_benefactor.go | 1 + service/admin/admin/update_staff.go | 1 + service/admin/super_admin/get_admin.go | 1 + service/admin/super_admin/get_all_admin.go | 1 + service/admin/super_admin/login.go | 1 + service/admin/super_admin/register_admin.go | 1 + service/admin/super_admin/service.go | 1 + service/admin/super_admin/update_admin.go | 1 + service/benefactor/benefactor/get.go | 1 + service/benefactor/benefactor/login.go | 45 +++++++++++++++++++++ service/benefactor/benefactor/register.go | 36 +++++++++++++++++ service/benefactor/benefactor/service.go | 27 +++++++++++++ service/benefactor/benefactor/update.go | 1 + service/staff/staff/get.go | 1 + service/staff/staff/login.go | 45 +++++++++++++++++++++ service/staff/staff/service.go | 27 +++++++++++++ 53 files changed, 377 insertions(+), 41 deletions(-) create mode 100644 entity/admin_role.go create mode 100644 entity/gender.go create mode 100644 entity/staff.go create mode 100644 entity/staff_status.go delete mode 100644 entity/user_gender.go create mode 100644 param/admin/admin/get.go create mode 100644 param/admin/admin/get_all_benefactor.go create mode 100644 param/admin/admin/get_all_staff.go create mode 100644 param/admin/admin/get_benefactor.go create mode 100644 param/admin/admin/get_staff.go create mode 100644 param/admin/admin/login.go create mode 100644 param/admin/admin/register_benefactor.go create mode 100644 param/admin/admin/register_staff.go create mode 100644 param/admin/admin/update_benefactor.go create mode 100644 param/admin/admin/update_staff.go create mode 100644 param/admin/super_admin/get_admin.go create mode 100644 param/admin/super_admin/get_all_admin.go create mode 100644 param/admin/super_admin/login.go create mode 100644 param/admin/super_admin/register_admin.go create mode 100644 param/admin/super_admin/update_admin.go create mode 100644 param/benefactor/benefactor/get.go create mode 100644 param/benefactor/benefactor/login.go create mode 100644 param/benefactor/benefactor/register.go create mode 100644 param/benefactor/benefactor/update.go create mode 100644 param/staff/staff/get.go create mode 100644 param/staff/staff/login.go create mode 100644 service/admin/admin/get.go create mode 100644 service/admin/admin/get_all_benefactor.go create mode 100644 service/admin/admin/get_all_staff.go create mode 100644 service/admin/admin/get_benefactor.go create mode 100644 service/admin/admin/get_staff.go create mode 100644 service/admin/admin/login.go create mode 100644 service/admin/admin/register_benefactor.go create mode 100644 service/admin/admin/register_staff.go create mode 100644 service/admin/admin/service.go create mode 100644 service/admin/admin/update_benefactor.go create mode 100644 service/admin/admin/update_staff.go create mode 100644 service/admin/super_admin/get_admin.go create mode 100644 service/admin/super_admin/get_all_admin.go create mode 100644 service/admin/super_admin/login.go create mode 100644 service/admin/super_admin/register_admin.go create mode 100644 service/admin/super_admin/service.go create mode 100644 service/admin/super_admin/update_admin.go create mode 100644 service/benefactor/benefactor/get.go create mode 100644 service/benefactor/benefactor/login.go create mode 100644 service/benefactor/benefactor/register.go create mode 100644 service/benefactor/benefactor/service.go create mode 100644 service/benefactor/benefactor/update.go create mode 100644 service/staff/staff/get.go create mode 100644 service/staff/staff/login.go create mode 100644 service/staff/staff/service.go diff --git a/entity/admin.go b/entity/admin.go index 39bdfab..76e83e6 100644 --- a/entity/admin.go +++ b/entity/admin.go @@ -7,12 +7,13 @@ type Admin struct { FirstName string LastName string PhoneNumber string + Role AdminRole Address string Description string Email string City string - Gender UserGender - Status BenefactorStatus + Gender Gender + Status AdminStatus Birthday time.Time StatusChangedAt time.Time } diff --git a/entity/admin_role.go b/entity/admin_role.go new file mode 100644 index 0000000..2251e87 --- /dev/null +++ b/entity/admin_role.go @@ -0,0 +1,38 @@ +package entity + +type AdminRole uint + +const ( + AdminAdminRole AdminRole = iota + 1 + AdminSuperAdminRole +) + +var AdminRoleStrings = map[AdminRole]string{ + AdminAdminRole: "admin", + AdminSuperAdminRole: "super-admin", +} + +func (s AdminRole) String() string { + return AdminRoleStrings[s] +} + +// AllAdminRole returns a slice containing all string values of AdminRole. +func AllAdminRole() []string { + roleStrings := make([]string, len(AdminRoleStrings)) + for role, str := range AdminRoleStrings { + roleStrings[int(role)-1] = str + } + + return roleStrings +} + +// MapToAdminRole converts a string to the corresponding AdminRole value. +func MapToAdminRole(roleStr string) AdminRole { + for role, str := range AdminRoleStrings { + if str == roleStr { + return role + } + } + + return AdminRole(0) +} diff --git a/entity/benefactor.go b/entity/benefactor.go index 02db3c6..9bf5288 100644 --- a/entity/benefactor.go +++ b/entity/benefactor.go @@ -11,7 +11,7 @@ type Benefactor struct { Description string Email string City string - Gender UserGender + Gender Gender Status BenefactorStatus Birthday time.Time StatusChangedAt time.Time diff --git a/entity/gender.go b/entity/gender.go new file mode 100644 index 0000000..797d872 --- /dev/null +++ b/entity/gender.go @@ -0,0 +1,38 @@ +package entity + +type Gender uint + +const ( + MaleGender Gender = iota + 1 + FemaleGender +) + +var GenderStrings = map[Gender]string{ + MaleGender: "male", + FemaleGender: "female", +} + +func (s Gender) String() string { + return GenderStrings[s] +} + +// AllGender returns a slice containing all string values of Gender. +func AllGender() []string { + statusStrings := make([]string, len(GenderStrings)) + for status, str := range GenderStrings { + statusStrings[int(status)-1] = str + } + + return statusStrings +} + +// MapToGender converts a string to the corresponding Gender value. +func MapToGender(statusStr string) Gender { + for status, str := range GenderStrings { + if str == statusStr { + return status + } + } + + return Gender(0) +} diff --git a/entity/staff.go b/entity/staff.go new file mode 100644 index 0000000..b5095c5 --- /dev/null +++ b/entity/staff.go @@ -0,0 +1,18 @@ +package entity + +import "time" + +type Staff struct { + ID uint + FirstName string + LastName string + PhoneNumber string + Address string + Description string + Email string + City string + Gender Gender + Status StaffStatus + Birthday time.Time + StatusChangedAt time.Time +} diff --git a/entity/staff_status.go b/entity/staff_status.go new file mode 100644 index 0000000..3a348a8 --- /dev/null +++ b/entity/staff_status.go @@ -0,0 +1,38 @@ +package entity + +type StaffStatus uint + +const ( + StaffActiveStatus StaffStatus = iota + 1 + StaffDeactiveStatus +) + +var StaffStatusStrings = map[StaffStatus]string{ + StaffActiveStatus: "active", + StaffDeactiveStatus: "deactive", +} + +func (s StaffStatus) String() string { + return StaffStatusStrings[s] +} + +// AllStaffStatus returns a slice containing all string values of StaffStatus. +func AllStaffStatus() []string { + statusStrings := make([]string, len(StaffStatusStrings)) + for status, str := range StaffStatusStrings { + statusStrings[int(status)-1] = str + } + + return statusStrings +} + +// MapToStaffStatus converts a string to the corresponding StaffStatus value. +func MapToStaffStatus(statusStr string) StaffStatus { + for status, str := range StaffStatusStrings { + if str == statusStr { + return status + } + } + + return StaffStatus(0) +} diff --git a/entity/user_gender.go b/entity/user_gender.go deleted file mode 100644 index 2670f91..0000000 --- a/entity/user_gender.go +++ /dev/null @@ -1,38 +0,0 @@ -package entity - -type UserGender uint - -const ( - UserMaleGender UserGender = iota + 1 - UserFemaleGender -) - -var UserGenderStrings = map[UserGender]string{ - UserMaleGender: "male", - UserFemaleGender: "female", -} - -func (s UserGender) String() string { - return UserGenderStrings[s] -} - -// AllUserGender returns a slice containing all string values of UserGender. -func AllUserGender() []string { - statusStrings := make([]string, len(UserGenderStrings)) - for status, str := range UserGenderStrings { - statusStrings[int(status)-1] = str - } - - return statusStrings -} - -// MapToUserGender converts a string to the corresponding UserGender value. -func MapToUserGender(statusStr string) UserGender { - for status, str := range UserGenderStrings { - if str == statusStr { - return status - } - } - - return UserGender(0) -} diff --git a/param/admin/admin/get.go b/param/admin/admin/get.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/get.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/admin/get_all_benefactor.go b/param/admin/admin/get_all_benefactor.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/get_all_benefactor.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/admin/get_all_staff.go b/param/admin/admin/get_all_staff.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/get_all_staff.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/admin/get_benefactor.go b/param/admin/admin/get_benefactor.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/get_benefactor.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/admin/get_staff.go b/param/admin/admin/get_staff.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/get_staff.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/admin/login.go b/param/admin/admin/login.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/login.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/admin/register_benefactor.go b/param/admin/admin/register_benefactor.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/register_benefactor.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/admin/register_staff.go b/param/admin/admin/register_staff.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/register_staff.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/admin/update_benefactor.go b/param/admin/admin/update_benefactor.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/update_benefactor.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/admin/update_staff.go b/param/admin/admin/update_staff.go new file mode 100644 index 0000000..1ae4025 --- /dev/null +++ b/param/admin/admin/update_staff.go @@ -0,0 +1 @@ +package adminparam diff --git a/param/admin/super_admin/get_admin.go b/param/admin/super_admin/get_admin.go new file mode 100644 index 0000000..a15243e --- /dev/null +++ b/param/admin/super_admin/get_admin.go @@ -0,0 +1 @@ +package superadminparam diff --git a/param/admin/super_admin/get_all_admin.go b/param/admin/super_admin/get_all_admin.go new file mode 100644 index 0000000..a15243e --- /dev/null +++ b/param/admin/super_admin/get_all_admin.go @@ -0,0 +1 @@ +package superadminparam diff --git a/param/admin/super_admin/login.go b/param/admin/super_admin/login.go new file mode 100644 index 0000000..a15243e --- /dev/null +++ b/param/admin/super_admin/login.go @@ -0,0 +1 @@ +package superadminparam diff --git a/param/admin/super_admin/register_admin.go b/param/admin/super_admin/register_admin.go new file mode 100644 index 0000000..a15243e --- /dev/null +++ b/param/admin/super_admin/register_admin.go @@ -0,0 +1 @@ +package superadminparam diff --git a/param/admin/super_admin/update_admin.go b/param/admin/super_admin/update_admin.go new file mode 100644 index 0000000..a15243e --- /dev/null +++ b/param/admin/super_admin/update_admin.go @@ -0,0 +1 @@ +package superadminparam diff --git a/param/benefactor/benefactor/get.go b/param/benefactor/benefactor/get.go new file mode 100644 index 0000000..9afd5df --- /dev/null +++ b/param/benefactor/benefactor/get.go @@ -0,0 +1,7 @@ +package benefactorparam + +type BenefactorProfileRequest struct{} + +type BenefactorProfileResponse struct { + Benefactor entity.Benefactor +} diff --git a/param/benefactor/benefactor/login.go b/param/benefactor/benefactor/login.go new file mode 100644 index 0000000..b483d57 --- /dev/null +++ b/param/benefactor/benefactor/login.go @@ -0,0 +1,7 @@ +package benefactorparam + +type BenefactorLoginRequest struct{} + +type BenefactorLoginResponse struct { + Benefactor entity.Benefactor +} diff --git a/param/benefactor/benefactor/register.go b/param/benefactor/benefactor/register.go new file mode 100644 index 0000000..5e60d35 --- /dev/null +++ b/param/benefactor/benefactor/register.go @@ -0,0 +1,9 @@ +package benefactorparam + +import entity "git.gocasts.ir/ebhomengo/niki/entity" + +type BenefactorRegisterRequest struct{} + +type BenefactorRegisterResponse struct { + Benefactor entity.Benefactor +} diff --git a/param/benefactor/benefactor/update.go b/param/benefactor/benefactor/update.go new file mode 100644 index 0000000..ee3160a --- /dev/null +++ b/param/benefactor/benefactor/update.go @@ -0,0 +1 @@ +package benefactorparam diff --git a/param/staff/staff/get.go b/param/staff/staff/get.go new file mode 100644 index 0000000..ee4625b --- /dev/null +++ b/param/staff/staff/get.go @@ -0,0 +1 @@ +package staffparam diff --git a/param/staff/staff/login.go b/param/staff/staff/login.go new file mode 100644 index 0000000..ee4625b --- /dev/null +++ b/param/staff/staff/login.go @@ -0,0 +1 @@ +package staffparam diff --git a/service/admin/admin/get.go b/service/admin/admin/get.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/get.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/get_all_benefactor.go b/service/admin/admin/get_all_benefactor.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/get_all_benefactor.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/get_all_staff.go b/service/admin/admin/get_all_staff.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/get_all_staff.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/get_benefactor.go b/service/admin/admin/get_benefactor.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/get_benefactor.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/get_staff.go b/service/admin/admin/get_staff.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/get_staff.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/login.go b/service/admin/admin/login.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/login.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/register_benefactor.go b/service/admin/admin/register_benefactor.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/register_benefactor.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/register_staff.go b/service/admin/admin/register_staff.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/register_staff.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/service.go b/service/admin/admin/service.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/service.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/update_benefactor.go b/service/admin/admin/update_benefactor.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/update_benefactor.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/admin/update_staff.go b/service/admin/admin/update_staff.go new file mode 100644 index 0000000..da1256d --- /dev/null +++ b/service/admin/admin/update_staff.go @@ -0,0 +1 @@ +package adminservice diff --git a/service/admin/super_admin/get_admin.go b/service/admin/super_admin/get_admin.go new file mode 100644 index 0000000..5806c01 --- /dev/null +++ b/service/admin/super_admin/get_admin.go @@ -0,0 +1 @@ +package superadminservice diff --git a/service/admin/super_admin/get_all_admin.go b/service/admin/super_admin/get_all_admin.go new file mode 100644 index 0000000..5806c01 --- /dev/null +++ b/service/admin/super_admin/get_all_admin.go @@ -0,0 +1 @@ +package superadminservice diff --git a/service/admin/super_admin/login.go b/service/admin/super_admin/login.go new file mode 100644 index 0000000..5806c01 --- /dev/null +++ b/service/admin/super_admin/login.go @@ -0,0 +1 @@ +package superadminservice diff --git a/service/admin/super_admin/register_admin.go b/service/admin/super_admin/register_admin.go new file mode 100644 index 0000000..5806c01 --- /dev/null +++ b/service/admin/super_admin/register_admin.go @@ -0,0 +1 @@ +package superadminservice diff --git a/service/admin/super_admin/service.go b/service/admin/super_admin/service.go new file mode 100644 index 0000000..5806c01 --- /dev/null +++ b/service/admin/super_admin/service.go @@ -0,0 +1 @@ +package superadminservice diff --git a/service/admin/super_admin/update_admin.go b/service/admin/super_admin/update_admin.go new file mode 100644 index 0000000..5806c01 --- /dev/null +++ b/service/admin/super_admin/update_admin.go @@ -0,0 +1 @@ +package superadminservice diff --git a/service/benefactor/benefactor/get.go b/service/benefactor/benefactor/get.go new file mode 100644 index 0000000..5673010 --- /dev/null +++ b/service/benefactor/benefactor/get.go @@ -0,0 +1 @@ +package benefactorservice diff --git a/service/benefactor/benefactor/login.go b/service/benefactor/benefactor/login.go new file mode 100644 index 0000000..5c293b5 --- /dev/null +++ b/service/benefactor/benefactor/login.go @@ -0,0 +1,45 @@ +package benefactorservice + +import ( + "fmt" + + "gameapp/param" + "gameapp/pkg/richerror" +) + +func (s Service) Login(req param.LoginRequest) (param.LoginResponse, error) { + const op = "benefactorservice.Login" + + // TODO - it would be better to user two separate method for existence check and getUserByPhoneNumber + user, err := s.repo.GetUserByPhoneNumber(req.PhoneNumber) + if err != nil { + return param.LoginResponse{}, richerror.New(op).WithErr(err). + WithMeta(map[string]interface{}{"phone_number": req.PhoneNumber}) + } + + if user.Password != getMD5Hash(req.Password) { + return param.LoginResponse{}, fmt.Errorf("username or password isn't correct") + } + + accessToken, err := s.auth.CreateAccessToken(user) + if err != nil { + return param.LoginResponse{}, fmt.Errorf("unexpected error: %w", err) + } + + refreshToken, err := s.auth.CreateRefreshToken(user) + if err != nil { + return param.LoginResponse{}, fmt.Errorf("unexpected error: %w", err) + } + + return param.LoginResponse{ + User: param.UserInfo{ + ID: user.ID, + PhoneNumber: user.PhoneNumber, + Name: user.Name, + }, + Tokens: param.Tokens{ + AccessToken: accessToken, + RefreshToken: refreshToken, + }, + }, nil +} diff --git a/service/benefactor/benefactor/register.go b/service/benefactor/benefactor/register.go new file mode 100644 index 0000000..22b921c --- /dev/null +++ b/service/benefactor/benefactor/register.go @@ -0,0 +1,36 @@ +package benefactorservice + +import ( + "fmt" + + entity "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor" +) + +// rol +// login(sms)-> {rigester}() -> next +func (s Service) Register(req param.RegisterRequest) (param.RegisterResponse, error) { + // TODO - we should verify phone number by verification code + + // TODO - replace md5 with bcrypt + user := entity.Benefactor{ + ID: 0, + PhoneNumber: req.PhoneNumber, + Name: req.Name, + Password: getMD5Hash(req.Password), + Role: entity.BenefactorRole, + } + + // create new user in storage + createdUser, err := s.repo.Register(user) + if err != nil { + return param.RegisterResponse{}, fmt.Errorf("unexpected error: %w", err) + } + + // return created user + return param.RegisterResponse{User: param.UserInfo{ + ID: createdUser.ID, + PhoneNumber: createdUser.Name, + Name: createdUser.PhoneNumber, + }}, nil +} diff --git a/service/benefactor/benefactor/service.go b/service/benefactor/benefactor/service.go new file mode 100644 index 0000000..a3ec957 --- /dev/null +++ b/service/benefactor/benefactor/service.go @@ -0,0 +1,27 @@ +package benefactorservice + +import ( + "context" + + "git.gocasts.ir/ebhomengo/niki/entity" +) + +type Repository interface { + Register(benefactor entity.Benefactor) (entity.Benefactor, error) + GetUserByPhoneNumber(phoneNumber string) (entity.Benefactor, error) + GetUserByID(ctx context.Context, userID uint) (entity.Benefactor, error) +} + +type AuthGenerator interface { + CreateAccessToken(benefactor entity.Benefactor) (string, error) + CreateRefreshToken(benefactor entity.Benefactor) (string, error) +} + +type Service struct { + auth AuthGenerator + repo Repository +} + +func New(authGenerator AuthGenerator, repo Repository) Service { + return Service{auth: authGenerator, repo: repo} +} diff --git a/service/benefactor/benefactor/update.go b/service/benefactor/benefactor/update.go new file mode 100644 index 0000000..5673010 --- /dev/null +++ b/service/benefactor/benefactor/update.go @@ -0,0 +1 @@ +package benefactorservice diff --git a/service/staff/staff/get.go b/service/staff/staff/get.go new file mode 100644 index 0000000..b246338 --- /dev/null +++ b/service/staff/staff/get.go @@ -0,0 +1 @@ +package staffservice diff --git a/service/staff/staff/login.go b/service/staff/staff/login.go new file mode 100644 index 0000000..30388e0 --- /dev/null +++ b/service/staff/staff/login.go @@ -0,0 +1,45 @@ +package staffservice + +import ( + "fmt" + + "gameapp/param" + "gameapp/pkg/richerror" +) + +func (s Service) Login(req param.LoginRequest) (param.LoginResponse, error) { + const op = "benefactorservice.Login" + + // TODO - it would be better to user two separate method for existence check and getUserByPhoneNumber + user, err := s.repo.GetUserByPhoneNumber(req.PhoneNumber) + if err != nil { + return param.LoginResponse{}, richerror.New(op).WithErr(err). + WithMeta(map[string]interface{}{"phone_number": req.PhoneNumber}) + } + + if user.Password != getMD5Hash(req.Password) { + return param.LoginResponse{}, fmt.Errorf("username or password isn't correct") + } + + accessToken, err := s.auth.CreateAccessToken(user) + if err != nil { + return param.LoginResponse{}, fmt.Errorf("unexpected error: %w", err) + } + + refreshToken, err := s.auth.CreateRefreshToken(user) + if err != nil { + return param.LoginResponse{}, fmt.Errorf("unexpected error: %w", err) + } + + return param.LoginResponse{ + User: param.UserInfo{ + ID: user.ID, + PhoneNumber: user.PhoneNumber, + Name: user.Name, + }, + Tokens: param.Tokens{ + AccessToken: accessToken, + RefreshToken: refreshToken, + }, + }, nil +} diff --git a/service/staff/staff/service.go b/service/staff/staff/service.go new file mode 100644 index 0000000..9a21f9e --- /dev/null +++ b/service/staff/staff/service.go @@ -0,0 +1,27 @@ +package staffservice + +import ( + "context" + + "git.gocasts.ir/ebhomengo/niki/entity" +) + +type Repository interface { + Register(benefactor entity.Benefactor) (entity.Benefactor, error) + GetUserByPhoneNumber(phoneNumber string) (entity.Benefactor, error) + GetUserByID(ctx context.Context, userID uint) (entity.Benefactor, error) +} + +type AuthGenerator interface { + CreateAccessToken(benefactor entity.Benefactor) (string, error) + CreateRefreshToken(benefactor entity.Benefactor) (string, error) +} + +type Service struct { + auth AuthGenerator + repo Repository +} + +func New(authGenerator AuthGenerator, repo Repository) Service { + return Service{auth: authGenerator, repo: repo} +}