// GET admin get new vehicle page func AdminCompanyVehicleNew(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } msgK, msgV := c.GetFlash() ts.Render(w, "admin-company-vehicle-form.tmpl", tmpl.Model{ msgK: msgV, "vehicles": service.FindAllVehicleByCompany(c.GetPathVar("companyId")), "company": service.FindOneCompany(c.GetPathVar("companyId")), }) }
// GET admin get all company's drivers func AdminCompanyDriverGetAll(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } msgK, msgV := c.GetFlash() ts.Render(w, "admin-company-driver.tmpl", tmpl.Model{ msgK: msgV, "drivers": service.FindAllDriverByCompany(c.GetPathVar("companyId")), "company": service.FindOneCompany(c.GetPathVar("companyId")), }) return }
func AdminDriverDocumentGetOne(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } driver := service.FindOneDriver(c.GetPathVar("driverId")) document := service.FindOneDocument(c.GetPathVar("documentId")) ts.Render(w, document.Name+".tmpl", tmpl.Model{ "driver": driver, "company": service.FindOneCompany(driver.CompanyId), "document": document, "admin": true, }) }
// GET admin get company func AdminCompanyGetOne(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/login", "admin", "employee", "developer") { return } msgK, msgV := c.GetFlash() ts.Render(w, "admin-company.tmpl", tmpl.Model{ msgK: msgV, "company": service.FindOneCompany(c.GetPathVar("id")), "driverCount": service.GetDriverCount(c.GetPathVar("id")), "vehicleCount": service.GetVehicleCount(c.GetPathVar("id")), }) return }
// 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, "") if !service.CanUpdateCompany(company.Id, company.Name) { c.SetFlash("alertError", "Error company name already in use") http.Redirect(w, r, "/admin/company/"+r.FormValue("id"), 303) return } service.SaveCompany(company) c.SetFlash("alertSuccess", "Successfully saved company") http.Redirect(w, r, "/admin/company/"+r.FormValue("id"), 303) return }
// GET driver get document func getDocument(w http.ResponseWriter, r *http.Request, c *web.Context) { if !c.CheckAuth(w, r, "/"+c.GetPathVar("slug"), "driver") { return } if c.GetFromSession("slug") != c.GetPathVar("slug") { http.Redirect(w, r, "/"+c.GetFromSession("slug").(string)+"/document/"+c.GetPathVar("id"), 303) } document := service.FindOneDocument(c.GetPathVar("id")) driver := service.FindOneDriver(c.GetFromSession("id").(string)) if document.DriverId != driver.Id { http.Redirect(w, r, "/"+c.GetFromSession("slug").(string)+"/driver/", 303) return } ts.Render(w, document.Name+".tmpl", tmpl.Model{ "driver": driver, "company": service.FindOneCompany(c.GetFromSession("companyId").(string)), "document": document, }) }
// 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 }