chores: refactor struct and structure project

This commit is contained in:
ericprd
2025-03-05 21:21:44 +08:00
parent 13a8481f22
commit 567e0e32ca
103 changed files with 1125 additions and 731 deletions
+5 -3
View File
@@ -1,14 +1,16 @@
package authsvc
import authdomain "legalgo-BE-go/internal/domain/auth"
import (
staffdomain "legalgo-BE-go/internal/domain/staff"
)
func (as *AuthSvc) GetStaffProfile(email string) (*authdomain.StaffProfile, error) {
func (as *impl) GetStaffProfile(email string) (*staffdomain.StaffProfile, error) {
staff, err := as.staffRepo.GetStaffByEmail(email)
if err != nil {
return nil, err
}
profile := &authdomain.StaffProfile{
profile := &staffdomain.StaffProfile{
ID: staff.ID,
Name: staff.Name,
Email: staff.Email,
+4 -2
View File
@@ -1,8 +1,10 @@
package authsvc
import authdomain "legalgo-BE-go/internal/domain/auth"
import (
userdomain "legalgo-BE-go/internal/domain/user"
)
func (as *AuthSvc) GetUserProfile(email string) (*authdomain.UserProfile, error) {
func (as *impl) GetUserProfile(email string) (*userdomain.UserProfile, error) {
user, err := as.userRepo.GetUserProfile(email)
if err != nil {
return nil, err
+15 -14
View File
@@ -5,34 +5,35 @@ import (
subscriberepository "legalgo-BE-go/internal/accessor/subscribe"
subscribeplanrepository "legalgo-BE-go/internal/accessor/subscribeplan"
userrepository "legalgo-BE-go/internal/accessor/user_repository"
authdomain "legalgo-BE-go/internal/domain/auth"
staffdomain "legalgo-BE-go/internal/domain/staff"
userdomain "legalgo-BE-go/internal/domain/user"
)
type AuthSvc struct {
staffRepo staffrepository.StaffIntf
type impl struct {
staffRepo staffrepository.Staff
userRepo userrepository.UserIntf
subsRepo subscriberepository.SubsIntf
subsPlanRepo subscribeplanrepository.SubsPlanIntf
}
type AuthIntf interface {
LoginAsStaff(authdomain.LoginReq) (string, error)
RegisterUser(authdomain.RegisterUserReq) (string, error)
GetUserProfile(string) (*authdomain.UserProfile, error)
type Auth interface {
LoginAsStaff(staffdomain.StaffLogin) (string, error)
RegisterStaff(staffdomain.StaffRegister) (string, error)
GetStaffProfile(string) (*staffdomain.StaffProfile, error)
UpdateStaff(staffdomain.Staff) error
LoginAsUser(authdomain.LoginReq) (string, error)
RegisterStaff(authdomain.RegisterStaffReq) (string, error)
UpdateStaff(authdomain.Staff) error
GetStaffProfile(string) (*authdomain.StaffProfile, error)
LoginAsUser(userdomain.UserLogin) (string, error)
RegisterUser(userdomain.UserRegister) (string, error)
GetUserProfile(string) (*userdomain.UserProfile, error)
}
func New(
staffRepo staffrepository.StaffIntf,
staffRepo staffrepository.Staff,
userRepo userrepository.UserIntf,
subsRepo subscriberepository.SubsIntf,
subsPlanRepo subscribeplanrepository.SubsPlanIntf,
) AuthIntf {
return &AuthSvc{
) Auth {
return &impl{
staffRepo: staffRepo,
userRepo: userRepo,
subsRepo: subsRepo,
+3 -1
View File
@@ -4,12 +4,13 @@ import (
"errors"
authdomain "legalgo-BE-go/internal/domain/auth"
staffdomain "legalgo-BE-go/internal/domain/staff"
"legalgo-BE-go/internal/utilities/utils"
"github.com/google/uuid"
)
func (sv *AuthSvc) LoginAsStaff(spec authdomain.LoginReq) (string, error) {
func (sv *impl) LoginAsStaff(spec staffdomain.StaffLogin) (string, error) {
staff, err := sv.staffRepo.GetStaffByEmail(spec.Email)
if err != nil {
return "", errors.New(err.Error())
@@ -23,6 +24,7 @@ func (sv *AuthSvc) LoginAsStaff(spec authdomain.LoginReq) (string, error) {
authToken := authdomain.AuthToken{
Email: staff.Email,
SessionID: uuid.NewString(),
Role: "staff",
}
token, err := utils.GenerateToken(authToken)
+3 -1
View File
@@ -4,12 +4,13 @@ import (
"errors"
authdomain "legalgo-BE-go/internal/domain/auth"
userdomain "legalgo-BE-go/internal/domain/user"
"legalgo-BE-go/internal/utilities/utils"
"github.com/google/uuid"
)
func (a *AuthSvc) LoginAsUser(spec authdomain.LoginReq) (string, error) {
func (a *impl) LoginAsUser(spec userdomain.UserLogin) (string, error) {
user, err := a.userRepo.GetUserByEmail(spec.Email)
if err != nil {
return "", errors.New(err.Error())
@@ -23,6 +24,7 @@ func (a *AuthSvc) LoginAsUser(spec authdomain.LoginReq) (string, error) {
authToken := authdomain.AuthToken{
Email: user.Email,
SessionID: uuid.NewString(),
Role: "user",
}
token, err := utils.GenerateToken(authToken)
+5 -3
View File
@@ -4,12 +4,13 @@ import (
"errors"
authdomain "legalgo-BE-go/internal/domain/auth"
staffdomain "legalgo-BE-go/internal/domain/staff"
"legalgo-BE-go/internal/utilities/utils"
"github.com/google/uuid"
)
func (a *AuthSvc) RegisterStaff(spec authdomain.RegisterStaffReq) (string, error) {
func (a *impl) RegisterStaff(spec staffdomain.StaffRegister) (string, error) {
_, err := a.staffRepo.GetStaffByEmail(spec.Email)
if err == nil {
return "", errors.New("this email address is already in use")
@@ -19,7 +20,7 @@ func (a *AuthSvc) RegisterStaff(spec authdomain.RegisterStaffReq) (string, error
return "", err
}
staff := authdomain.Staff{
staff := staffdomain.Staff{
ID: uuid.NewString(),
Email: spec.Email,
Password: hashedPwd,
@@ -27,7 +28,7 @@ func (a *AuthSvc) RegisterStaff(spec authdomain.RegisterStaffReq) (string, error
ProfilePicture: spec.ProfilePicture,
}
_, err = a.staffRepo.Create(&staff)
err = a.staffRepo.Create(staff)
if err != nil {
return "", errors.New(err.Error())
}
@@ -35,6 +36,7 @@ func (a *AuthSvc) RegisterStaff(spec authdomain.RegisterStaffReq) (string, error
authToken := authdomain.AuthToken{
Email: staff.Email,
SessionID: uuid.NewString(),
Role: "staff",
}
token, err := utils.GenerateToken(authToken)
+5 -3
View File
@@ -4,12 +4,13 @@ import (
"errors"
authdomain "legalgo-BE-go/internal/domain/auth"
userdomain "legalgo-BE-go/internal/domain/user"
"legalgo-BE-go/internal/utilities/utils"
"github.com/google/uuid"
)
func (a *AuthSvc) RegisterUser(spec authdomain.RegisterUserReq) (string, error) {
func (a *impl) RegisterUser(spec userdomain.UserRegister) (string, error) {
_, err := a.userRepo.GetUserByEmail(spec.Email)
if err == nil {
@@ -40,7 +41,7 @@ func (a *AuthSvc) RegisterUser(spec authdomain.RegisterUserReq) (string, error)
return "", err
}
user := authdomain.User{
user := userdomain.User{
ID: uuid.NewString(),
Email: spec.Email,
SubscribeID: subsId,
@@ -48,7 +49,7 @@ func (a *AuthSvc) RegisterUser(spec authdomain.RegisterUserReq) (string, error)
Phone: spec.Phone,
}
_, err = a.userRepo.CreateUser(&user)
err = a.userRepo.CreateUser(user)
if err != nil {
return "", errors.New(err.Error())
}
@@ -56,6 +57,7 @@ func (a *AuthSvc) RegisterUser(spec authdomain.RegisterUserReq) (string, error)
authToken := authdomain.AuthToken{
Email: user.Email,
SessionID: uuid.NewString(),
Role: "user",
}
token, err := utils.GenerateToken(authToken)
+4 -2
View File
@@ -1,8 +1,10 @@
package authsvc
import authdomain "legalgo-BE-go/internal/domain/auth"
import (
staffdomain "legalgo-BE-go/internal/domain/staff"
)
func (as *AuthSvc) UpdateStaff(spec authdomain.Staff) error {
func (as *impl) UpdateStaff(spec staffdomain.Staff) error {
if _, err := as.staffRepo.GetStaffByID(spec.ID); err != nil {
return err
}