Exemple #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));
     }
 }
Exemple #2
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);
        }
Exemple #3
0
        public void PutDocument_ShouldReturnOk()
        {
            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.PutEmployeeOffice(3, passport.ToDTO()) as OkNegotiatedContentResult <EmployeeOfficeDTO>;

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Content.Id);
        }