feat: add feature get list role for admin user
This commit is contained in:
parent
c9525270a9
commit
35d51b99d0
|
@ -0,0 +1,8 @@
|
||||||
|
package dto
|
||||||
|
|
||||||
|
type RoleResponseDTO struct {
|
||||||
|
ID string `json:"role_id"`
|
||||||
|
RoleName string `json:"role_name"`
|
||||||
|
CreatedAt string `json:"createdAt"`
|
||||||
|
UpdatedAt string `json:"updatedAt"`
|
||||||
|
}
|
|
@ -1 +1,46 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/internal/services"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RoleHandler struct {
|
||||||
|
RoleService services.RoleService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRoleHandler(roleService services.RoleService) *RoleHandler {
|
||||||
|
return &RoleHandler{RoleService: roleService}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *RoleHandler) GetRoles(c *fiber.Ctx) error {
|
||||||
|
|
||||||
|
roleID, ok := c.Locals("roleID").(string)
|
||||||
|
if !ok || roleID != utils.RoleAdministrator {
|
||||||
|
return utils.GenericErrorResponse(c, fiber.StatusForbidden, "Forbidden: You don't have permission to access this resource")
|
||||||
|
}
|
||||||
|
|
||||||
|
roles, err := h.RoleService.GetRoles()
|
||||||
|
if err != nil {
|
||||||
|
return utils.GenericErrorResponse(c, fiber.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils.LogResponse(c, roles, "Roles fetched successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *RoleHandler) GetRoleByID(c *fiber.Ctx) error {
|
||||||
|
roleID := c.Params("role_id")
|
||||||
|
|
||||||
|
roleIDFromSession, ok := c.Locals("roleID").(string)
|
||||||
|
if !ok || roleIDFromSession != utils.RoleAdministrator {
|
||||||
|
return utils.GenericErrorResponse(c, fiber.StatusForbidden, "Forbidden: You don't have permission to access this resource")
|
||||||
|
}
|
||||||
|
|
||||||
|
role, err := h.RoleService.GetRoleByID(roleID)
|
||||||
|
if err != nil {
|
||||||
|
return utils.GenericErrorResponse(c, fiber.StatusNotFound, "role id tidak ditemukan")
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils.LogResponse(c, role, "Role fetched successfully")
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
type RoleRepository interface {
|
type RoleRepository interface {
|
||||||
FindByID(id string) (*model.Role, error)
|
FindByID(id string) (*model.Role, error)
|
||||||
|
FindAll() ([]model.Role, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type roleRepository struct {
|
type roleRepository struct {
|
||||||
|
@ -25,3 +26,12 @@ func (r *roleRepository) FindByID(id string) (*model.Role, error) {
|
||||||
}
|
}
|
||||||
return &role, nil
|
return &role, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *roleRepository) FindAll() ([]model.Role, error) {
|
||||||
|
var roles []model.Role
|
||||||
|
err := r.DB.Find(&roles).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return roles, nil
|
||||||
|
}
|
||||||
|
|
|
@ -1 +1,103 @@
|
||||||
package services
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pahmiudahgede/senggoldong/dto"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/internal/repositories"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RoleService interface {
|
||||||
|
GetRoles() ([]dto.RoleResponseDTO, error)
|
||||||
|
GetRoleByID(roleID string) (*dto.RoleResponseDTO, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type roleService struct {
|
||||||
|
RoleRepo repositories.RoleRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRoleService(roleRepo repositories.RoleRepository) RoleService {
|
||||||
|
return &roleService{RoleRepo: roleRepo}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roleService) GetRoles() ([]dto.RoleResponseDTO, error) {
|
||||||
|
|
||||||
|
cacheKey := "roles_list"
|
||||||
|
cachedData, err := utils.GetJSONData(cacheKey)
|
||||||
|
if err == nil && cachedData != nil {
|
||||||
|
var roles []dto.RoleResponseDTO
|
||||||
|
if data, ok := cachedData["data"].([]interface{}); ok {
|
||||||
|
for _, item := range data {
|
||||||
|
role, ok := item.(map[string]interface{})
|
||||||
|
if ok {
|
||||||
|
roles = append(roles, dto.RoleResponseDTO{
|
||||||
|
ID: role["role_id"].(string),
|
||||||
|
RoleName: role["role_name"].(string),
|
||||||
|
CreatedAt: role["createdAt"].(string),
|
||||||
|
UpdatedAt: role["updatedAt"].(string),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return roles, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
roles, err := s.RoleRepo.FindAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to fetch roles: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var roleDTOs []dto.RoleResponseDTO
|
||||||
|
for _, role := range roles {
|
||||||
|
createdAt, _ := utils.FormatDateToIndonesianFormat(role.CreatedAt)
|
||||||
|
updatedAt, _ := utils.FormatDateToIndonesianFormat(role.UpdatedAt)
|
||||||
|
|
||||||
|
roleDTOs = append(roleDTOs, dto.RoleResponseDTO{
|
||||||
|
ID: role.ID,
|
||||||
|
RoleName: role.RoleName,
|
||||||
|
CreatedAt: createdAt,
|
||||||
|
UpdatedAt: updatedAt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheData := map[string]interface{}{
|
||||||
|
"data": roleDTOs,
|
||||||
|
}
|
||||||
|
err = utils.SetJSONData(cacheKey, cacheData, time.Hour*24)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error caching roles data to Redis: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return roleDTOs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roleService) GetRoleByID(roleID string) (*dto.RoleResponseDTO, error) {
|
||||||
|
|
||||||
|
role, err := s.RoleRepo.FindByID(roleID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("role not found: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
createdAt, _ := utils.FormatDateToIndonesianFormat(role.CreatedAt)
|
||||||
|
updatedAt, _ := utils.FormatDateToIndonesianFormat(role.UpdatedAt)
|
||||||
|
|
||||||
|
roleDTO := &dto.RoleResponseDTO{
|
||||||
|
ID: role.ID,
|
||||||
|
RoleName: role.RoleName,
|
||||||
|
CreatedAt: createdAt,
|
||||||
|
UpdatedAt: updatedAt,
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheKey := fmt.Sprintf("role:%s", roleID)
|
||||||
|
cacheData := map[string]interface{}{
|
||||||
|
"data": roleDTO,
|
||||||
|
}
|
||||||
|
err = utils.SetJSONData(cacheKey, cacheData, time.Hour*24)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error caching role data to Redis: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return roleDTO, nil
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
// presentation/role_route.go
|
||||||
|
package presentation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/config"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/internal/handler"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/internal/repositories"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/internal/services"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/middleware"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RoleRouter(api fiber.Router) {
|
||||||
|
roleRepo := repositories.NewRoleRepository(config.DB)
|
||||||
|
roleService := services.NewRoleService(roleRepo)
|
||||||
|
roleHandler := handler.NewRoleHandler(roleService)
|
||||||
|
|
||||||
|
api.Get("/roles", middleware.AuthMiddleware, middleware.RoleMiddleware(utils.RoleAdministrator), roleHandler.GetRoles)
|
||||||
|
api.Get("/role/:role_id", middleware.AuthMiddleware, middleware.RoleMiddleware(utils.RoleAdministrator), roleHandler.GetRoleByID)
|
||||||
|
}
|
|
@ -12,4 +12,5 @@ func SetupRoutes(app *fiber.App) {
|
||||||
presentation.AuthRouter(api)
|
presentation.AuthRouter(api)
|
||||||
presentation.UserProfileRouter(api)
|
presentation.UserProfileRouter(api)
|
||||||
presentation.UserPinRouter(api)
|
presentation.UserPinRouter(api)
|
||||||
|
presentation.RoleRouter(api)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue