feat: get all staff

This commit is contained in:
ericprd
2025-03-25 18:01:14 +08:00
parent 3311b80943
commit 98b871a5ed
18 changed files with 89 additions and 7 deletions
+21
View File
@@ -0,0 +1,21 @@
package staffsvc
import (
staffdomain "legalgo-BE-go/internal/domain/staff"
)
func (as *impl) GetProfile(id string) (*staffdomain.StaffProfile, error) {
staff, err := as.staffRepo.GetStaffByID(id)
if err != nil {
return nil, err
}
profile := &staffdomain.StaffProfile{
ID: staff.ID,
Name: staff.Name,
Email: staff.Email,
ProfilePicture: staff.ProfilePicture,
}
return profile, nil
}
+7
View File
@@ -0,0 +1,7 @@
package staffsvc
import staffdomain "legalgo-BE-go/internal/domain/staff"
func (i *impl) GetStaffs() ([]staffdomain.StaffProfile, error) {
return i.staffRepo.GetStaffs()
}
+7
View File
@@ -0,0 +1,7 @@
package staffsvc
import userdomain "legalgo-BE-go/internal/domain/user"
func (i *impl) GetUsers() ([]userdomain.UserProfile, error) {
return i.staffRepo.GetUsers()
}
+40
View File
@@ -0,0 +1,40 @@
package staffsvc
import (
staffrepository "legalgo-BE-go/internal/accessor/staff"
subscriberepository "legalgo-BE-go/internal/accessor/subscribe"
subscribeplanrepository "legalgo-BE-go/internal/accessor/subscribe_plan"
userrepository "legalgo-BE-go/internal/accessor/user"
staffdomain "legalgo-BE-go/internal/domain/staff"
userdomain "legalgo-BE-go/internal/domain/user"
)
type impl struct {
staffRepo staffrepository.Staff
userRepo userrepository.User
subsRepo subscriberepository.Subscribe
subsPlanRepo subscribeplanrepository.SubscribePlan
}
type Auth interface {
Login(staffdomain.StaffLogin) (string, error)
Register(staffdomain.StaffRegister) (string, error)
GetProfile(string) (*staffdomain.StaffProfile, error)
GetStaffs() ([]staffdomain.StaffProfile, error)
GetUsers() ([]userdomain.UserProfile, error)
Update(string, staffdomain.StaffUpdate) error
}
func New(
staffRepo staffrepository.Staff,
userRepo userrepository.User,
subsRepo subscriberepository.Subscribe,
subsPlanRepo subscribeplanrepository.SubscribePlan,
) Auth {
return &impl{
staffRepo: staffRepo,
userRepo: userRepo,
subsRepo: subsRepo,
subsPlanRepo: subsPlanRepo,
}
}
+36
View File
@@ -0,0 +1,36 @@
package staffsvc
import (
"fmt"
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 *impl) Login(spec staffdomain.StaffLogin) (string, error) {
staff, err := sv.staffRepo.GetStaffByEmail(spec.Email)
if err != nil {
return "", err
}
matchPassword := utils.ComparePassword(staff.Password, spec.Password)
if !matchPassword {
return "", fmt.Errorf("wrong password")
}
authToken := authdomain.AuthToken{
Email: staff.Email,
SessionID: uuid.NewString(),
Role: "staff",
ID: staff.ID,
}
token, err := utils.GenerateToken(authToken)
if err != nil {
return "", err
}
return token, nil
}
+47
View File
@@ -0,0 +1,47 @@
package staffsvc
import (
"fmt"
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 *impl) Register(spec staffdomain.StaffRegister) (string, error) {
_, err := a.staffRepo.GetStaffByEmail(spec.Email)
if err == nil {
return "", fmt.Errorf("this email address is already in use")
}
hashedPwd, err := utils.HashPassword(spec.Password)
if err != nil {
return "", err
}
staff := staffdomain.Staff{
ID: uuid.NewString(),
Email: spec.Email,
Password: hashedPwd,
Name: spec.Name,
ProfilePicture: spec.ProfilePicture,
}
err = a.staffRepo.Create(staff)
if err != nil {
return "", err
}
authToken := authdomain.AuthToken{
Email: staff.Email,
SessionID: uuid.NewString(),
Role: "staff",
ID: staff.ID,
}
token, err := utils.GenerateToken(authToken)
if err != nil {
return "", err
}
return token, nil
}
+24
View File
@@ -0,0 +1,24 @@
package staffsvc
import (
staffdomain "legalgo-BE-go/internal/domain/staff"
)
func (as *impl) Update(id string, spec staffdomain.StaffUpdate) error {
if _, err := as.staffRepo.GetStaffByID(id); err != nil {
return err
}
newSpec := staffdomain.Staff{
ID: id,
Email: spec.Email,
Name: spec.Name,
ProfilePicture: spec.ProfilePicture,
}
if err := as.staffRepo.Update(newSpec); err != nil {
return err
}
return nil
}