From 98b871a5eda718195abc087aa8cd8b9cc4a81441 Mon Sep 17 00:00:00 2001 From: ericprd Date: Tue, 25 Mar 2025 18:01:14 +0800 Subject: [PATCH] feat: get all staff --- internal/accessor/staff/get_staff.go | 13 ++++ internal/accessor/staff/impl.go | 1 + internal/api/http/news/update.go | 2 +- internal/api/http/staff/get_staffs.go | 59 +++++++++++++++++++ internal/api/http/staff/get_users.go | 2 +- internal/api/http/staff/login.go | 2 +- internal/api/http/staff/module.go | 1 + internal/api/http/staff/profile.go | 2 +- internal/api/http/staff/register.go | 2 +- internal/api/http/staff/update.go | 2 +- internal/services/module.go | 2 +- internal/services/{staffsvc => staff}/get.go | 0 internal/services/staff/get_staffs.go | 7 +++ .../services/{staffsvc => staff}/get_users.go | 0 internal/services/{staffsvc => staff}/impl.go | 1 + .../services/{staffsvc => staff}/login.go | 0 .../services/{staffsvc => staff}/register.go | 0 .../services/{staffsvc => staff}/update.go | 0 18 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 internal/accessor/staff/get_staff.go create mode 100644 internal/api/http/staff/get_staffs.go rename internal/services/{staffsvc => staff}/get.go (100%) create mode 100644 internal/services/staff/get_staffs.go rename internal/services/{staffsvc => staff}/get_users.go (100%) rename internal/services/{staffsvc => staff}/impl.go (95%) rename internal/services/{staffsvc => staff}/login.go (100%) rename internal/services/{staffsvc => staff}/register.go (100%) rename internal/services/{staffsvc => staff}/update.go (100%) diff --git a/internal/accessor/staff/get_staff.go b/internal/accessor/staff/get_staff.go new file mode 100644 index 0000000..103657a --- /dev/null +++ b/internal/accessor/staff/get_staff.go @@ -0,0 +1,13 @@ +package staffrepository + +import staffdomain "legalgo-BE-go/internal/domain/staff" + +func (a *accessor) GetStaffs() ([]staffdomain.StaffProfile, error) { + var staffs []staffdomain.StaffProfile + + if err := a.db.Table("staffs").Find(&staffs).Error; err != nil { + return staffs, err + } + + return staffs, nil +} diff --git a/internal/accessor/staff/impl.go b/internal/accessor/staff/impl.go index 633d125..d8d4750 100644 --- a/internal/accessor/staff/impl.go +++ b/internal/accessor/staff/impl.go @@ -14,6 +14,7 @@ type Staff interface { GetStaffByEmail(string) (*staffdomain.Staff, error) GetStaffByID(string) (*staffdomain.Staff, error) GetUsers() ([]userdomain.UserProfile, error) + GetStaffs() ([]staffdomain.StaffProfile, error) Create(staffdomain.Staff) error Update(staffdomain.Staff) error } diff --git a/internal/api/http/news/update.go b/internal/api/http/news/update.go index dec6777..e4d9280 100644 --- a/internal/api/http/news/update.go +++ b/internal/api/http/news/update.go @@ -5,7 +5,7 @@ import ( authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth" newsdomain "legalgo-BE-go/internal/domain/news" newssvc "legalgo-BE-go/internal/services/news" - staffsvc "legalgo-BE-go/internal/services/staffsvc" + staffsvc "legalgo-BE-go/internal/services/staff" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" "net/http" diff --git a/internal/api/http/staff/get_staffs.go b/internal/api/http/staff/get_staffs.go new file mode 100644 index 0000000..fc439b3 --- /dev/null +++ b/internal/api/http/staff/get_staffs.go @@ -0,0 +1,59 @@ +package staffhttp + +import ( + authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth" + staffsvc "legalgo-BE-go/internal/services/staff" + "legalgo-BE-go/internal/utilities/response" + "legalgo-BE-go/internal/utilities/utils" + "net/http" + + "github.com/go-chi/chi/v5" +) + +func GetStaffs( + router chi.Router, + staffSvc staffsvc.Auth, +) { + router.With(authmiddleware.Authorize()).Get("/staff/get-all", func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + staffDetail, err := utils.GetTokenDetail(r) + if err != nil { + response.RespondJsonErrorWithCode( + ctx, + w, + err, + response.ErrBadRequest.Code, + response.ErrBadRequest.HttpCode, + "failed to get staff token", + ) + return + } + + if staffDetail.Role != "staff" { + response.RespondJsonErrorWithCode( + ctx, + w, + err, + response.ErrUnauthorized.Code, + response.ErrUnauthorized.HttpCode, + "unauthorized", + ) + return + } + + staffs, err := staffSvc.GetStaffs() + if err != nil { + response.RespondJsonErrorWithCode( + ctx, + w, + err, + response.ErrBadRequest.Code, + response.ErrBadRequest.HttpCode, + err.Error(), + ) + return + } + + response.RespondJsonSuccess(ctx, w, staffs) + }) +} diff --git a/internal/api/http/staff/get_users.go b/internal/api/http/staff/get_users.go index 5fab9b3..30e644f 100644 --- a/internal/api/http/staff/get_users.go +++ b/internal/api/http/staff/get_users.go @@ -2,7 +2,7 @@ package staffhttp import ( authmiddleware "legalgo-BE-go/internal/api/http/middleware/auth" - staffsvc "legalgo-BE-go/internal/services/staffsvc" + staffsvc "legalgo-BE-go/internal/services/staff" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" "net/http" diff --git a/internal/api/http/staff/login.go b/internal/api/http/staff/login.go index 871441f..4c5dde1 100644 --- a/internal/api/http/staff/login.go +++ b/internal/api/http/staff/login.go @@ -5,7 +5,7 @@ import ( responsedomain "legalgo-BE-go/internal/domain/reponse" staffdomain "legalgo-BE-go/internal/domain/staff" - staffsvc "legalgo-BE-go/internal/services/staffsvc" + staffsvc "legalgo-BE-go/internal/services/staff" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" diff --git a/internal/api/http/staff/module.go b/internal/api/http/staff/module.go index ee3df16..8ba5f18 100644 --- a/internal/api/http/staff/module.go +++ b/internal/api/http/staff/module.go @@ -9,5 +9,6 @@ var Module = fx.Module("auth-api", Update, GetProfile, GetUsers, + GetStaffs, ), ) diff --git a/internal/api/http/staff/profile.go b/internal/api/http/staff/profile.go index 56fb943..20d6b78 100644 --- a/internal/api/http/staff/profile.go +++ b/internal/api/http/staff/profile.go @@ -1,7 +1,7 @@ package staffhttp import ( - staffsvc "legalgo-BE-go/internal/services/staffsvc" + staffsvc "legalgo-BE-go/internal/services/staff" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" "net/http" diff --git a/internal/api/http/staff/register.go b/internal/api/http/staff/register.go index 0713986..0efb047 100644 --- a/internal/api/http/staff/register.go +++ b/internal/api/http/staff/register.go @@ -4,7 +4,7 @@ import ( "net/http" staffdomain "legalgo-BE-go/internal/domain/staff" - staffsvc "legalgo-BE-go/internal/services/staffsvc" + staffsvc "legalgo-BE-go/internal/services/staff" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" diff --git a/internal/api/http/staff/update.go b/internal/api/http/staff/update.go index 44b4ef4..1e0b832 100644 --- a/internal/api/http/staff/update.go +++ b/internal/api/http/staff/update.go @@ -2,7 +2,7 @@ package staffhttp import ( staffdomain "legalgo-BE-go/internal/domain/staff" - staffsvc "legalgo-BE-go/internal/services/staffsvc" + staffsvc "legalgo-BE-go/internal/services/staff" "legalgo-BE-go/internal/utilities/response" "legalgo-BE-go/internal/utilities/utils" "net/http" diff --git a/internal/services/module.go b/internal/services/module.go index 5c0da4c..95ec6b2 100644 --- a/internal/services/module.go +++ b/internal/services/module.go @@ -6,7 +6,7 @@ import ( logssvc "legalgo-BE-go/internal/services/logs" newssvc "legalgo-BE-go/internal/services/news" "legalgo-BE-go/internal/services/oss" - staffsvc "legalgo-BE-go/internal/services/staffsvc" + staffsvc "legalgo-BE-go/internal/services/staff" subscribesvc "legalgo-BE-go/internal/services/subscribe" subscribeplansvc "legalgo-BE-go/internal/services/subscribe_plan" tagsvc "legalgo-BE-go/internal/services/tag" diff --git a/internal/services/staffsvc/get.go b/internal/services/staff/get.go similarity index 100% rename from internal/services/staffsvc/get.go rename to internal/services/staff/get.go diff --git a/internal/services/staff/get_staffs.go b/internal/services/staff/get_staffs.go new file mode 100644 index 0000000..713df93 --- /dev/null +++ b/internal/services/staff/get_staffs.go @@ -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() +} diff --git a/internal/services/staffsvc/get_users.go b/internal/services/staff/get_users.go similarity index 100% rename from internal/services/staffsvc/get_users.go rename to internal/services/staff/get_users.go diff --git a/internal/services/staffsvc/impl.go b/internal/services/staff/impl.go similarity index 95% rename from internal/services/staffsvc/impl.go rename to internal/services/staff/impl.go index 827a41e..a1bba69 100644 --- a/internal/services/staffsvc/impl.go +++ b/internal/services/staff/impl.go @@ -20,6 +20,7 @@ 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 } diff --git a/internal/services/staffsvc/login.go b/internal/services/staff/login.go similarity index 100% rename from internal/services/staffsvc/login.go rename to internal/services/staff/login.go diff --git a/internal/services/staffsvc/register.go b/internal/services/staff/register.go similarity index 100% rename from internal/services/staffsvc/register.go rename to internal/services/staff/register.go diff --git a/internal/services/staffsvc/update.go b/internal/services/staff/update.go similarity index 100% rename from internal/services/staffsvc/update.go rename to internal/services/staff/update.go