Wird jetzt nach kategorien die Seiten angezeigt
This commit is contained in:
@@ -85,6 +85,7 @@ func GetFlash(w http.ResponseWriter, r *http.Request, name string) []string {
|
||||
|
||||
func (server *Server) Run(addr string) {
|
||||
fmt.Printf("Listening to port %s", addr)
|
||||
println("")
|
||||
log.Fatal(http.ListenAndServe(addr, server.Router))
|
||||
}
|
||||
|
||||
@@ -182,7 +183,7 @@ func (server *Server) routeInit() {
|
||||
server.Router.HandleFunc("/register", server.DoRegister).Methods("POST")
|
||||
server.Router.HandleFunc("/logout", server.Logout).Methods("GET")
|
||||
|
||||
server.Router.HandleFunc("/products/cat/{id}", server.Products).Methods("GET")
|
||||
server.Router.HandleFunc("/products/cat/{id}", server.ProductsByCategory).Methods("GET")
|
||||
server.Router.HandleFunc("/products/{slug}", server.GetProductByID).Methods("GET")
|
||||
|
||||
server.Router.HandleFunc("/section/{id}", server.Categories).Methods("GET")
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (server *Server) Products(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"},
|
||||
@@ -17,14 +17,24 @@ func (server *Server) Products(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
|
||||
page, _ := strconv.Atoi(q.Get("page"))
|
||||
|
||||
vars := mux.Vars(r)
|
||||
if vars["id"] == "" {
|
||||
return
|
||||
}
|
||||
|
||||
category := vars["id"]
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
println(category)
|
||||
perPage := 9
|
||||
|
||||
_ = perPage
|
||||
productModel := models.Product{}
|
||||
|
||||
products, totalRows, err := productModel.GetProducts(server.DB, perPage, page)
|
||||
products, totalRows, err := productModel.GetProductsByCategory(server.DB, category)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
@@ -7,15 +7,17 @@ import (
|
||||
)
|
||||
|
||||
type Product struct {
|
||||
ID string `gorm:"size:36;not null;uniqueIndex;primary_key"`
|
||||
ParentID string `gorm:"size:36;index"`
|
||||
Name string `gorm:"size:255"`
|
||||
Slug string `gorm:"size:255"`
|
||||
ProductImages []ProductImage
|
||||
Price decimal.Decimal `gorm:"type:decimal(16,2);"`
|
||||
Categories []Category `gorm:"many2many:product_categories;"`
|
||||
ShortDescription string `gorm:"type:text"`
|
||||
Description string `gorm:"type:text"`
|
||||
ID string `gorm:"size:36;not null;uniqueIndex;primary_key"`
|
||||
ParentID string `gorm:"size:36;index"`
|
||||
Name string `gorm:"size:255"`
|
||||
Slug string `gorm:"size:255"`
|
||||
ProductImages []ProductImage
|
||||
Price decimal.Decimal `gorm:"type:decimal(16,2);"`
|
||||
//Categories []Category `gorm:"many2many:product_categories;"`
|
||||
Category Category
|
||||
CategoryID string
|
||||
ShortDescription string `gorm:"type:text"`
|
||||
Description string `gorm:"type:text"`
|
||||
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
@@ -41,6 +43,19 @@ func (p *Product) GetProducts(db *gorm.DB, perPage int, page int) (*[]Product, i
|
||||
return &products, count, nil
|
||||
}
|
||||
|
||||
func (p *Product) GetProductsByCategory(db *gorm.DB, categoryID string) (*[]Product, int64, error) {
|
||||
var count int64
|
||||
var products []Product
|
||||
|
||||
err := db.Debug().Model(&Product{}).Order("created_at desc").Where("category_id = ?", categoryID).Find(&products).Count(&count).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return &products, count, nil
|
||||
|
||||
}
|
||||
|
||||
func (p *Product) FindByID(db *gorm.DB, productID string) (*Product, error) {
|
||||
var err error
|
||||
var product Product
|
||||
|
||||
@@ -44,6 +44,6 @@ func Run() {
|
||||
server.InitCommands(appConfig, dbConfig)
|
||||
} else {
|
||||
server.Initialize(appConfig, dbConfig)
|
||||
server.Run(":" + appConfig.AppPort)
|
||||
server.Run("127.0.0.1:" + appConfig.AppPort)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user