first commit

This commit is contained in:
Beyhan Oğur
2026-04-26 21:43:40 +03:00
commit f34e54c5a5
100 changed files with 27342 additions and 0 deletions

View File

@@ -0,0 +1,169 @@
package handlers
import (
"gobeyhan/app/account/services"
"gobeyhan/database/models"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
type RoleHandler struct {
service *services.RoleService
}
func NewRoleHandler(service *services.RoleService) *RoleHandler {
return &RoleHandler{service: service}
}
// AdminGetAllRoles godoc
// @Summary Get all roles (Admin)
// @Description Get list of all roles with permissions
// @Tags admin,roles
// @Accept json
// @Produce json
// @Security BearerAuth
// @Success 200 {array} models.Role
// @Router /api/v1/admin/roles [get]
func (h *RoleHandler) AdminGetAllRoles(c *gin.Context) {
roles, err := h.service.GetAllRoles()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"data": roles})
}
// AdminGetRoleByID godoc
// @Summary Get role by ID (Admin)
// @Description Get a single role by ID
// @Tags admin,roles
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "Role ID"
// @Success 200 {object} models.Role
// @Router /api/v1/admin/roles/{id} [get]
func (h *RoleHandler) AdminGetRoleByID(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role ID"})
return
}
role, err := h.service.GetRoleByID(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if role == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Role not found"})
return
}
c.JSON(http.StatusOK, gin.H{"data": role})
}
// AdminCreateRole godoc
// @Summary Create a new role (Admin)
// @Description Create a new role
// @Tags admin,roles
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param role body models.Role true "Role object"
// @Success 201 {object} models.Role
// @Router /api/v1/admin/roles [post]
func (h *RoleHandler) AdminCreateRole(c *gin.Context) {
var input struct {
Name string `json:"name" binding:"required"`
Description string `json:"description"`
}
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
role := &models.Role{
Name: input.Name,
Description: input.Description,
}
if err := h.service.CreateRole(role); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"data": role})
}
// AdminUpdateRole godoc
// @Summary Update a role (Admin)
// @Description Update an existing role
// @Tags admin,roles
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "Role ID"
// @Param role body models.Role true "Role object"
// @Success 200 {object} models.Role
// @Router /api/v1/admin/roles/{id} [put]
func (h *RoleHandler) AdminUpdateRole(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role ID"})
return
}
var input map[string]interface{}
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := h.service.UpdateRole(id, input); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Fetch updated role
role, err := h.service.GetRoleByID(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"data": role})
}
// AdminDeleteRole godoc
// @Summary Delete a role (Admin)
// @Description Delete a role by ID
// @Tags admin,roles
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "Role ID"
// @Success 200 {object} map[string]string
// @Router /api/v1/admin/roles/{id} [delete]
func (h *RoleHandler) AdminDeleteRole(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role ID"})
return
}
if err := h.service.DeleteRole(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Role deleted successfully"})
}