UPDATE: get role content
All checks were successful
Build and Release / release (push) Successful in 1m31s
All checks were successful
Build and Release / release (push) Successful in 1m31s
This commit is contained in:
73
internal/controllers/roleController.go
Normal file
73
internal/controllers/roleController.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/internal/services"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
type RoleController struct {
|
||||
service services.RoleService
|
||||
}
|
||||
|
||||
func NewRoleController(svc services.RoleService) *RoleController {
|
||||
return &RoleController{service: svc}
|
||||
}
|
||||
|
||||
// GetRoleById handles fetching a single role by ID.
|
||||
// @Summary Get role by ID
|
||||
// @Description Get detailed information about a specific role
|
||||
// @Tags Roles
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Role ID"
|
||||
// @Security ApiKeyAuth
|
||||
// @Success 200 {object} response.CommonResponse
|
||||
// @Failure 500 {object} response.CommonResponse
|
||||
// @Router /roles/{id} [get]
|
||||
func (h *RoleController) GetRoleById(c fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
RoleId := c.Params("id")
|
||||
res, err := h.service.GetRoleByID(ctx, RoleId)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
|
||||
Status: true,
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// GetAllRole handles fetching all roles.
|
||||
// @Summary Get all roles
|
||||
// @Description Get a list of all roles in the system
|
||||
// @Tags Roles
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security ApiKeyAuth
|
||||
// @Success 200 {object} response.CommonResponse
|
||||
// @Failure 500 {object} response.CommonResponse
|
||||
// @Router /roles [get]
|
||||
func (h *RoleController) GetAllRole(c fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
res, err := h.service.GetAllRole(ctx)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
|
||||
Status: true,
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
24
internal/routes/roleRoute.go
Normal file
24
internal/routes/roleRoute.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"history-api/internal/controllers"
|
||||
"history-api/internal/middlewares"
|
||||
"history-api/internal/repositories"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func RoleRoutes(app *fiber.App, controller *controllers.RoleController, userRepo repositories.UserRepository) {
|
||||
route := app.Group("/roles")
|
||||
route.Get(
|
||||
"/",
|
||||
middlewares.JwtAccess(userRepo),
|
||||
controller.GetAllRole,
|
||||
)
|
||||
|
||||
route.Get(
|
||||
"/:id",
|
||||
middlewares.JwtAccess(userRepo),
|
||||
controller.GetRoleById,
|
||||
)
|
||||
}
|
||||
51
internal/services/roleService.go
Normal file
51
internal/services/roleService.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/internal/models"
|
||||
"history-api/internal/repositories"
|
||||
"history-api/pkg/convert"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
type RoleService interface {
|
||||
GetRoleByID(ctx context.Context, id string) (*response.RoleResponse, error)
|
||||
GetAllRole(ctx context.Context) ([]*response.RoleResponse, error)
|
||||
}
|
||||
|
||||
type roleService struct {
|
||||
roleRepo repositories.RoleRepository
|
||||
}
|
||||
|
||||
|
||||
func NewRoleService(
|
||||
roleRepo repositories.RoleRepository,
|
||||
) RoleService {
|
||||
return &roleService{
|
||||
roleRepo: roleRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *roleService) GetAllRole(ctx context.Context) ([]*response.RoleResponse, error) {
|
||||
roles, err := r.roleRepo.All(ctx)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return models.RolesEntityToResponse(roles), nil
|
||||
}
|
||||
|
||||
func (r *roleService) GetRoleByID(ctx context.Context, id string) (*response.RoleResponse, error) {
|
||||
roleId, err := convert.StringToUUID(id)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
role, err := r.roleRepo.GetByID(ctx, roleId)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Role not found")
|
||||
}
|
||||
|
||||
return role.ToResponse(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user