func (pc *partController) GetSearch(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	makeId, _ := strconv.Atoi(vars["makeId"])
	modelId, _ := strconv.Atoi(vars["modelId"])
	yearId, _ := strconv.Atoi(vars["yearId"])
	engineId, _ := strconv.Atoi(vars["engineId"])
	categories, err := model.GetPartCategories()

	if err != nil {
		log.Println(err)
		w.WriteHeader(500)
	} else {
		employeeNumber, _ := strconv.Atoi(vars["employeeNumber"])
		employee, _ := model.GetEmployee(employeeNumber)
		autoMake, _ := model.GetMake(makeId)
		autoModel, _ := model.GetModel(modelId)
		autoYear, _ := model.GetYear(yearId)
		autoEngine, _ := model.GetEngine(engineId)

		categoriesJSON, _ := json.Marshal(categories)

		vmodel := vm.SearchResult{Base: vm.Base{Employee: employee},
			Make:           autoMake,
			Model:          autoModel,
			Year:           autoYear,
			Engine:         autoEngine,
			CategoriesJSON: string(categoriesJSON),
		}

		pc.searchResultTemplate.Execute(w, vmodel)
	}
}
func (pc *partController) GetEngine(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	makeId, _ := strconv.Atoi(vars["makeId"])

	modelId, _ := strconv.Atoi(vars["modelId"])

	yearId, _ := strconv.Atoi(vars["yearId"])

	engines, err := model.SearchForEngines(modelId, yearId)
	if err != nil {
		log.Println(err)
		w.WriteHeader(500)
	} else {
		employeeNumber, _ := strconv.Atoi(vars["employeeNumber"])
		if err != nil {
			log.Println(err)
			w.WriteHeader(500)
		} else {
			employee, _ := model.GetEmployee(employeeNumber)
			autoModel, _ := model.GetModel(modelId)
			autoMake, _ := model.GetMake(makeId)
			autoYear, _ := model.GetYear(yearId)

			vmodel := vm.PartEngine{Base: vm.Base{Employee: employee},
				Make:    autoMake,
				Model:   autoModel,
				Year:    autoYear,
				Engines: engines,
			}
			pc.autoEngineTemplate.Execute(w, vmodel)
		}
	}
}
func (ac *adminController) GetMenu(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	employeeNumber, _ := strconv.Atoi(vars["employeeNumber"])
	employee, _ := model.GetEmployee(employeeNumber)

	vmodel := vm.Base{Employee: employee}
	ac.menuTemplate.Execute(w, vmodel)
}
func (pc *partController) GetModel(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	makeId, _ := strconv.Atoi(vars["makeId"])
	make, err := model.GetMake(makeId)
	if err != nil {
		w.WriteHeader(500)
	} else {
		employeeNumber, _ := strconv.Atoi(vars["employeeNumber"])
		employee, _ := model.GetEmployee(employeeNumber)
		vmodel := vm.PartModel{Base: vm.Base{Employee: employee}, Make: make}
		pc.autoModelTemplate.Execute(w, vmodel)
	}
}
func (pc *partController) GetMake(w http.ResponseWriter, r *http.Request) {
	employeeNumber, err := strconv.Atoi(r.FormValue("employeeNumber"))
	if err != nil {
		log.Println(err)
		w.WriteHeader(500)
	} else {
		employee, err := model.GetEmployee(employeeNumber)
		if err != nil {
			log.Print(err)
			http.Redirect(w, r, "/", 307)
		} else {
			vmodel := vm.PartMake{Base: vm.Base{Employee: employee}}
			pc.autoMakeTemplate.Execute(w, vmodel)
		}
	}

}
func (pc *partController) GetPart(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	partId, _ := strconv.Atoi(vars["partId"])
	employeeNumber, _ := strconv.Atoi(vars["employeeNumber"])
	employee, _ := model.GetEmployee(employeeNumber)

	part, err := model.GetPart(partId)

	if err != nil {
		log.Println(err)
		w.WriteHeader(500)
	} else {
		vmodel := vm.Part{Base: vm.Base{Employee: employee}, Part: part}

		pc.partTemplate.Execute(w, vmodel)
	}
}
func (pc *partController) GetPartSearchPartial(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	modelId, _ := strconv.Atoi(vars["modelId"])
	yearId, _ := strconv.Atoi(vars["yearId"])
	engineId, _ := strconv.Atoi(vars["engineId"])
	typeId, _ := strconv.Atoi(vars["typeId"])
	parts, err := model.SearchForParts(modelId, yearId, engineId, typeId)
	if err != nil {
		log.Print(err)
		w.WriteHeader(500)
	} else {
		employeeNumber, _ := strconv.Atoi(r.URL.Query().Get("employeeNumber"))
		employee, _ := model.GetEmployee(employeeNumber)

		vmodel := vm.PartsPartial{Base: vm.Base{Employee: employee}, Parts: parts}
		pc.searchResultPartialTemplate.Execute(w, vmodel)
	}
}
func (ac *adminController) GetCreateEmp(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	employeeNumber, _ := strconv.Atoi(vars["employeeNumber"])
	employee, _ := model.GetEmployee(employeeNumber)
	roles, err := model.GetRoles()

	if err != nil {
		log.Println(err)
		w.WriteHeader(500)
	} else {

		vmodel := vm.AdminCreateEmployee{
			Base:        vm.Base{Employee: employee},
			Roles:       roles,
			NewEmployee: &model.Employee{Role: &model.Role{}},
		}
		ac.createEmpTemplate.Execute(w, vmodel)
	}

}
func (ac *adminController) PostCreateEmp(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	roleId, _ := strconv.Atoi(r.FormValue("role"))
	role, _ := model.GetRole(roleId)

	payRate, _ := strconv.ParseFloat(r.FormValue("payRate"), 32)
	hireDate, _ := time.Parse("2006-01-02", r.FormValue("hireDate"))
	log.Print(r.FormValue("hireDate"))

	newEmployee := &model.Employee{
		GivenName:  r.FormValue("givenName"),
		Surname:    r.FormValue("surname"),
		Address:    r.FormValue("address"),
		City:       r.FormValue("city"),
		State:      r.FormValue("state"),
		PostalCode: r.FormValue("postalCode"),
		Role:       role,
		HireDate:   hireDate,
		PayRate:    float32(payRate),
	}

	newEmployee, err := model.CreateEmployee(newEmployee)

	if err != nil {
		employeeNumber, _ := strconv.Atoi(vars["employeeNumber"])
		employee, _ := model.GetEmployee(employeeNumber)
		roles, _ := model.GetRoles()

		vmodel := vm.AdminCreateEmployee{
			Base:        vm.Base{Employee: employee},
			Roles:       roles,
			NewEmployee: newEmployee,
		}
		ac.createEmpTemplate.Execute(w, vmodel)
	} else {
		http.Redirect(w, r, "/admin/employee?employeeNumber="+
			strconv.Itoa(newEmployee.EmployeeNumber), 302)
	}

}
Exemple #10
0
func (ac *adminController) GetEmployeeView(w http.ResponseWriter, r *http.Request) {
	employeeNumber, err := strconv.Atoi(r.URL.Query().Get("employeeNumber"))
	if err != nil {
		log.Println(err)
		w.WriteHeader(500)
	} else {
		employee, err := model.GetEmployee(employeeNumber)
		if err != nil {
			log.Println(err)
			w.WriteHeader(500)
		} else {
			vmodel := vm.AdminViewEmployee{Base: vm.Base{Employee: &model.Employee{}},
				ViewedEmployee: employee,
			}

			ac.viewEmpTemplate.Execute(w, vmodel)
		}

	}

}