From eb404b37e925800655bcfb61c4779d1aecb035dd Mon Sep 17 00:00:00 2001 From: AzenKain Date: Sun, 5 Apr 2026 00:22:34 +0700 Subject: [PATCH] UPDATE: get role content --- cmd/api/server.go | 3 + docs/docs.go | 77 ++++++++++++++++++++++++++ docs/swagger.json | 77 ++++++++++++++++++++++++++ docs/swagger.yaml | 48 ++++++++++++++++ internal/controllers/roleController.go | 73 ++++++++++++++++++++++++ internal/routes/roleRoute.go | 24 ++++++++ internal/services/roleService.go | 51 +++++++++++++++++ 7 files changed, 353 insertions(+) create mode 100644 internal/controllers/roleController.go create mode 100644 internal/routes/roleRoute.go create mode 100644 internal/services/roleService.go diff --git a/cmd/api/server.go b/cmd/api/server.go index 085e67f..780c17e 100644 --- a/cmd/api/server.go +++ b/cmd/api/server.go @@ -79,16 +79,19 @@ func (s *FiberServer) SetupServer(sqlPg sqlc.DBTX, sqlTile *sql.DB, redis cache. // service setup authService := services.NewAuthService(userRepo, roleRepo, tokenRepo, redis) userService := services.NewUserService(userRepo, roleRepo) + roleService := services.NewRoleService(roleRepo) tileService := services.NewTileService(tileRepo) // controller setup authController := controllers.NewAuthController(authService, oauth) userController := controllers.NewUserController(userService) tileController := controllers.NewTileController(tileService) + roleController := controllers.NewRoleController(roleService) // route setup routes.AuthRoutes(s.App, authController, userRepo) routes.UserRoutes(s.App, userController, userRepo) + routes.RoleRoutes(s.App, roleController, userRepo) routes.TileRoutes(s.App, tileController) routes.NotFoundRoute(s.App) } diff --git a/docs/docs.go b/docs/docs.go index 7528405..aff6d05 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -362,6 +362,83 @@ const docTemplate = `{ } } }, + "/roles": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Get a list of all roles in the system", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Roles" + ], + "summary": "Get all roles", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse" + } + } + } + } + }, + "/roles/{id}": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Get detailed information about a specific role", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Roles" + ], + "summary": "Get role by ID", + "parameters": [ + { + "type": "string", + "description": "Role ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse" + } + } + } + } + }, "/tiles/metadata": { "get": { "description": "Retrieve map metadata", diff --git a/docs/swagger.json b/docs/swagger.json index 776e271..62a9d45 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -355,6 +355,83 @@ } } }, + "/roles": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Get a list of all roles in the system", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Roles" + ], + "summary": "Get all roles", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse" + } + } + } + } + }, + "/roles/{id}": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Get detailed information about a specific role", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Roles" + ], + "summary": "Get role by ID", + "parameters": [ + { + "type": "string", + "description": "Role ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse" + } + } + } + } + }, "/tiles/metadata": { "get": { "description": "Retrieve map metadata", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 60e0079..0752bc2 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -398,6 +398,54 @@ paths: summary: Verify a security token tags: - Auth + /roles: + get: + consumes: + - application/json + description: Get a list of all roles in the system + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/history-api_internal_dtos_response.CommonResponse' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/history-api_internal_dtos_response.CommonResponse' + security: + - ApiKeyAuth: [] + summary: Get all roles + tags: + - Roles + /roles/{id}: + get: + consumes: + - application/json + description: Get detailed information about a specific role + parameters: + - description: Role ID + in: path + name: id + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/history-api_internal_dtos_response.CommonResponse' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/history-api_internal_dtos_response.CommonResponse' + security: + - ApiKeyAuth: [] + summary: Get role by ID + tags: + - Roles /tiles/{z}/{x}/{y}: get: description: Fetch vector or raster map tile data by Z, X, Y coordinates diff --git a/internal/controllers/roleController.go b/internal/controllers/roleController.go new file mode 100644 index 0000000..6f2961f --- /dev/null +++ b/internal/controllers/roleController.go @@ -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, + }) +} diff --git a/internal/routes/roleRoute.go b/internal/routes/roleRoute.go new file mode 100644 index 0000000..201b233 --- /dev/null +++ b/internal/routes/roleRoute.go @@ -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, + ) +} diff --git a/internal/services/roleService.go b/internal/services/roleService.go new file mode 100644 index 0000000..505fdc8 --- /dev/null +++ b/internal/services/roleService.go @@ -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 +} \ No newline at end of file