Esempio n. 1
0
func (this *categoryController) get(w http.ResponseWriter, req *http.Request) {

	_, er := req.Cookie("goSessionId")

	if er == nil {
		// This expects a map for the parameters caught by the current request
		vars := mux.Vars(req)

		idRaw := vars["id"]
		id, err := strconv.Atoi(idRaw)

		if err == nil && id < 4 {
			Products, _ := models.GetProductList(id)
			productsVM := []viewmodels.Product{}

			for _, modelProducts := range Products {
				productsVM = append(productsVM, converter.ConvertProductsToViewModel(modelProducts))
			}

			vm := viewmodels.GetProducts()
			vm.LoggedIn = true
			vm.Products = productsVM

			w.Header().Add("Content-Type", "text/html")

			responseWriter := util.GetResponseWriter(w, req)
			defer responseWriter.Close()
			this.template.Execute(responseWriter, vm)
		} else {
			w.WriteHeader(404)
		}
	} else {
		http.Redirect(w, req, "/home", http.StatusFound)
	}
}
Esempio n. 2
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 {
		category, err := models.GetCategoryById(id)

		if err == nil {

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

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

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

			vm.Products = productVMs

			this.template.Execute(responseWriter, vm)
		}
	} else {
		w.WriteHeader(404)
	}
}
Esempio n. 3
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 {
		vm := viewmodels.GetProducts(id)

		w.Header().Add("Content-Type", "text/html")
		this.template.Execute(w, vm)
	} else {
		w.WriteHeader(404)
	}
}