예제 #1
0
func (this *categoryController) get(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	idRaw := vars["id"]
	id, err := strconv.Atoi(idRaw)
	if err != nil {
		w.WriteHeader(404)
		return
	}

	category, err := models.GetCategoryById(id)
	if err != nil {
		w.WriteHeader(404)
		return
	}

	vm := viewmodels.GetCategoryPage(category.Title())
	productVMs := []viewmodels.Product{}

	for _, product := range category.Products() {
		productVMs = append(productVMs, converters.ConvertProductToViewModel(product))
	}
	vm.Products = productVMs

	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	this.template.Execute(responseWriter, vm)
}
예제 #2
0
func (this *productController) get(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	idRaw := vars["id"]
	id, err := strconv.Atoi(idRaw)
	if err != nil {
		w.WriteHeader(404)
		return
	}

	product, err := models.GetProductById(id)

	if err != nil {
		w.WriteHeader(404)
		return
	}

	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	vm := viewmodels.GetProductPage(product.Name())
	vm.Product = converters.ConvertProductToViewModel(product)
	this.template.Execute(responseWriter, vm)
}