endpoint update page role settings

This commit is contained in:
renaldybrada
2026-02-25 14:32:22 +07:00
parent a22a7a1aeb
commit 42267aa1af
8 changed files with 332 additions and 4 deletions
+71
View File
@@ -166,6 +166,44 @@ const docTemplate = `{
}
}
}
},
"put": {
"tags": [
"Access Role"
],
"summary": "Update Role Page Settings",
"parameters": [
{
"type": "string",
"description": "id role",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Body Update Role Page",
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/access.UpdateRolePageRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/shared.BaseResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/shared.BaseErrorResponse"
}
}
}
}
},
"/access/sync-keycloak-role": {
@@ -978,6 +1016,10 @@ const docTemplate = `{
"definitions": {
"access.AccessPage": {
"type": "object",
"required": [
"id",
"is_active"
],
"properties": {
"id": {
"type": "string"
@@ -990,11 +1032,17 @@ const docTemplate = `{
},
"parent_id": {
"type": "string"
},
"sort": {
"type": "integer"
}
}
},
"access.DetailRolePageResponse": {
"type": "object",
"required": [
"id"
],
"properties": {
"access_page": {
"type": "array",
@@ -1066,6 +1114,29 @@ const docTemplate = `{
}
}
},
"access.UpdateRolePageRequest": {
"type": "object",
"required": [
"id"
],
"properties": {
"access_page": {
"type": "array",
"items": {
"$ref": "#/definitions/access.AccessPage"
}
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"status": {
"type": "boolean"
}
}
},
"access.UserRoleResponse": {
"type": "object",
"properties": {
+71
View File
@@ -160,6 +160,44 @@
}
}
}
},
"put": {
"tags": [
"Access Role"
],
"summary": "Update Role Page Settings",
"parameters": [
{
"type": "string",
"description": "id role",
"name": "id",
"in": "path",
"required": true
},
{
"description": "Body Update Role Page",
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/access.UpdateRolePageRequest"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/shared.BaseResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/shared.BaseErrorResponse"
}
}
}
}
},
"/access/sync-keycloak-role": {
@@ -972,6 +1010,10 @@
"definitions": {
"access.AccessPage": {
"type": "object",
"required": [
"id",
"is_active"
],
"properties": {
"id": {
"type": "string"
@@ -984,11 +1026,17 @@
},
"parent_id": {
"type": "string"
},
"sort": {
"type": "integer"
}
}
},
"access.DetailRolePageResponse": {
"type": "object",
"required": [
"id"
],
"properties": {
"access_page": {
"type": "array",
@@ -1060,6 +1108,29 @@
}
}
},
"access.UpdateRolePageRequest": {
"type": "object",
"required": [
"id"
],
"properties": {
"access_page": {
"type": "array",
"items": {
"$ref": "#/definitions/access.AccessPage"
}
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"status": {
"type": "boolean"
}
}
},
"access.UserRoleResponse": {
"type": "object",
"properties": {
+47
View File
@@ -10,6 +10,11 @@ definitions:
type: string
parent_id:
type: string
sort:
type: integer
required:
- id
- is_active
type: object
access.DetailRolePageResponse:
properties:
@@ -23,6 +28,8 @@ definitions:
type: string
status:
type: boolean
required:
- id
type: object
access.ListRolePermissionPaginateResponse:
properties:
@@ -59,6 +66,21 @@ definitions:
- keycloak_id
- name
type: object
access.UpdateRolePageRequest:
properties:
access_page:
items:
$ref: '#/definitions/access.AccessPage'
type: array
id:
type: string
name:
type: string
status:
type: boolean
required:
- id
type: object
access.UserRoleResponse:
properties:
email:
@@ -621,6 +643,31 @@ paths:
summary: Detail Role Page Settings
tags:
- Access Role
put:
parameters:
- description: id role
in: path
name: id
required: true
type: string
- description: Body Update Role Page
in: body
name: body
required: true
schema:
$ref: '#/definitions/access.UpdateRolePageRequest'
responses:
"200":
description: OK
schema:
$ref: '#/definitions/shared.BaseResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/shared.BaseErrorResponse'
summary: Update Role Page Settings
tags:
- Access Role
/access/sync-keycloak-role:
post:
parameters:
+34
View File
@@ -195,6 +195,40 @@ func (h AccessHandler) DetailRolePageSettings(c *gin.Context) {
))
}
// UpdateRolePageSettings godoc
// @Summary Update Role Page Settings
// @Tags Access Role
// @Param id path string true "id role"
// @Param body body UpdateRolePageRequest true "Body Update Role Page"
// @Success 200 {object} shared.BaseResponse
// @Failure 500 {object} shared.BaseErrorResponse
// @Router /access/role-permission/{id} [put]
func (h AccessHandler) UpdateRolePageSettings(c *gin.Context) {
var req UpdateRolePageRequest
//bind json body
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, shared.BaseErrorResponse{
Success: false,
Code: 400,
Message: "error bind json",
Errors: shared.ValidationError(err),
})
return
}
err := h.repo.UpdateRolePermission(c, req)
if err != nil {
c.JSON(500, shared.BaseErrorResponse{
Success: false,
Code: 500,
Message: err.Error(),
})
return
}
c.JSON(200,
shared.ToBaseResponse(
"", true, 200, "success update role permission",
))
}
+101 -1
View File
@@ -27,6 +27,7 @@ type IAccessRepository interface {
ListUserRole(c context.Context, q QueryListUserRole) (ListUserRolePaginateResponse, error)
ListRolePermission(c context.Context, q QueryListRolePermission) (ListRolePermissionPaginateResponse, error)
DetailRolePermission(c context.Context, permission_id string) (DetailRolePageResponse, error)
UpdateRolePermission(c context.Context, req UpdateRolePageRequest) error
}
type accessRepo struct {
@@ -36,7 +37,7 @@ type accessRepo struct {
func NewRepository(dbService database.Service) IAccessRepository {
queryBuilder := queryUtils.NewQueryBuilder(queryUtils.DBTypePostgreSQL).SetAllowedColumns([]string{
"id", "name", "is_active", "keycloak_id", "id_user", "id_permission", "email", "created_at",
"id", "name", "is_active", "keycloak_id", "id_user", "id_permission", "email", "created_at", "id_page",
}).SetAllowedTables([]string{TBL_USER})
queryBuilder.SetSecurityOptions(false, 100)
@@ -373,6 +374,7 @@ func (r accessRepo) DetailRolePermission(c context.Context, permission_id string
ID: p.ID,
Page: p.Name,
IsActive: isActive,
Sort: p.Sort,
ParentId: &p.ParentId.String,
})
}
@@ -380,6 +382,44 @@ func (r accessRepo) DetailRolePermission(c context.Context, permission_id string
return result, nil
}
func (r accessRepo) UpdateRolePermission(c context.Context, req UpdateRolePageRequest) error {
db, err := r.db.GetSQLXDB(DB_NAME)
if err != nil {
return err
}
// START TRANSACTION
tx, err := db.BeginTx(c, nil)
if err != nil {
return err
}
// delete all page permission by permission id
err = r.deletePageIdsByPermissionId(c, tx, req.ID)
if err != nil {
tx.Rollback()
return err
}
// select active page
var pageIds []string
for _, page := range req.AccessPage {
if page.IsActive {
pageIds = append(pageIds, page.ID)
}
}
// inserting new permission
err = r.insertPagePermission(c, tx, req.ID, pageIds)
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
// PRIVATE FUNCTIONS
// Table user function
@@ -698,6 +738,66 @@ func (r accessRepo) getPageIdsByPermissionIds(c context.Context, db *sqlx.DB, pe
return result, nil
}
func (r accessRepo) deletePageIdsByPermissionId(c context.Context, tx *sql.Tx, permissionId string) error {
filters := []queryUtils.FilterGroup{
{
Filters: []queryUtils.DynamicFilter{
{Column: "id_permission", Operator: queryUtils.OpEqual, Value: permissionId},
},
},
}
sql, args, err := r.queryBuilder.BuildDeleteQuery(TBL_PAGE_PERMISSION, filters)
if err != nil {
log.Printf("Unable to create delete page permission query : %v", err)
return err
}
_, err = tx.ExecContext(c, sql, args...)
if err != nil {
log.Printf("Unable to executing delete page permission : %v", err)
return err
}
return nil
}
func (r accessRepo) insertPagePermission(c context.Context, tx *sql.Tx, permissionId string, pageIds []string) error {
if len(pageIds) > 0 {
var valueUserPermission [][]interface{}
for _, pageId := range pageIds {
id := uuid.New().String()
itemValues := []interface{}{id, permissionId, pageId}
valueUserPermission = append(valueUserPermission, itemValues)
}
insertPagePermissionQuery := queryUtils.InsertBulkData{
Columns: []string{
"id", "id_permission", "id_page",
},
Values: valueUserPermission,
}
returningCols := []string{
"id",
}
sql, args, err := r.queryBuilder.BuildBulkInsertQuery(TBL_PAGE_PERMISSION, insertPagePermissionQuery, returningCols...)
if err != nil {
log.Printf("error building query insert page permission %s", err)
return err
}
_, err = tx.ExecContext(c, sql, args...)
if err != nil {
log.Println(err)
return err
}
log.Printf(sql, args)
log.Printf("success insert page permission")
}
return nil
}
// End role page permission functions
// Table pages functions
+2
View File
@@ -24,3 +24,5 @@ type QueryListRolePermission struct {
Limit int `form:"limit,default=10"`
Offset int `form:"offset,default=0"`
}
type UpdateRolePageRequest DetailRolePageResponse
+5 -3
View File
@@ -63,13 +63,15 @@ type ListRolePermissionPaginateResponse struct {
}
type AccessPage struct {
ID string `json:"id"`
ID string `json:"id" binding:"required"`
Page string `json:"page"`
IsActive bool `json:"is_active"`
IsActive bool `json:"is_active" binding:"required"`
Sort int `json:"sort"`
ParentId *string `json:"parent_id"`
}
type DetailRolePageResponse struct {
ID string `json:"id" db:"id"`
ID string `json:"id" db:"id" binding:"required"`
Name string `json:"name" db:"name"`
Status bool `json:"status" db:"is_active"`
AccessPage []AccessPage `json:"access_page"`
+1
View File
@@ -16,4 +16,5 @@ func RegisterRoutes(r *gin.RouterGroup, dbService database.Service) {
r.GET("/role-permission", accessHandler.ListRolePageSettings)
r.GET("/role-permission/:id", accessHandler.DetailRolePageSettings)
r.PUT("/role-permission/:id", accessHandler.UpdateRolePageSettings)
}