public IEnumerable <EmployeeBOL> GetMapEmployees(IEnumerable <Employee> employees) { try { var employeesBOL = new List <EmployeeBOL>(); foreach (var employee in employees) { var employeeBOL = new EmployeeBOL { EmployeeId = employee.EmployeeId, FirstName = employee.FirstName, LastName = employee.LastName, Salary = employee.Salary, Department = employee.Department }; employeesBOL.Add(employeeBOL); } return(employeesBOL); } catch (Exception ex) { throw ex; } }
public async Task <bool> AddEmployeeAsync(EmployeeBOL employeeBOL) { try { var employee = _employeeModel.GetMapEmployeeBOL(employeeBOL); await _employeeRepo.AddAsync(employee); return(true); } catch (Exception ex) { throw ex; } }
public async Task <HttpResponseMessage> PutEmployeeAsync(string employeeId, [FromBody] EmployeeBOL employeeBol) { try { var entity = await _employeeLogic.UpdateEmployeeAsync(employeeId, employeeBol); return(entity == null ? Request.CreateResponse(HttpStatusCode.NotFound, $"Employee with Id={employeeId} not found.") : Request.CreateResponse(HttpStatusCode.OK, entity)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public async Task <HttpResponseMessage> PostEmployeeAsync([FromBody] EmployeeBOL employeeBol) { try { await _employeeLogic.AddEmployeeAsync(employeeBol); var message = Request.CreateResponse(HttpStatusCode.Created, employeeBol); message.Headers.Location = new Uri($"{Request.RequestUri}/{employeeBol.EmployeeId}"); return(message); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public EmployeeBOL GetMapEmployee(Employee employee) { try { var employeeBOL = new EmployeeBOL { EmployeeId = employee.EmployeeId, FirstName = employee.FirstName, LastName = employee.LastName, Salary = employee.Salary, Department = employee.Department }; return(employeeBOL); } catch (Exception ex) { throw ex; } }
public Employee GetMapEmployeeBOL(EmployeeBOL employeeBOL) { try { var employee = new Employee { EmployeeId = employeeBOL.EmployeeId, FirstName = employeeBOL.FirstName, LastName = employeeBOL.LastName, Salary = employeeBOL.Salary, Department = employeeBOL.Department }; return(employee); } catch (Exception ex) { throw ex; } }
public async Task PostEmployee_ShouldReturnEmployee() { var employeeBOL = new EmployeeBOL { EmployeeId = "21-12344", FirstName = "xyz", LastName = "abc", Department = "IT", Salary = 20000 }; var employeeLogic = new Mock <IEmployeeLogic>(); employeeLogic.Setup(x => x.AddEmployeeAsync(employeeBOL)); var controller = new EmployeesController(employeeLogic.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; controller.Request = new HttpRequestMessage { RequestUri = new Uri("https://localhost:44370/api/Employees") }; controller.Configuration = new HttpConfiguration(); controller.Configuration.Routes.MapHttpRoute( "DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional }); controller.RequestContext.RouteData = new HttpRouteData( new HttpRoute(), new HttpRouteValueDictionary { { "controller", "Employees" } } ); var response = await controller.PostEmployeeAsync(employeeBOL); Assert.IsNotNull(response); Assert.AreEqual(response.StatusCode, HttpStatusCode.Created); Assert.AreEqual("https://localhost:44370/api/Employees/21-12344", response.Headers.Location.AbsoluteUri); Assert.IsTrue(response.TryGetContentValue(out employeeBOL)); Assert.AreEqual("21-12344", employeeBOL.EmployeeId); }
public async Task <EmployeeBOL> UpdateEmployeeAsync(string employeeId, EmployeeBOL employeeBOL) { try { var employeeEntity = _employeeModel.GetMapEmployeeBOL(employeeBOL); var employee = await _employeeRepo.UpdateAsync(employeeId, employeeEntity); if (employee == null) { return(null); } return(_employeeModel.GetMapEmployee(employee)); } catch (Exception ex) { throw ex; } }
public async Task GetEmployeeById_ShouldReturnNotFound() { var employeeBOL = new EmployeeBOL { EmployeeId = "21-12344", FirstName = "xyz", LastName = "abc", Department = "IT", Salary = 20000 }; var employeeLogic = new Mock <IEmployeeLogic>(); employeeLogic.Setup(x => x.GetEmployeeByIdAsync("21-12344")) .Returns(Task.FromResult(employeeBOL)); var controller = new EmployeesController(employeeLogic.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; var response = await controller.GetEmployeeByIdAsync(It.IsAny <string>()); Assert.AreEqual(response.StatusCode, HttpStatusCode.NotFound); Assert.AreNotEqual(It.IsAny <string>(), employeeBOL.EmployeeId); }
public async Task DeleteEmployee_ShouldDeleteEMployeeById() { var employeeBOL = new EmployeeBOL { EmployeeId = "21-12344", FirstName = "xyz", LastName = "abc", Department = "IT", Salary = 20000 }; var employeeLogic = new Mock <IEmployeeLogic>(); employeeLogic.Setup(x => x.DeleteEmployeeByIdAsync("21-12344")) .Returns(Task.FromResult(employeeBOL)); var controller = new EmployeesController(employeeLogic.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; var response = await controller.DeleteEmployeeByIdAsync("21-12344"); Assert.IsNotNull(response); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); Assert.IsTrue(response.TryGetContentValue(out employeeBOL)); Assert.AreEqual("21-12344", employeeBOL.EmployeeId); }
public async Task PutEmployee_ShouldReturnUpdatedEmployee() { var employeeBOL = new EmployeeBOL { EmployeeId = "21-12344", FirstName = "xyz", LastName = "abc", Department = "IT", Salary = 20000 }; var employeeLogic = new Mock <IEmployeeLogic>(); employeeLogic.Setup(x => x.UpdateEmployeeAsync(It.IsAny <string>(), It.IsAny <EmployeeBOL>())) .Returns(Task.FromResult(employeeBOL)); var controller = new EmployeesController(employeeLogic.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; var response = await controller.PutEmployeeAsync("21-12344", new EmployeeBOL { FirstName = "XYZ", LastName = "ABC", Department = "IT", Salary = 10000 }); Assert.IsNotNull(response); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); Assert.IsTrue(response.TryGetContentValue(out employeeBOL)); Assert.AreEqual("21-12344", employeeBOL.EmployeeId); }