Add admin order management with full status workflow.
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"shop/internal/models"
|
||||
"shop/internal/orderstatus"
|
||||
)
|
||||
|
||||
type adminOrdersData struct {
|
||||
Title string
|
||||
Email string
|
||||
Orders []models.Order
|
||||
StatusList []orderStatusOption
|
||||
Success string
|
||||
}
|
||||
|
||||
type orderStatusOption struct {
|
||||
Code string
|
||||
Label string
|
||||
}
|
||||
|
||||
func statusOptions() []orderStatusOption {
|
||||
var list []orderStatusOption
|
||||
for _, code := range orderstatus.AllCodes {
|
||||
list = append(list, orderStatusOption{Code: code, Label: orderstatus.Label(code)})
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func (h *AdminHandler) OrderList(w http.ResponseWriter, r *http.Request) {
|
||||
email, ok := h.requireAuth(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
orders, err := h.orders.All(r.Context(), 200)
|
||||
if err != nil {
|
||||
log.Printf("admin orders: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
success := r.URL.Query().Get("ok")
|
||||
msg := ""
|
||||
if success == "1" {
|
||||
msg = "Статус заказа обновлён"
|
||||
}
|
||||
|
||||
h.render(w, "admin_orders.html", adminOrdersData{
|
||||
Title: "Заказы",
|
||||
Email: email,
|
||||
Orders: orders,
|
||||
StatusList: statusOptions(),
|
||||
Success: msg,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AdminHandler) OrderUpdateStatus(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := h.requireAuth(w, r); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
status := r.FormValue("status")
|
||||
if !orderstatus.Valid(status) {
|
||||
http.Redirect(w, r, "/admin/orders", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.orders.UpdateStatus(r.Context(), id, status); err != nil {
|
||||
log.Printf("order status: %v", err)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/admin/orders?ok=1", http.StatusSeeOther)
|
||||
}
|
||||
Reference in New Issue
Block a user