Beispiel #1
0
        public IHttpActionResult DeleteEmployeeCar(int id)
        {
            try
            {
                UnitOfWork unitOfWork = new UnitOfWork(factory);

                EmployeeCar employeeCar = unitOfWork.EmployeeCarsRepository.GetByID(id);

                employeeCar.Deleted = true;
                unitOfWork.EmployeeCarsRepository.Update(employeeCar);
                unitOfWork.Save();

                EmployeeCarDTO dto = employeeCar.ToDTO();
                return(Ok(dto));
            }
            catch (NotFoundException nfe)
            {
                return(NotFound());
            }
            catch (ConflictException ce)
            {
                return(Conflict());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Beispiel #2
0
 public IHttpActionResult PostEmployeeCar(EmployeeCarDTO employeeCar)
 {
     if (employeeCar == null || !ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     try
     {
         EmployeeCar car        = employeeCar.FromDTO();
         UnitOfWork  unitOfWork = new UnitOfWork(factory);
         employeeCar.Id = car.NewId(unitOfWork);
         unitOfWork.EmployeeCarsRepository.Insert(car);
         unitOfWork.Save();
         //cos there are no included object-properies we need to load, then just ToDTO call
         EmployeeCarDTO dto = car.ToDTO();
         return(CreatedAtRoute("GetEmployeeCar", new { id = dto.Id }, dto));
     }
     catch (NotFoundException nfe)
     {
         return(NotFound());
     }
     catch (ConflictException ce)
     {
         return(Conflict());
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Beispiel #3
0
        public void PutCar_ShouldFail_WhenDifferentID()
        {
            var carsTestData = new List <EmployeeCar>()
            {
                new EmployeeCar {
                    Id = 1, EmployeeId = 2
                },
                new EmployeeCar {
                    Id = 2, Deleted = true, EmployeeId = 2
                },
                new EmployeeCar {
                    Id = 3, EmployeeId = 3
                }
            };
            var cars      = MockHelper.MockDbSet(carsTestData);
            var dbContext = new Mock <IAppDbContext>();

            dbContext.Setup(m => m.EmployeeCars).Returns(cars.Object);
            dbContext.Setup(d => d.Set <EmployeeCar>()).Returns(cars.Object);
            var factory = new Mock <IDbContextFactory>();

            factory.Setup(m => m.CreateDbContext()).Returns(dbContext.Object);

            IDbContextFactory fac = factory.Object;
            var controller        = new EmployeeCarsController(fac);

            EmployeeCar car = new EmployeeCar {
                Id = 1, Capacity = 333, Deleted = null, EmployeeId = 1
            };
            var badresult = controller.PutEmployeeCar(999, car.ToDTO());

            Assert.IsInstanceOfType(badresult, typeof(BadRequestResult));
        }
Beispiel #4
0
        public void PutCar_ShouldReturnOk()
        {
            var carsTestData = new List <EmployeeCar>()
            {
                new EmployeeCar {
                    Id = 1, EmployeeId = 2
                },
                new EmployeeCar {
                    Id = 2, Deleted = true, EmployeeId = 2
                },
                new EmployeeCar {
                    Id = 3, EmployeeId = 3
                }
            };
            var cars      = MockHelper.MockDbSet(carsTestData);
            var dbContext = new Mock <IAppDbContext>();

            dbContext.Setup(m => m.EmployeeCars).Returns(cars.Object);
            dbContext.Setup(d => d.Set <EmployeeCar>()).Returns(cars.Object);
            var factory = new Mock <IDbContextFactory>();

            factory.Setup(m => m.CreateDbContext()).Returns(dbContext.Object);

            IDbContextFactory fac = factory.Object;
            var controller        = new EmployeeCarsController(fac);

            EmployeeCar car = new EmployeeCar {
                Id = 4, Capacity = 33, Deleted = null, EmployeeId = 1
            };

            var result = controller.PutEmployeeCar(4, car.ToDTO()) as OkNegotiatedContentResult <EmployeeCarDTO>;

            // EmployeeCarDTO dto = result.Content;

            Assert.IsNotNull(result);
            Assert.AreEqual(33, (int)result.Content.Capacity);
            Assert.AreEqual(4, result.Content.Id);
            Assert.AreEqual(1, result.Content.EmployeeId);
        }
Beispiel #5
0
        public void PostCar_ShouldReturnSame()
        {
            var carsTestData = new List <EmployeeCar>()
            {
                new EmployeeCar {
                    Id = 1, EmployeeId = 2
                },
                new EmployeeCar {
                    Id = 2, Deleted = true, EmployeeId = 2
                },
                new EmployeeCar {
                    Id = 3, EmployeeId = 3
                }
            };


            var cars      = MockHelper.MockDbSet(carsTestData);
            var dbContext = new Mock <IAppDbContext>();

            dbContext.Setup(m => m.EmployeeCars).Returns(cars.Object);
            dbContext.Setup(d => d.Set <EmployeeCar>()).Returns(cars.Object);
            var factory = new Mock <IDbContextFactory>();

            factory.Setup(m => m.CreateDbContext()).Returns(dbContext.Object);

            IDbContextFactory fac = factory.Object;
            var controller        = new EmployeeCarsController(fac);

            EmployeeCar item = new EmployeeCar {
                Id = 0, Capacity = 333, Deleted = null, EmployeeId = 3
            };
            var result = controller.PostEmployeeCar(item.ToDTO()) as CreatedAtRouteNegotiatedContentResult <EmployeeCarDTO>;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.RouteName, "GetEmployeeCar");
            Assert.AreEqual(result.RouteValues["id"], result.Content.Id);
            Assert.AreEqual(result.Content.Capacity, item.Capacity);
        }
Beispiel #6
0
        public IHttpActionResult PutEmployeeCar(int id, EmployeeCarDTO employeeCar)
        {
            if (employeeCar == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id != employeeCar.Id)
            {
                return(BadRequest());
            }
            try
            {
                EmployeeCar car = employeeCar.FromDTO();

                UnitOfWork unitOfWork = new UnitOfWork(factory);
                unitOfWork.EmployeeCarsRepository.Update(car);
                unitOfWork.Save();


                //cos there are no included object-properies we need to load, then just ToDTO call
                EmployeeCarDTO dto = car.ToDTO();
                return(Ok(dto));
            }
            catch (NotFoundException nfe)
            {
                return(NotFound());
            }
            catch (ConflictException ce)
            {
                return(Conflict());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }