public bool AddEmployee(Admin admin, Employee employee) { if (employee == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var employeeExist = GetEmployeeById(admin, employee.employeeid); if (employeeExist != null) throw new Exception(ErrorConstants.EMPLOYEE_WITH_GIVEN_ID_ALREADY_EXIST); var response = elasticClient.Index<Employee>(employee, i => i .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_EMPLOYEE) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public bool UpdateEmployee(Admin admin, Employee employee) { if (employee == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var employeeElasticId = GetEmployeeId(employee.employeeid); if (String.IsNullOrWhiteSpace(employeeElasticId)) throw new Exception(ErrorConstants.EMPLOYEE_NOT_FOUND); var employeeUpdate = new Dictionary<string, object>(); if (!String.IsNullOrWhiteSpace(employee.name)) employeeUpdate[ConstEmployee.NAME] = employee.name; if (!String.IsNullOrWhiteSpace(employee.addr1)) employeeUpdate[ConstEmployee.ADDR_1] = employee.addr1; if (!String.IsNullOrWhiteSpace(employee.addr2)) employeeUpdate[ConstEmployee.ADDR_2] = employee.addr2; if (!String.IsNullOrWhiteSpace(employee.city)) employeeUpdate[ConstEmployee.CITY] = employee.city; if (!String.IsNullOrWhiteSpace(employee.district)) employeeUpdate[ConstEmployee.DISTRICT] = employee.district; if (!String.IsNullOrWhiteSpace(employee.state)) employeeUpdate[ConstEmployee.STATE] = employee.state; if (!String.IsNullOrWhiteSpace(employee.country)) employeeUpdate[ConstEmployee.COUNTRY] = employee.country; if (!String.IsNullOrWhiteSpace(employee.pincode)) employeeUpdate[ConstEmployee.PIN_CODE] = employee.pincode; if (!String.IsNullOrWhiteSpace(employee.phone)) employeeUpdate[ConstEmployee.PHONE] = employee.phone; if (!String.IsNullOrWhiteSpace(employee.email)) employeeUpdate[ConstEmployee.EMAIL] = employee.email; if (!String.IsNullOrWhiteSpace(employee.designation)) employeeUpdate[ConstEmployee.DESIGNATION] = employee.designation; var elasticClient = GetElasticClient(); var response = elasticClient.Update<Employee, object>(u => u .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_EMPLOYEE) .Id(employeeElasticId) .Doc(employeeUpdate) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public JsonResult UpdateEmployee() { var id = Request.Params.Get(AppConstants.EMPLOYEE_ID); var name = Request.Params.Get(AppConstants.NAME); var addr1 = Request.Params.Get(AppConstants.ADDRESS_1); var addr2 = Request.Params.Get(AppConstants.ADDRESS_2); var city = Request.Params.Get(AppConstants.CITY); var district = Request.Params.Get(AppConstants.DISTRICT); var state = Request.Params.Get(AppConstants.START); var country = Request.Params.Get(AppConstants.COUNTRY); var phone = Request.Params.Get(AppConstants.PHONE); var pinCode = Request.Params.Get(AppConstants.PIN_CODE); var email = Request.Params.Get(AppConstants.EMAIL); var designation = Request.Params.Get(AppConstants.DESIGNATION); var response = new ServiceResponse(); var admin = CookieHelper.GetLoggedInAdmin(HttpContext); if (admin == null) { response.result = ErrorConstants.ADMIN_NOT_LOGGED_IN; return Json(response); } if (String.IsNullOrWhiteSpace(id)) { response.result = ErrorConstants.REQUIRED_FIELD_EMPTY; return Json(response); } if (String.IsNullOrWhiteSpace(name) && String.IsNullOrWhiteSpace(addr1) && String.IsNullOrWhiteSpace(addr2) && String.IsNullOrWhiteSpace(city) && String.IsNullOrWhiteSpace(district) && String.IsNullOrWhiteSpace(state) && String.IsNullOrWhiteSpace(country) && String.IsNullOrWhiteSpace(pinCode) && String.IsNullOrWhiteSpace(phone) && String.IsNullOrWhiteSpace(email) && String.IsNullOrWhiteSpace(designation)) { response.result = ErrorConstants.NO_CHANGES; return Json(response); } try { var employee = new Employee() { employeeid = id, name = name, addr1 = addr1, addr2 = addr2, city = city, district = district, state = state, country = country, pincode = pinCode, phone = phone, email = email, designation = designation }; if (employeeManager.UpdateEmployee(admin, employee)) { response.result = SuccessConstants.EMPLOYEE_UPDATED; response.status = true; } else response.result = ErrorConstants.PROBLEM_UPDATING_EMPLOYEE; return Json(response); } catch (Exception e) { Console.Error.WriteLine(e.GetBaseException().Message); response.result = e.GetBaseException().Message; } return Json(response); }