Categoriecontroller added

This commit is contained in:
2023-11-19 12:27:53 +01:00
parent 57b217793d
commit fa3132f61d
8 changed files with 79 additions and 5 deletions

View File

@@ -185,6 +185,8 @@ func (server *Server) routeInit() {
server.Router.HandleFunc("/products", server.Products).Methods("GET")
server.Router.HandleFunc("/products/{slug}", server.GetProductByID).Methods("GET")
server.Router.HandleFunc("/section/{id}", server.SelectSection).Methods("GET")
staticFileDirectory := http.Dir("./assets/")
staticFileHandler := http.StripPrefix("/public/", http.FileServer(staticFileDirectory))
server.Router.PathPrefix("/public/").Handler(staticFileHandler).Methods("GET")

View File

@@ -0,0 +1,25 @@
package controllers
import (
"github.com/gorilla/mux"
"github.com/unrolled/render"
"net/http"
)
func (server *Server) Categories(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
}
_ = renderer.HTML(w, http.StatusOK, "home", map[string]interface{}{
//"user": user,
//"sectionen": sectionen,
})
}

View File

@@ -8,7 +8,7 @@ import (
)
func (server *Server) Home(w http.ResponseWriter, r *http.Request) {
render := render.New(render.Options{
renderer := render.New(render.Options{
Layout: "layout",
Extensions: []string{".html", ".tmpl"},
})
@@ -21,7 +21,7 @@ func (server *Server) Home(w http.ResponseWriter, r *http.Request) {
user := server.CurrentUser(w, r)
_ = render.HTML(w, http.StatusOK, "home", map[string]interface{}{
_ = renderer.HTML(w, http.StatusOK, "home", map[string]interface{}{
"user": user,
"sectionen": sectionen,
})

View File

@@ -1,6 +1,9 @@
package models
import "time"
import (
"gorm.io/gorm"
"time"
)
type Section struct {
ID string
@@ -9,3 +12,12 @@ type Section struct {
UpdatedAt time.Time
Categories []Category
}
func GetSection(db *gorm.DB) ([]Section, error) {
var sections []Section
err := db.Debug().Model(&Section{}).Order("created_at desc").Find(&sections).Error
if err != nil {
return nil, err
}
return sections, nil
}