247 lines
5.8 KiB
Go
Executable File
247 lines
5.8 KiB
Go
Executable File
package category
|
|
|
|
import (
|
|
"bankline/internal/appresult"
|
|
"bankline/internal/handlers"
|
|
"bankline/internal/listing"
|
|
"bankline/pkg/logging"
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const (
|
|
getAllURL = ""
|
|
getOneURL = "/:id"
|
|
getStatusURL = "/:id/status"
|
|
)
|
|
|
|
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(getAllURL, h.GetAll)
|
|
router.GET(getOneURL, 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 data categories
|
|
// @Tags 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)"
|
|
// @Success 200 {object} DataCategoryDTO
|
|
// @Router /admin/categories [get]
|
|
func (h *handler) GetAll(c *gin.Context) {
|
|
result, err := h.repository.GetAllData(context.Background(), listing.FromQuery(c))
|
|
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 categories
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "category ID"
|
|
// @Success 200 {object} CategoryDTO
|
|
// @Router /admin/categories/{id} [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 {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = result
|
|
c.JSON(http.StatusOK, successResult)
|
|
|
|
return
|
|
}
|
|
|
|
// Create godoc
|
|
// @Description create categories
|
|
// @Tags categories
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param categories body AddCategoryDTO true "Create JSON"
|
|
// @Success 200 {string} string
|
|
// @Router /admin/categories [post]
|
|
func (h *handler) Create(c *gin.Context) {
|
|
var (
|
|
err error
|
|
reqDTO AddCategoryDTO
|
|
)
|
|
|
|
if err = c.ShouldBindJSON(&reqDTO); err != nil {
|
|
c.JSON(http.StatusNotFound, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
res, err := h.repository.AddData(context.Background(), reqDTO)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrNotAcceptable)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = res
|
|
c.JSON(http.StatusOK, successResult)
|
|
return
|
|
}
|
|
|
|
// Update godoc
|
|
// @Description update categories
|
|
// @Tags categories
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "category ID"
|
|
// @Param categories body UpdateCategoryDTO true "Create JSON"
|
|
// @Success 200 {object} CategoryIdDTO
|
|
// @Router /admin/categories/{id} [put]
|
|
func (h *handler) Update(c *gin.Context) {
|
|
var (
|
|
err error
|
|
reqDTO UpdateCategoryDTO
|
|
res CategoryIdDTO
|
|
)
|
|
|
|
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 {
|
|
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 {
|
|
fmt.Println("err :::", err)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = res
|
|
c.JSON(http.StatusOK, successResult)
|
|
return
|
|
}
|
|
|
|
// UpdateStatus godoc
|
|
// @Description update status categories
|
|
// @Tags categories
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "category id"
|
|
// @Param status query bool true "status"
|
|
// @Success 200 {object} CategoryDTO
|
|
// @Router /admin/categories/{id}/status [put]
|
|
func (h *handler) UpdateStatus(c *gin.Context) {
|
|
var (
|
|
err error
|
|
res CategoryDTO
|
|
)
|
|
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 category
|
|
// @Tags categories
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "category ID"
|
|
// @Success 200 {string} string
|
|
// @Router /admin/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 {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
successResult := appresult.Success
|
|
successResult.Data = ""
|
|
c.JSON(http.StatusOK, successResult)
|
|
return
|
|
}
|