github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/controllers/role/role.go (about)

     1  // Copyright 2023 IAC. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package role
    16  
    17  import (
    18  	"net/http"
    19  
    20  	"github.com/gin-gonic/gin"
    21  )
    22  
    23  type RoleController struct{}
    24  
    25  func (c *RoleController) List(ctx *gin.Context) {
    26  	// Retrieve a list of roles from the database
    27  	roles := []Role{ /* ... */ }
    28  
    29  	// Send the list of roles in the response
    30  	ctx.JSON(http.StatusOK, roles)
    31  }
    32  
    33  func (c *RoleController) Create(ctx *gin.Context) {
    34  	// Retrieve role data from the request body
    35  	var role Role
    36  	if err := ctx.BindJSON(&role); err != nil {
    37  		ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid role data"})
    38  		return
    39  	}
    40  
    41  	// Save the role data to the database
    42  	if err := SaveRole(&role); err != nil {
    43  		ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Failed to save role data"})
    44  		return
    45  	}
    46  
    47  	// Send the saved role data in the response
    48  	ctx.JSON(http.StatusOK, role)
    49  }
    50  
    51  func SaveRole(role *Role) error {
    52  	// Save the role data to the database
    53  	return nil
    54  }
    55  
    56  type Role struct {
    57  	ID   string `json:"id"`
    58  	Role string `json:"role"`
    59  }