fixed
This commit is contained in:
264
backend/internal/admin/sub_category/handler.go
Executable file
264
backend/internal/admin/sub_category/handler.go
Executable file
@@ -0,0 +1,264 @@
|
||||
package sub_category
|
||||
|
||||
import (
|
||||
"bankline/internal/appresult"
|
||||
"bankline/internal/handlers"
|
||||
"bankline/internal/listing"
|
||||
"bankline/pkg/logging"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
getAllURL = ""
|
||||
getOneURL = "/:id"
|
||||
getEditURL = "/:id/edit"
|
||||
getStatusURL = "/:id/status"
|
||||
)
|
||||
|
||||
var validate *validator.Validate
|
||||
|
||||
type handler struct {
|
||||
repository Repository
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewHandler(repository Repository, logger *logging.Logger) handlers.Handler {
|
||||
return &handler{
|
||||
repository: repository,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
router.GET(getOneURL, h.GetAll)
|
||||
router.GET(getEditURL, h.GetOne)
|
||||
router.POST(getAllURL, h.Create)
|
||||
|
||||
router.PUT(getOneURL, h.Update)
|
||||
router.PUT(getStatusURL, h.UpdateStatus)
|
||||
router.DELETE(getOneURL, h.Delete)
|
||||
}
|
||||
|
||||
// GetAll godoc
|
||||
// @Description all sub-categories
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param limit query int false "limit"
|
||||
// @Param page query int false "page"
|
||||
// @Param search query string false "search term"
|
||||
// @Param sort query string false "sort column (id, status, createdAt)"
|
||||
// @Param order query string false "sort order (asc, desc)"
|
||||
// @Param id path int true "category id"
|
||||
// @Success 200 {object} DataDTO
|
||||
// @Router /admin/sub-categories/{id} [get]
|
||||
func (h *handler) GetAll(c *gin.Context) {
|
||||
categoryId := c.Param("id")
|
||||
|
||||
id, err := strconv.Atoi(categoryId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.repository.GetAllData(context.Background(), listing.FromQuery(c), id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = result
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne godoc
|
||||
// @Description one data category
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "sub-category id"
|
||||
// @Success 200 {object} SubCategoryDTO
|
||||
// @Router /admin/sub-categories/{id}/edit [get]
|
||||
func (h *handler) GetOne(c *gin.Context) {
|
||||
|
||||
categoryId := c.Param("id")
|
||||
id, err := strconv.Atoi(categoryId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.repository.GetData(context.Background(), id)
|
||||
if err != nil {
|
||||
appresult.ErrInternalServer.Err = err
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = result
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Create godoc
|
||||
// @Description create sub categories
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param sub-categories body AddSubCategoryDTO true "Create JSON"
|
||||
// @Success 200 {object} SubCategoryDTO
|
||||
// @Router /admin/sub-categories [post]
|
||||
func (h *handler) Create(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
reqDTO AddSubCategoryDTO
|
||||
)
|
||||
|
||||
if err = c.ShouldBindJSON(&reqDTO); err != nil {
|
||||
appresult.ErrMissingParam.Err = err
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := h.repository.AddData(context.Background(), reqDTO)
|
||||
|
||||
if err != nil {
|
||||
appresult.ErrInternalServer.Err = err
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = res
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
|
||||
// Update godoc
|
||||
// @Description update sub categories
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "sub-category id"
|
||||
// @Param sub-categories body UpdateSubCategoryDTO true "Create JSON"
|
||||
// @Success 200 {object} SubCategoryDTO
|
||||
// @Router /admin/sub-categories/{id} [put]
|
||||
func (h *handler) Update(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
reqDTO UpdateSubCategoryDTO
|
||||
res SubCategoryDTO
|
||||
)
|
||||
|
||||
categoryId := c.Param("id")
|
||||
id, err := strconv.Atoi(categoryId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err = c.ShouldBindJSON(&reqDTO); err != nil {
|
||||
appresult.ErrNotFound.Err = err
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("uuid :::", id)
|
||||
if id != 0 {
|
||||
res, err = h.repository.UpdateData(context.Background(), reqDTO, id)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
appresult.ErrInternalServer.Err = err
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = res
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus godoc
|
||||
// @Description update status sub-categories
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "sub-category id"
|
||||
// @Param status query bool true "status"
|
||||
// @Success 200 {object} SubCategoryDTO
|
||||
// @Router /admin/sub-categories/{id}/status [put]
|
||||
func (h *handler) UpdateStatus(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
res SubCategoryDTO
|
||||
)
|
||||
Id := c.Param("id")
|
||||
id, err := strconv.Atoi(Id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
status, _ := c.GetQuery("status")
|
||||
fmt.Println("id :::", id)
|
||||
if id != 0 {
|
||||
res, err = h.repository.UpdateStatusData(context.Background(), status, id)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = res
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete godoc
|
||||
// @Description delete sub-category
|
||||
// @Tags sub-categories
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "sub category id"
|
||||
// @Success 200 {string} string
|
||||
// @Router /admin/sub-categories/{id} [delete]
|
||||
func (h *handler) Delete(c *gin.Context) {
|
||||
|
||||
categoryId := c.Param("id")
|
||||
id, err := strconv.Atoi(categoryId)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.repository.DeleteData(context.Background(), id)
|
||||
if err != nil {
|
||||
appresult.ErrInternalServer.Err = err
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = ""
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user