Sectionen im Home hinzugefügt

This commit is contained in:
2023-11-15 21:30:45 +01:00
parent eac3fd9a9b
commit 57b217793d
5 changed files with 78 additions and 31 deletions

View File

@@ -2,6 +2,8 @@ package controllers
import (
"github.com/unrolled/render"
"log"
"moretcgshop/app/models"
"net/http"
)
@@ -11,9 +13,16 @@ func (server *Server) Home(w http.ResponseWriter, r *http.Request) {
Extensions: []string{".html", ".tmpl"},
})
sectionen, err := models.GetSection(server.DB)
if err != nil {
log.Fatal(err.Error())
}
user := server.CurrentUser(w, r)
_ = render.HTML(w, http.StatusOK, "home", map[string]interface{}{
"user": user,
"user": user,
"sectionen": sectionen,
})
}

View File

@@ -1,14 +1,37 @@
package models
import "time"
import (
"gorm.io/gorm"
"time"
)
type Category struct {
ID string
ParentID string
ID string `gorm:"size:36;not null;uniqueIndex;primary_key"`
ParentID string `gorm:"size:36;"`
Section Section
SectionID string
SectionID string `gorm:"size:36;index"`
Products []Product `gorm:"many2many:product_categories;"`
Name string
Name string `gorm:"size:100;"`
Image string
CreatedAt time.Time
UpdatedAt time.Time
}
func (p *Category) Get(db *gorm.DB, perPage int, page int) (*[]Product, int64, error) {
var count int64
var products []Product
err := db.Debug().Model(&Product{}).Count(&count).Error
if err != nil {
return nil, 0, err
}
offset := (page - 1) * perPage
err = db.Debug().Model(&Product{}).Order("created_at desc").Limit(perPage).Offset(offset).Find(&products).Error
if err != nil {
return nil, 0, err
}
return &products, count, nil
}