161 lines
3.8 KiB
Go
161 lines
3.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"github.com/google/uuid"
|
|
"github.com/gorilla/mux"
|
|
"github.com/shopspring/decimal"
|
|
"github.com/unrolled/render"
|
|
"log"
|
|
"moretcgshop/app/models"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func (server *Server) AddProduct(w http.ResponseWriter, r *http.Request) {
|
|
renderer := render.New(render.Options{
|
|
Layout: "layout",
|
|
Extensions: []string{".html", ".tmpl"},
|
|
})
|
|
|
|
//http://localhost:9000/admin/product/add/7bfbead0-cbef-4eb1-bb8d-966359050047
|
|
vars := mux.Vars(r)
|
|
if vars["categoryID"] == "" {
|
|
println("CategoryID is null")
|
|
return
|
|
}
|
|
categoryID := vars["categoryID"]
|
|
|
|
categoryModel := &models.Category{}
|
|
|
|
category, err := categoryModel.GetCategoryByID(server.DB, categoryID)
|
|
if err != nil {
|
|
log.Fatalf(err.Error())
|
|
return
|
|
}
|
|
|
|
_ = renderer.HTML(w, http.StatusOK, "admin_productAdd", map[string]interface{}{
|
|
"category": category,
|
|
"error": GetFlash(w, r, "error"),
|
|
})
|
|
|
|
}
|
|
|
|
func (server *Server) DoAddProduct(w http.ResponseWriter, r *http.Request) {
|
|
name := r.FormValue("name")
|
|
price := r.FormValue("price")
|
|
shortdescription := r.FormValue("shortdescription")
|
|
description := r.FormValue("description")
|
|
categoryID := r.FormValue("categoryID")
|
|
|
|
if name == "" || price == "" || shortdescription == "" || description == "" {
|
|
SetFlash(w, r, "error", "Name,Preis, Kurze Beschreibung und Beschreibung sind pflicht Felder")
|
|
http.Redirect(w, r, "/admin/product/add/"+categoryID, http.StatusSeeOther)
|
|
return
|
|
}
|
|
|
|
productModel := models.Product{}
|
|
|
|
pr, err := decimal.NewFromString(price)
|
|
if err != nil {
|
|
SetFlash(w, r, "error", err.Error())
|
|
http.Redirect(w, r, "/admin/product/add/"+categoryID, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
params := &models.Product{
|
|
ID: uuid.New().String(),
|
|
Name: name,
|
|
ShortDescription: shortdescription,
|
|
Description: description,
|
|
CategoryID: categoryID,
|
|
Price: pr,
|
|
}
|
|
|
|
product, err := productModel.CreateProduct(server.DB, params)
|
|
if err != nil {
|
|
log.Println(err.Error())
|
|
SetFlash(w, r, "error", "Sorry Produkt konnte nicht hinzugefügt werden")
|
|
http.Redirect(w, r, "/admin/product/add/"+categoryID, http.StatusSeeOther)
|
|
return
|
|
}
|
|
|
|
_ = product
|
|
http.Redirect(w, r, "/admin/product/add/"+categoryID, http.StatusSeeOther)
|
|
}
|
|
|
|
func (server *Server) GetEditProduct(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
func (server *Server) DoEditProduct(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
func (server *Server) ProductsByCategory(w http.ResponseWriter, r *http.Request) {
|
|
renderer := render.New(render.Options{
|
|
Layout: "layout",
|
|
Extensions: []string{".html", ".tmpl"},
|
|
})
|
|
|
|
q := r.URL.Query()
|
|
|
|
page, _ := strconv.Atoi(q.Get("page"))
|
|
|
|
vars := mux.Vars(r)
|
|
if vars["id"] == "" {
|
|
return
|
|
}
|
|
|
|
categoryID := vars["id"]
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
|
|
perPage := 9
|
|
|
|
_ = perPage
|
|
productModel := models.Product{}
|
|
categoryModel := models.Category{}
|
|
|
|
category, _ := categoryModel.GetCategoryByID(server.DB, categoryID)
|
|
products, totalRows, err := productModel.GetProductsByCategory(server.DB, categoryID)
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
_ = totalRows
|
|
|
|
_ = renderer.HTML(w, http.StatusOK, "products", map[string]interface{}{
|
|
"products": products,
|
|
"category": category,
|
|
"user": server.CurrentUser(w, r),
|
|
})
|
|
}
|
|
|
|
func (server *Server) GetProductByID(w http.ResponseWriter, r *http.Request) {
|
|
renderer := render.New(render.Options{
|
|
Layout: "layout",
|
|
Extensions: []string{".html", ".tmpl"},
|
|
})
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
if vars["id"] == "" {
|
|
return
|
|
}
|
|
|
|
productModel := models.Product{}
|
|
product, err := productModel.FindByID(server.DB, vars["id"])
|
|
if err != nil {
|
|
println(err.Error())
|
|
return
|
|
}
|
|
|
|
_ = renderer.HTML(w, http.StatusOK, "product", map[string]interface{}{
|
|
"product": product,
|
|
"success": GetFlash(w, r, "success"),
|
|
"error": GetFlash(w, r, "error"),
|
|
})
|
|
}
|