Exemplo n.º 1
0
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)
	}

}
Exemplo n.º 2
0
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)
	}

}