feat: user admin akses job and add dep midtrans

This commit is contained in:
pahmiudahgede 2025-03-14 02:55:34 +07:00
parent 7d2a02c65f
commit cc347fed7d
6 changed files with 131 additions and 4 deletions

1
go.mod
View File

@ -27,6 +27,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/midtrans/midtrans-go v1.3.8 // indirect
github.com/rivo/uniseg v0.2.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect

2
go.sum
View File

@ -48,6 +48,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/midtrans/midtrans-go v1.3.8 h1:r6eq51LJwbMQ05dBF3Twg99u45G3pLxP5INYoqOoNzU=
github.com/midtrans/midtrans-go v1.3.8/go.mod h1:5hN2oiZDP3/SwSBxHPTg8eC/RVoRE9DXQOY1Ah9au10=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=

View File

@ -30,6 +30,49 @@ func (h *UserProfileHandler) GetUserProfile(c *fiber.Ctx) error {
return utils.SuccessResponse(c, userProfile, "User profile retrieved successfully") return utils.SuccessResponse(c, userProfile, "User profile retrieved successfully")
} }
func (h *UserProfileHandler) GetUserProfileById(c *fiber.Ctx) error {
userID := c.Params("userid")
if userID == "" {
return utils.ValidationErrorResponse(c, map[string][]string{"userid": {"user ID is required"}})
}
// userID, ok := c.Locals("userID").(string)
// if !ok || userID == "" {
// return utils.GenericResponse(c, fiber.StatusUnauthorized, "Unauthorized: User session not found")
// }
userProfile, err := h.UserProfileService.GetUserProfile(userID)
if err != nil {
return utils.GenericResponse(c, fiber.StatusNotFound, err.Error())
}
return utils.SuccessResponse(c, userProfile, "User profile retrieved successfully")
}
func (h *UserProfileHandler) GetAllUsers(c *fiber.Ctx) error {
users, err := h.UserProfileService.GetAllUsers()
if err != nil {
return utils.GenericResponse(c, fiber.StatusInternalServerError, err.Error())
}
return utils.SuccessResponse(c, users, "All users retrieved successfully")
}
func (h *UserProfileHandler) GetUsersByRoleID(c *fiber.Ctx) error {
roleID := c.Params("roleid")
if roleID == "" {
return utils.ValidationErrorResponse(c, map[string][]string{"roleId": {"Role ID is required"}})
}
users, err := h.UserProfileService.GetUsersByRoleID(roleID)
if err != nil {
return utils.GenericResponse(c, fiber.StatusInternalServerError, err.Error())
}
return utils.SuccessResponse(c, users, "Users retrieved successfully")
}
func (h *UserProfileHandler) UpdateUserProfile(c *fiber.Ctx) error { func (h *UserProfileHandler) UpdateUserProfile(c *fiber.Ctx) error {
var updateData dto.UpdateUserDTO var updateData dto.UpdateUserDTO
if err := c.BodyParser(&updateData); err != nil { if err := c.BodyParser(&updateData); err != nil {

View File

@ -11,6 +11,9 @@ type UserProfileRepository interface {
FindByID(userID string) (*model.User, error) FindByID(userID string) (*model.User, error)
Update(user *model.User) error Update(user *model.User) error
UpdateAvatar(userID, avatarURL string) error UpdateAvatar(userID, avatarURL string) error
FindAll() ([]model.User, error)
FindByRoleID(roleID string) ([]model.User, error)
} }
type userProfileRepository struct { type userProfileRepository struct {
@ -54,3 +57,21 @@ func (r *userProfileRepository) UpdateAvatar(userID, avatarURL string) error {
} }
return nil return nil
} }
func (r *userProfileRepository) FindAll() ([]model.User, error) {
var users []model.User
err := r.DB.Preload("Role").Find(&users).Error
if err != nil {
return nil, err
}
return users, nil
}
func (r *userProfileRepository) FindByRoleID(roleID string) ([]model.User, error) {
var users []model.User
err := r.DB.Preload("Role").Where("role_id = ?", roleID).Find(&users).Error
if err != nil {
return nil, err
}
return users, nil
}

View File

@ -24,6 +24,9 @@ type UserProfileService interface {
UpdateUserProfile(userID string, updateData dto.UpdateUserDTO) (*dto.UserResponseDTO, error) UpdateUserProfile(userID string, updateData dto.UpdateUserDTO) (*dto.UserResponseDTO, error)
UpdateUserPassword(userID string, passwordData dto.UpdatePasswordDTO) (string, error) UpdateUserPassword(userID string, passwordData dto.UpdatePasswordDTO) (string, error)
UpdateUserAvatar(userID string, file *multipart.FileHeader) (string, error) UpdateUserAvatar(userID string, file *multipart.FileHeader) (string, error)
GetAllUsers() ([]dto.UserResponseDTO, error)
GetUsersByRoleID(roleID string) ([]dto.UserResponseDTO, error)
} }
type userProfileService struct { type userProfileService struct {
@ -87,6 +90,56 @@ func (s *userProfileService) GetUserProfile(userID string) (*dto.UserResponseDTO
return userResponse, nil return userResponse, nil
} }
func (s *userProfileService) GetAllUsers() ([]dto.UserResponseDTO, error) {
users, err := s.UserProfileRepo.FindAll()
if err != nil {
return nil, err
}
var response []dto.UserResponseDTO
for _, user := range users {
response = append(response, dto.UserResponseDTO{
ID: user.ID,
Username: user.Username,
Avatar: user.Avatar,
Name: user.Name,
Phone: user.Phone,
Email: user.Email,
EmailVerified: user.EmailVerified,
RoleName: user.Role.RoleName,
CreatedAt: user.CreatedAt.Format(time.RFC3339),
UpdatedAt: user.UpdatedAt.Format(time.RFC3339),
})
}
return response, nil
}
func (s *userProfileService) GetUsersByRoleID(roleID string) ([]dto.UserResponseDTO, error) {
users, err := s.UserProfileRepo.FindByRoleID(roleID)
if err != nil {
return nil, err
}
var response []dto.UserResponseDTO
for _, user := range users {
response = append(response, dto.UserResponseDTO{
ID: user.ID,
Username: user.Username,
Avatar: user.Avatar,
Name: user.Name,
Phone: user.Phone,
Email: user.Email,
EmailVerified: user.EmailVerified,
RoleName: user.Role.RoleName,
CreatedAt: user.CreatedAt.Format(time.RFC3339),
UpdatedAt: user.UpdatedAt.Format(time.RFC3339),
})
}
return response, nil
}
func (s *userProfileService) UpdateUserProfile(userID string, updateData dto.UpdateUserDTO) (*dto.UserResponseDTO, error) { func (s *userProfileService) UpdateUserProfile(userID string, updateData dto.UpdateUserDTO) (*dto.UserResponseDTO, error) {
user, err := s.UserProfileRepo.FindByID(userID) user, err := s.UserProfileRepo.FindByID(userID)
if err != nil { if err != nil {

View File

@ -14,8 +14,15 @@ func UserProfileRouter(api fiber.Router) {
userProfileService := services.NewUserProfileService(userProfileRepo) userProfileService := services.NewUserProfileService(userProfileRepo)
userProfileHandler := handler.NewUserProfileHandler(userProfileService) userProfileHandler := handler.NewUserProfileHandler(userProfileService)
api.Get("/user", middleware.AuthMiddleware, userProfileHandler.GetUserProfile) userProfilRoute := api.Group("/user")
api.Put("/user/update-user", middleware.AuthMiddleware, userProfileHandler.UpdateUserProfile)
api.Patch("/user/update-user-password", middleware.AuthMiddleware, userProfileHandler.UpdateUserPassword) userProfilRoute.Get("/info", middleware.AuthMiddleware, userProfileHandler.GetUserProfile)
api.Patch("/user/upload-photoprofile", middleware.AuthMiddleware, userProfileHandler.UpdateUserAvatar)
userProfilRoute.Get("/show-all", middleware.AuthMiddleware, userProfileHandler.GetAllUsers)
userProfilRoute.Get("/:userid", middleware.AuthMiddleware, userProfileHandler.GetUserProfileById)
userProfilRoute.Get("/:roleid", middleware.AuthMiddleware, userProfileHandler.GetUsersByRoleID)
userProfilRoute.Put("/update-user", middleware.AuthMiddleware, userProfileHandler.UpdateUserProfile)
userProfilRoute.Patch("/update-user-password", middleware.AuthMiddleware, userProfileHandler.UpdateUserPassword)
userProfilRoute.Patch("/upload-photoprofile", middleware.AuthMiddleware, userProfileHandler.UpdateUserAvatar)
} }