public IHttpActionResult DeleteEmployeeOffice(int id) { try { UnitOfWork unitOfWork = new UnitOfWork(factory); EmployeeOffice office = unitOfWork.EmployeeOfficesRepository.Get(d => d.Id == id, includeProperties: "Office,Department,Profession").FirstOrDefault(); office.Deleted = true; unitOfWork.EmployeeOfficesRepository.Update(office); unitOfWork.Save(); EmployeeOfficeDTO dto = office.ToDTO(); return(Ok(dto)); } catch (NotFoundException nfe) { return(NotFound()); } catch (ConflictException ce) { return(Conflict()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public IHttpActionResult PostEmployeeOffice(EmployeeOfficeDTO employeeOffice) { if (employeeOffice == null || !ModelState.IsValid) { return(BadRequest(ModelState)); } try { EmployeeOffice office = employeeOffice.FromDTO(); UnitOfWork unitOfWork = new UnitOfWork(factory); office.Id = office.NewId(unitOfWork); unitOfWork.EmployeeOfficesRepository.Insert(office); unitOfWork.Save(); EmployeeOfficeDTO dto = unitOfWork.EmployeeOfficesRepository.Get(d => d.Id == office.Id, includeProperties: "Office,Department,Profession").FirstOrDefault().ToDTO(); return(CreatedAtRoute("GetEmployeeOffice", new { id = dto.Id }, dto)); } catch (NotFoundException nfe) { return(NotFound()); } catch (ConflictException ce) { return(Conflict()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public IHttpActionResult PutEmployeeOffice(int id, EmployeeOfficeDTO employeeOffice) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != employeeOffice.Id) { return(BadRequest()); } try { EmployeeOffice office = employeeOffice.FromDTO(); UnitOfWork unitOfWork = new UnitOfWork(factory); unitOfWork.EmployeeOfficesRepository.Update(office); unitOfWork.Save(); EmployeeOfficeDTO dto = unitOfWork.EmployeeOfficesRepository.Get(d => d.Id == id, includeProperties: "Office,Department,Profession").FirstOrDefault().ToDTO(); return(Ok(dto)); } catch (NotFoundException nfe) { return(NotFound()); } catch (ConflictException ce) { return(Conflict()); } catch (Exception e) { return(BadRequest(e.Message)); } }
public void PostOffice_ShoulAddOffice() { var officesTestData = new List <EmployeeOffice>() { new EmployeeOffice { Id = 1, EmployeeId = 2 }, new EmployeeOffice { Id = 2, Deleted = true, EmployeeId = 2 }, new EmployeeOffice { Id = 3, EmployeeId = 3 } }; var offices = MockHelper.MockDbSet(officesTestData); offices.Setup(d => d.Find(It.IsAny <object>())).Returns <object[]>((keyValues) => { return(offices.Object.SingleOrDefault(product => product.Id == (int)keyValues.Single())); }); offices.Setup(d => d.Add(It.IsAny <EmployeeOffice>())).Returns <EmployeeOffice>((contact) => { officesTestData.Add(contact); offices = MockHelper.MockDbSet(officesTestData); return(contact); }); var dbContext = new Mock <IAppDbContext>(); dbContext.Setup(m => m.EmployeeOffices).Returns(offices.Object); dbContext.Setup(d => d.Set <EmployeeOffice>()).Returns(offices.Object); dbContext.Setup(d => d.ExecuteStoredProcedure <int>(It.IsAny <string>(), It.IsAny <object[]>())) .Returns <string, object[]>((query, parameters) => { List <int> list = new List <int>(); if (query.Contains("NewTableId")) { int i = offices.Object.Max(d => d.Id) + 1; list.Add(i); } else { list.Add(0); } return(list); }); var factory = new Mock <IDbContextFactory>(); factory.Setup(m => m.CreateDbContext()).Returns(dbContext.Object); EmployeeOffice passport = new EmployeeOffice { Id = 0, EmployeeId = 3 }; var controller = new EmployeeOfficesController(factory.Object); var result = controller.PostEmployeeOffice(passport.ToDTO()) as CreatedAtRouteNegotiatedContentResult <EmployeeOfficeDTO>; Assert.IsNotNull(result); Assert.AreEqual(4, result.Content.Id); Assert.AreEqual(3, result.Content.EmployeeId); }
public async Task <bool> UpdateOfficeEmployees(string employeeId, ICollection <OfficeEmployeesUpdateServiceModel> model) { Employee employee = await this._context.Employees .Include(eo => eo.EmployeesOffices) .ThenInclude(o => o.Office) .FirstOrDefaultAsync(e => e.EmployeeId == employeeId); if (employee == null) { return(false); } foreach (var office in model) { EmployeeOffice employeeOffice = new EmployeeOffice { EmployeeId = employee.EmployeeId, OfficeId = office.OfficeId }; if (office.IsSelected && !employee.EmployeesOffices.Select(x => x.OfficeId).Contains(employeeOffice.OfficeId)) { employee.EmployeesOffices.Add(employeeOffice); } else if (office.IsSelected && employee.EmployeesOffices.Select(x => x.OfficeId).Contains(employeeOffice.OfficeId)) { continue; } else if (!office.IsSelected && !employee.EmployeesOffices.Select(x => x.OfficeId).Contains(employeeOffice.OfficeId)) { continue; } else { var entityToRemove = await this._context.EmployeesOffices.FirstOrDefaultAsync(x => x.OfficeId == employeeOffice.OfficeId && x.EmployeeId == employee.EmployeeId); this._context.EmployeesOffices.Remove(entityToRemove); await this._context.SaveChangesAsync(); } } this._context.Employees.Update(employee); int result = await this._context.SaveChangesAsync(); if (result > 0) { return(true); } return(false); }
public async Task CreateAsync(int employeeId, int officeId) { var doesExit = this.employeesOfficesRepository.All().Any(eo => eo.OfficeId == officeId); if (doesExit) { return; } var employeeOfficeNew = new EmployeeOffice { EmployeeId = employeeId, OfficeId = officeId, }; await this.employeesOfficesRepository.AddAsync(employeeOfficeNew); await this.employeesOfficesRepository.SaveChangesAsync(); }
public void DeleteOffice_ShouldDeleteAndReturnOk() { var officesTestData = new List <EmployeeOffice>() { new EmployeeOffice { Id = 1, EmployeeId = 2 }, new EmployeeOffice { Id = 2, Deleted = true, EmployeeId = 2 }, new EmployeeOffice { Id = 3, EmployeeId = 3 } }; var offices = MockHelper.MockDbSet(officesTestData); offices.Setup(d => d.Find(It.IsAny <object>())).Returns <object[]>((keyValues) => { return(offices.Object.SingleOrDefault(product => product.Id == (int)keyValues.Single())); }); var dbContext = new Mock <IAppDbContext>(); dbContext.Setup(m => m.EmployeeOffices).Returns(offices.Object); dbContext.Setup(d => d.Set <EmployeeOffice>()).Returns(offices.Object); var factory = new Mock <IDbContextFactory>(); factory.Setup(m => m.CreateDbContext()).Returns(dbContext.Object); EmployeeOffice passport = new EmployeeOffice { Id = 3, EmployeeId = 3 }; var controller = new EmployeeOfficesController(factory.Object); var result = controller.DeleteEmployeeOffice(3) as OkNegotiatedContentResult <EmployeeOfficeDTO>; Assert.IsNotNull(result); Assert.AreEqual(3, result.Content.Id); Assert.AreEqual(3, result.Content.EmployeeId); }