예제 #1
0
 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));
     }
 }
예제 #2
0
 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));
     }
 }
예제 #3
0
 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));
     }
 }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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();
        }
예제 #7
0
        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);
        }