Ejemplo n.º 1
0
func (this *homeController) get(w http.ResponseWriter, req *http.Request) {
	vm := viewmodels.GetHome()
	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()
	this.template.Execute(responseWriter, vm)
}
Ejemplo 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 {
		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)
}
Ejemplo n.º 3
0
func (this *loginController) login(w http.ResponseWriter, req *http.Request) {
	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	if req.Method == "POST" {
		email := req.FormValue("email")
		password := req.FormValue("password")

		member, err := models.GetMember(email, password)
		if err != nil {
			responseWriter.Write([]byte(err.Error()))
			return
		}

		session, err := models.CreateSession(member)
		if err != nil {
			responseWriter.Write([]byte(err.Error()))
			return
		}

		var cookie http.Cookie
		cookie.Name = "sessionId"
		cookie.Value = session.SessionId()
		responseWriter.Header().Add("Set-Cookie", cookie.String())
	}
	vm := viewmodels.GetLogin()
	this.template.Execute(responseWriter, vm)
}
Ejemplo n.º 4
0
func (this *aboutController) get(w http.ResponseWriter, req *http.Request) {
	skills := models.GetSkills()
	skillsVM := []viewmodels.Skill{}

	for _, modelSkills := range skills {
		skillsVM = append(skillsVM, converter.ConvertSkillToViewModel(modelSkills))
	}

	vm := viewmodels.GetAbout()

	vm.Skills = skillsVM

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

	responseWriter := util.GetResponseWriter(w, req)

	_, err := req.Cookie("goSessionId")
	if err == nil {
		vm.LoggedIn = true
	} else {
		vm.LoggedIn = false
	}

	this.template.Execute(responseWriter, vm)
	defer responseWriter.Close()
}
Ejemplo n.º 5
0
func serveResource(w http.ResponseWriter, req *http.Request) {
	path := "public" + req.URL.Path
	var contentType string

	if strings.HasSuffix(path, ".css") {
		contentType = "text/css"
	} else if strings.HasSuffix(path, ".js") {
		contentType = "application/javascript"
	} else if strings.HasSuffix(path, ".png") {
		contentType = "image/png"
	} else if strings.HasSuffix(path, ".mp4") {
		contentType = "video/mp4"
	} else {
		contentType = "text/plain"
	}

	f, err := os.Open(path)
	if err != nil {
		w.WriteHeader(404)
		return
	}

	defer f.Close()
	w.Header().Add("Content-Type", contentType)

	// add gzip compression if possible
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	br := bufio.NewReader(f)
	br.WriteTo(responseWriter)
}
Ejemplo n.º 6
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)
	}
}
Ejemplo n.º 7
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)
}
Ejemplo n.º 8
0
func (this *homeController) get(w http.ResponseWriter, req *http.Request) {
	// Since this controller method is only responsible for a single VIEW, its implementation becomes really simple

	// Grab a view model that is preconfigured with MOCK data using the VIEW model GETHOME
	vm := viewmodels.GetHome()

	// Tell the browser that we will be sending HTML by setting the content-type header on the response
	w.Header().Add("Content-Type", "text/html")

	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	_, err := req.Cookie("goSessionId")
	if err == nil {
		vm.LoggedIn = true

		ckn, _ := req.Cookie("loggedName")

		vm.LoggedName = ckn.Value
	} else {
		vm.LoggedIn = false
	}

	/* We execute the controllers template field.
	This small/lite controlled funciton is important
	Due the controllers position in the MVC pattern it is aware of both of the other layer. This makes it easy to use the
	controller to handle things that really are the responsability of another portion of the application */
	this.template.Execute(responseWriter, vm)

}
Ejemplo n.º 9
0
func (this *standLocatorController) get(w http.ResponseWriter, req *http.Request) {
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	vm := viewmodels.GetStandLocator()
	responseWriter.Header().Add("Content-Type", "text/html")
	this.template.Execute(responseWriter, vm)
}
Ejemplo n.º 10
0
func (this *standLocatorController) apiSearch(w http.ResponseWriter, req *http.Request) {
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()
	responseWriter.Header().Add("Content-Type", "application/json")

	vm := viewmodels.GetStandLocations()
	data, err := json.Marshal(vm)
	if err == nil {
		responseWriter.Write(data)
	} else {
		responseWriter.WriteHeader(404)
	}
}
Ejemplo n.º 11
0
func (this *homeController) login(w http.ResponseWriter, req *http.Request) {

	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

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

	vm := viewmodels.GetLogin()

	if req.FormValue("submit") == "signup" {
		http.Redirect(w, req, "/signup", http.StatusFound)
	} else {

		if req.Method == "POST" {

			email := req.FormValue("email")
			password := req.FormValue("password")

			member, err := models.GetMember(email, password)

			if err == nil {
				session, err := models.CreateSession(member)

				if err == nil {
					var cookie http.Cookie
					cookie.Name = "goSessionId"
					cookie.Expires = time.Now().Add(10 * time.Minute)
					cookie.Value = strconv.Itoa(session.MemberId())
					responseWriter.Header().Add("Set-Cookie", cookie.String())

					var cookie2 http.Cookie
					cookie2.Name = "loggedName"
					cookie2.Expires = time.Now().Add(10 * time.Minute)
					cookie2.Value = member.FirstName()
					responseWriter.Header().Add("Set-Cookie", cookie2.String())
				}
				vmh := viewmodels.GetHome()

				vmh.LoggedIn = true
				vmh.LoggedName = member.FirstName()

				this.template.Execute(responseWriter, vmh)
			} else {
				this.loginTemplate.Execute(responseWriter, vm)
			}
		} else {
			this.loginTemplate.Execute(responseWriter, vm)
		}
	}
}
Ejemplo n.º 12
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 {
		vm := viewmodels.GetProduct(id)
		w.Header().Add("Content-Type", "text/html")
		responseWriter := util.GetResponseWriter(w, req)
		defer responseWriter.Close()
		this.template.Execute(responseWriter, vm)
	} else {
		w.WriteHeader(404)
	}
}
Ejemplo n.º 13
0
func (this *categoriesController) get(w http.ResponseWriter, req *http.Request) {
	categories := models.GetCategories()

	categoriesVM := []viewmodels.Category{}
	for _, category := range categories {
		categoriesVM = append(categoriesVM, converters.ConvertCategoryToViewModel(category))
	}

	vm := viewmodels.GetCategories()
	vm.Categories = categoriesVM

	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()
	this.template.Execute(responseWriter, vm)
}
Ejemplo n.º 14
0
func (this *profileController) handle(w http.ResponseWriter, req *http.Request) {
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	vm := viewmodels.GetProfile()
	if req.Method == "POST" {
		vm.User.Email = req.FormValue("email")
		vm.User.FirstName = req.FormValue("firstName")
		vm.User.LastName = req.FormValue("lastName")
		vm.User.Stand.Address = req.FormValue("standAddress")
		vm.User.Stand.City = req.FormValue("standCity")
		vm.User.Stand.County = req.FormValue("standCounty")
		vm.User.Stand.Postcode = req.FormValue("standPostcode")
	}
	responseWriter.Header().Add("Content-Type", "text/html")
	this.template.Execute(responseWriter, vm)
}
Ejemplo n.º 15
0
func (this *homeController) signup(w http.ResponseWriter, req *http.Request) {

	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

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

	if req.Method == "POST" {
		firstName := req.FormValue("firstName")
		email := req.FormValue("email")
		password := req.FormValue("password")

		err := models.InsertMember(firstName, email, password)

		if err == nil {
			member, _ := models.GetMember(email, password)

			member, errr := models.GetMember(email, password)

			if errr == nil {
				session, err := models.CreateSession(member)

				if err == nil {
					var cookie http.Cookie
					cookie.Name = "goSessionId"
					cookie.Expires = time.Now().Add(10 * time.Minute)
					cookie.Value = strconv.Itoa(session.MemberId())
					responseWriter.Header().Add("Set-Cookie", cookie.String())

					var cookie2 http.Cookie
					cookie2.Name = "loggedName"
					cookie2.Expires = time.Now().Add(10 * time.Minute)
					cookie2.Value = member.FirstName()
					responseWriter.Header().Add("Set-Cookie", cookie2.String())
				}

				http.Redirect(w, req, "/home", http.StatusFound)
			}
		}
	}

	vm := viewmodels.GetSignup()

	this.signupTemplate.Execute(responseWriter, vm)

}
Ejemplo n.º 16
0
func (this *homeController) get(w http.ResponseWriter, req *http.Request) {
	vm := viewmodels.GetHome()

	/*
	 *sessionCookie, err := req.Cookie("sessionId")
	 *if err == nil {
	 *    member, err := models.GetMemberBySessionId(sessionCookie.Value)
	 *    if err == nil {
	 *        vm.Member.IsLoggedIn = true
	 *        vm.Member.FirstName = member.FirstName()
	 *    }
	 *}
	 */

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

	this.template.Execute(responseWriter, vm)
}
Ejemplo n.º 17
0
func (this *productController) get(w http.ResponseWriter, req *http.Request) {
	ck, er := req.Cookie("goSessionId")

	if er == nil {
		vars := mux.Vars(req)
		idRaw := vars["id"]
		id, err := strconv.Atoi(idRaw)

		if req.Method == "POST" {
			userId, _ := strconv.Atoi(ck.Value)
			productId := id

			models.InsertOrder(userId, productId)

			http.Redirect(w, req, "/categories", http.StatusFound)
		} else {
			if err == nil {
				vm := viewmodels.GetProductVM()
				vm.LoggedIn = true

				modelProduct, _ := models.GetProduct(id)
				vm.Product = converter.ConvertProductsToViewModel(modelProduct)

				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)
	}

}
Ejemplo n.º 18
0
func (this *profileController) get(w http.ResponseWriter, req *http.Request) {
	vm := viewmodels.GetProfile()

	responseWriter := util.GetResponseWriter(w, req)

	ck, err := req.Cookie("goSessionId")

	if err == nil {
		vm.LoggedIn = true

		userId, _ := strconv.Atoi(ck.Value)
		modelMember, _ := models.GetMemberById(userId)
		vm.Member = converter.ConvertMemberlToViewModel(modelMember)

		if req.Method == "GET" {
			var listOfProductIds []int
			listOfProductIds, _ = models.GetMembersOrder(userId)

			var listOfProducts []viewmodels.Product
			for _, val := range listOfProductIds {
				pr, _ := models.GetProduct(val)
				listOfProducts = append(listOfProducts, converter.ConvertProductsToViewModel(pr))
			}

			vm.Products = listOfProducts
			vm.LoggedIn = true

		} else {
			remButton := req.FormValue("remove")
			remId, _ := strconv.Atoi(remButton)

			if remButton == "" {
				productName := req.FormValue("name")
				productType := req.FormValue("type")
				productDescription := req.FormValue("description")
				productPrice := req.FormValue("price")
				productImgUrl := req.FormValue("imageurl")

				_, fileErr := models.GetProductByName(productName)

				if fileErr != nil {
					inputProduct := models.Product{}
					inputProduct.SetName(productName)
					inputProduct.SetDescription(productDescription)
					inputProduct.SetImageUrl(productImgUrl)

					typ, _ := strconv.Atoi(productType)
					inputProduct.SetTyp(typ)

					price64, _ := strconv.ParseFloat(productPrice, 2)
					price := float32(price64)
					inputProduct.SetPrice(price)

					insertErr := models.InsertProduct(inputProduct)

					if insertErr == nil {
						http.Redirect(w, req, "/profile", http.StatusFound)
					} else {
						log.Fatal(insertErr.Error())
					}

				} else {
					http.Redirect(w, req, "/home", http.StatusFound)
				}
			} else {
				deleteErr := models.RemoveOrder(userId, remId)

				if deleteErr == nil {
					http.Redirect(w, req, "/profile", http.StatusFound)
				}
			}
		}

	} else {
		http.Redirect(w, req, "/login", http.StatusFound)
	}

	w.Header().Add("Content-Type", "text/html")
	this.template.Execute(responseWriter, vm)
	defer responseWriter.Close()
}