Пример #1
0
// POST admin edit company
func AdminCompanyEdit(w http.ResponseWriter, r *http.Request, c *web.Context) {
	if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") {
		return
	}
	r.ParseForm()
	company := service.FindOneCompany(r.FormValue("id"))
	util.FormToStruct(&company, r.Form, "")
	service.SaveCompany(company)
	c.SetFlash("alertSuccess", "Successfully saved company")
	http.Redirect(w, r, "/admin/company/"+r.FormValue("id"), 303)
	return
}
Пример #2
0
// POST admin add company
func AdminCompanyAdd(w http.ResponseWriter, r *http.Request, c *web.Context) {
	if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") {
		return
	}
	r.ParseForm()
	var company service.Company
	util.FormToStruct(&company, r.Form, "")
	company.Id = util.UUID4()
	service.SaveCompany(company)
	c.SetFlash("alertSuccess", "Successfully saved company")
	http.Redirect(w, r, "/admin/company", 303)
	return
}
Пример #3
0
// POST admin upload company logo
func AdminUploadCompanyLogo(w http.ResponseWriter, r *http.Request, c *web.Context) {
	if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") {
		return
	}
	path := "static/logo/"
	if err := os.MkdirAll(path, 0755); err != nil {
		c.SetFlash("alertError", "Error uploading file")
		http.Redirect(w, r, "/admin/company/"+c.GetPathVar("id"), 303)
		return
	}
	r.ParseMultipartForm(32 << 20) // 32 MB
	file, handler, err := r.FormFile("logo")
	if err != nil || len(handler.Header["Content-Type"]) < 1 {
		fmt.Println(err)
		c.SetFlash("alertError", "Error uploading file")
		http.Redirect(w, r, "/admin/company/"+c.GetPathVar("id"), 303)
		return
	}
	defer file.Close()
	if handler.Header["Content-Type"][0] != "image/png" && handler.Header["Content-Type"][0] != "image/jpeg" {
		fmt.Println(err)
		c.SetFlash("alertError", "Error uploading file")
		http.Redirect(w, r, "/admin/company/"+c.GetPathVar("id"), 303)
		return
	}
	company := service.FindOneCompany(c.GetPathVar("id"))
	f, err := os.OpenFile(path+company.Id+".png", os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		fmt.Println(err)
		c.SetFlash("alertError", "Error uploading file")
		http.Redirect(w, r, "/admin/company/"+company.Id, 303)
		return
	}
	defer f.Close()
	io.Copy(f, file)
	company.Logo = true
	service.SaveCompany(company)
	c.SetFlash("alertSuccess", "Successfully uploaded file")
	http.Redirect(w, r, "/admin/company/"+company.Id, 303)
	return
}