Kategorien und Produkte können zur DB hinzugefügt werden.

This commit is contained in:
2023-11-20 15:51:42 +01:00
parent a87b0b3979
commit 5340963ac2
12 changed files with 330 additions and 19 deletions

View File

@@ -1,13 +1,96 @@
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",
@@ -55,12 +138,12 @@ func (server *Server) GetProductByID(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if vars["slug"] == "" {
if vars["id"] == "" {
return
}
productModel := models.Product{}
product, err := productModel.FindByID(server.DB, vars["slug"])
product, err := productModel.FindByID(server.DB, vars["id"])
if err != nil {
println(err.Error())
return