Skip to content

Commit

Permalink
add: EditOrder()
Browse files Browse the repository at this point in the history
ref: IndexOrders()
ref: ShowOrder()
  • Loading branch information
abbasfisal committed Nov 27, 2024
1 parent 241607d commit 090f7b7
Showing 1 changed file with 56 additions and 5 deletions.
61 changes: 56 additions & 5 deletions internal/modules/admin/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package handlers
import "C"
import (
"context"
errors2 "errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/spf13/viper"
"gorm.io/gorm"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -1902,28 +1904,38 @@ func (a AdminHandler) IndexOrders(c *gin.Context) {
orderPaginate, err := a.orderSrv.GetOrderPaginate(c)

if err != nil {
if errors2.Is(err, gorm.ErrRecordNotFound) {
html.Render(c, http.StatusFound, "admin_index_order", gin.H{
"TITLE": "لیست سفارشات",
"PRIMARY_MESSAGE": "سفارشی موجود نیست",
"PAGINATION": nil,
})
return
}
c.JSON(http.StatusBadRequest, gin.H{
"err": err,
})
return
}

html.Render(c, http.StatusFound, "admin_index_order", gin.H{
"TITLE": "لیست سفارشات",
"PAGINATION": orderPaginate,
})

return
}

func (a AdminHandler) ShowOrder(c *gin.Context) {

//get and convert order id from url
orderID, err := strconv.Atoi(c.Param("id"))
if err != nil {
sessions.Set(c, "message", "ID سفارش صحیح نمی باشد.")
c.Redirect(http.StatusFound, "/admins/orders")
return
}

//result, err := a.orderSrv.GetOrderBy(c, orderID)
orderRes, customerRes, err := a.orderSrv.GetOrderBy(c, orderID)
orderRes, err := a.orderSrv.GetOrderBy(c, orderID)
if err != nil {
sessions.Set(c, "message", err.Error())
c.Redirect(http.StatusFound, "/admins/orders")
Expand All @@ -1932,8 +1944,47 @@ func (a AdminHandler) ShowOrder(c *gin.Context) {

html.Render(c, http.StatusOK, "admin_show_order", gin.H{
"TITLE": "جزییات سفارش",
"Customer": customerRes,
"Data": orderRes,
"Customer": orderRes.Customer,
"Data": orderRes.Order,
})
return
}

func (a AdminHandler) EditOrder(c *gin.Context) {
//get order id from url
orderID, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.Redirect(http.StatusFound, "/admins/orders")
return
}

//bind request
var req requests.UpdateOrderStatus
bindErr := c.ShouldBind(&req)
if bindErr != nil {
c.JSON(200, gin.H{
"message": "bind error ",
"err": err,
})
return
}

url := fmt.Sprintf("/admins/orders/%d/details", orderID)

//اگه ادمین دیتای خالی ارسال کنه برای مدل سفارش اتفاقی نباید بیفته
if req.Note == "" && req.Status == -1 {
c.Redirect(http.StatusFound, url)
return
}

//update order
if err := a.orderSrv.ChangeOrderStatus(c, orderID, req); err != nil {
sessions.Set(c, "message", custom_error.UpdateOrderFaileds)
c.Redirect(http.StatusFound, url)
return
}

sessions.Set(c, "message", custom_error.SuccessfullyUpdated)
c.Redirect(http.StatusFound, url)
return
}

0 comments on commit 090f7b7

Please sign in to comment.