This commit is contained in:
Amir Tavakolian 2026-05-24 08:47:20 +03:30
parent 8aad672795
commit df16e37860
2 changed files with 29 additions and 19 deletions

View File

@ -3,7 +3,7 @@ package http
import (
"fmt"
"git.gocasts.ir/ebhomengo/niki/domain/authorization/service"
"github.com/gin-gonic/gin"
"github.com/labstack/echo/v4"
"net/http"
)
@ -15,36 +15,30 @@ func NewRoleHandler(service service.Authorization) RoleHandler {
return RoleHandler{service: service}
}
func (r RoleHandler) Store(c *gin.Context) {
func (r RoleHandler) Store(c echo.Context) error {
var request service.StoreRoleRequest
err := c.ShouldBindJSON(&request)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
if err := c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
}
role, err := r.service.Store(c, request)
role, err := r.service.Store(c.Request().Context(), request)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
}
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("role %s created successfully", role.Title_fa)})
return c.JSON(http.StatusOK, echo.Map{"message": fmt.Sprintf("role %s created successfully", role.Title_fa)})
}
func (r RoleHandler) Update(c *gin.Context) {
func (r RoleHandler) Update(c echo.Context) error {
var request service.UpdateRoleRequest
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
if err := c.Bind(&request); err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
}
role, err := r.service.Update(c, request)
role, err := r.service.Update(c.Request().Context(), request)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
return c.JSON(http.StatusBadRequest, echo.Map{"error": err.Error()})
}
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("role %s updated successfully", role.Title_fa)})
return c.JSON(http.StatusOK, echo.Map{"message": fmt.Sprintf("role %s updated successfully", role.Title_fa)})
}

View File

@ -1,7 +1,10 @@
package http
import (
"fmt"
"git.gocasts.ir/ebhomengo/niki/pkg/httpserver"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type RoleServer struct {
@ -14,5 +17,18 @@ func NewRoleServer(cfg httpserver.Config, handler RoleHandler) RoleServer {
}
func (s RoleServer) Start() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
group := e.Group("/role")
group.POST("/create", s.handler.Store)
group.PUT("/update/:id", s.handler.Update)
address := fmt.Sprintf("%s:%s", s.cfg.Host, s.cfg.Port)
err := e.Start(address)
if err != nil {
e.Logger.Fatal(err)
}
}