Kategorien und Produkte können zur DB hinzugefügt werden.
This commit is contained in:
@@ -184,10 +184,18 @@ func (server *Server) routeInit() {
|
||||
server.Router.HandleFunc("/logout", server.Logout).Methods("GET")
|
||||
|
||||
server.Router.HandleFunc("/products/cat/{id}", server.ProductsByCategory).Methods("GET")
|
||||
server.Router.HandleFunc("/products/{slug}", server.GetProductByID).Methods("GET")
|
||||
server.Router.HandleFunc("/products/{id}", server.GetProductByID).Methods("GET")
|
||||
|
||||
server.Router.HandleFunc("/section/{id}", server.Categories).Methods("GET")
|
||||
|
||||
server.Router.HandleFunc("/admin/category/{spielid}", server.AdminCategoryList).Methods("GET")
|
||||
server.Router.HandleFunc("/admin/category", server.AdminCategoryPost).Methods("POST")
|
||||
|
||||
server.Router.HandleFunc("/admin/product/add/{categoryID}", server.AddProduct).Methods("GET")
|
||||
server.Router.HandleFunc("/admin/product", server.DoAddProduct).Methods("POST")
|
||||
server.Router.HandleFunc("/admin/product/{productID}", server.AddProduct).Methods("GET")
|
||||
server.Router.HandleFunc("/admin/product/{productID}", server.DoEditProduct).Methods("POST")
|
||||
|
||||
staticFileDirectory := http.Dir("./assets/")
|
||||
staticFileHandler := http.StripPrefix("/public/", http.FileServer(staticFileDirectory))
|
||||
server.Router.PathPrefix("/public/").Handler(staticFileHandler).Methods("GET")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/unrolled/render"
|
||||
"moretcgshop/app/models"
|
||||
@@ -19,9 +20,9 @@ func (server *Server) Categories(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
categorieModel := models.Category{}
|
||||
categoryModel := models.Category{}
|
||||
|
||||
categorien, err := categorieModel.GetCategorie(server.DB, vars["id"])
|
||||
categories, err := categoryModel.GetCategory(server.DB, vars["id"])
|
||||
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
@@ -29,7 +30,63 @@ func (server *Server) Categories(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
_ = renderer.HTML(w, http.StatusOK, "categories", map[string]interface{}{
|
||||
//"user": user,
|
||||
"categorien": categorien,
|
||||
"categories": categories,
|
||||
})
|
||||
}
|
||||
|
||||
func (server *Server) AdminCategoryList(w http.ResponseWriter, r *http.Request) {
|
||||
renderer := render.New(render.Options{
|
||||
Layout: "layout",
|
||||
Extensions: []string{".html", ".tmpl"},
|
||||
})
|
||||
|
||||
vars := mux.Vars(r)
|
||||
if vars["spielid"] == "" {
|
||||
println("spielid is leer")
|
||||
return
|
||||
}
|
||||
|
||||
section, err := models.GetSectionByID(server.DB, vars["spielid"])
|
||||
|
||||
categoryModel := models.Category{}
|
||||
categories, err := categoryModel.GetCategory(server.DB, vars["spielid"])
|
||||
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_ = renderer.HTML(w, http.StatusOK, "admin_categoryList", map[string]interface{}{
|
||||
"categories": categories,
|
||||
"spiel": section,
|
||||
})
|
||||
}
|
||||
|
||||
func (server *Server) AdminCategoryPost(w http.ResponseWriter, r *http.Request) {
|
||||
kategorieName := r.FormValue("katname")
|
||||
spielID := r.FormValue("spielid")
|
||||
|
||||
if kategorieName == "" {
|
||||
SetFlash(w, r, "error", "Kategoriename wurde nicht eingetragen")
|
||||
http.Redirect(w, r, "/admin/category/"+spielID, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
kategorieModel := &models.Category{}
|
||||
|
||||
params := &models.Category{
|
||||
ID: uuid.New().String(),
|
||||
Name: kategorieName,
|
||||
SectionID: spielID,
|
||||
}
|
||||
|
||||
_, err := kategorieModel.CreateCategory(server.DB, params)
|
||||
if err != nil {
|
||||
SetFlash(w, r, "error", "Sorry das war nicht erfolgreich")
|
||||
http.Redirect(w, r, "/admin/category/"+spielID, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/admin/category/"+spielID, http.StatusSeeOther)
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user