Exemplo n.º 1
0
        private void Create_Click(object sender, RoutedEventArgs e)
        {
            var turple     = RenderCreate();
            var btnCreate  = turple.Item1;
            var firstname  = turple.Item2;
            var lastname   = turple.Item3;
            var expirience = turple.Item4;

            btnCreate.Click += async(object sen, RoutedEventArgs evArgs) =>
            {
                var pilot = new PilotDTO()
                {
                    FirstName = firstname.Text, LastName = lastname.Text, Experience = int.Parse(expirience.Text)
                };
                try
                {
                    await service.CreateAsync(pilot);
                }
                catch (Exception) { }

                pilotsList.Add(pilot);
                UpdateList();
                SingleItem.Children.Clear();
            };
        }
Exemplo n.º 2
0
        public async Task <PilotDTO> CreatePilot(PilotDTO pilot)
        {
            if (pilot != null)
            {
                if (await unit.PilotsRepo.GetEntityById(pilot.Id) != null)
                {
                    throw new ArgumentOutOfRangeException("Such user exsists!");
                }
                Pilot newPilot = mapper.Map <PilotDTO, Pilot>(pilot);
                if (newPilot == null || newPilot.Name == null || newPilot.Surname == null)
                {
                    throw new AutoMapperMappingException("Couldn't map PilotDTO into Pilot");
                }

                var result = await unit.PilotsRepo.Insert(newPilot);

                await unit.SaveChangesAsync();

                return(mapper.Map <Pilot, PilotDTO>(result) ?? throw new AutoMapperMappingException("Error: Can't map the pilot into pilotDTO"));
            }
            else
            {
                throw new ArgumentNullException();
            }
        }
Exemplo n.º 3
0
        public bool Update(int id, PilotDTO obj)
        {
            var result = db.Pilots.UpdateObject(id, _mapper.Map <Pilot>(obj));

            db.Save();
            return(result);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Update(int id, [FromBody] PilotDTO item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                item.Id = id;
                await service.GetById(id);

                await service.Update(item);

                await service.SaveChanges();

                return(Ok(item));
            }
            catch (NotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
        public PilotDTO AddPilot(PilotDTO pilot)
        {
            Validation(pilot);
            Pilot modelPilot = mapper.Map <PilotDTO, Pilot>(pilot);

            return(mapper.Map <Pilot, PilotDTO>(unitOfWork.Pilots.Create(modelPilot)));
        }
Exemplo n.º 6
0
        public void AddDeleteNewPilotTest_Returns_CreatedResult_And_Pilot_ShoudBe_AddedTo_Database_And_Then_ShouldBe_Deleted()
        {
            // Arrange
            MSSQLContext           context                = new MSSQLContext();
            CrewsRepository        crewsRepository        = new CrewsRepository();
            PilotsRepository       pilotsRepository       = new PilotsRepository();
            StewardessesRepository stewardessesRepository = new StewardessesRepository();
            CrewingUnitOfWork      uow        = new CrewingUnitOfWork(crewsRepository, pilotsRepository, stewardessesRepository, context);
            CrewingService         service    = new CrewingService(uow);
            PilotsController       controller = new PilotsController(mapper.GetDefaultMapper(), service);

            // add act
            var newPilotDTO = new PilotDTO()
            {
                Birth           = new DateTime(1985, 5, 12, 0, 0, 0),
                ExperienceYears = 15,
                Name            = "Grisha",
                Surname         = "Kramer"
            };

            var addResult = controller.AddPilot(newPilotDTO);

            // add assert
            Assert.IsInstanceOf <CreatedResult>(addResult);
            Assert.IsInstanceOf <PilotDTO>((addResult as CreatedResult).Value);

            // delete act
            var addedPilotDTO = (addResult as CreatedResult).Value as PilotDTO;
            var deleteResult  = controller.DeletePilot(addedPilotDTO.Id);

            // delete assert
            Assert.IsInstanceOf <OkResult>(deleteResult);
            Assert.IsInstanceOf <NotFoundObjectResult>(controller.GetPilot(addedPilotDTO.Id));
        }
Exemplo n.º 7
0
        public void UpdateEntity(int id, PilotDTO pilotDTO)
        {
            var pilot = pilotRepository.Get(id);

            if (pilot == null)
            {
                throw new ValidationException($"Pilot with this id {pilotDTO.Id} not found");
            }

            if (pilotDTO.FirstName != null)
            {
                pilot.FirstName = pilotDTO.FirstName;
            }
            if (pilotDTO.LastName != null)
            {
                pilot.LastName = pilotDTO.LastName;
            }
            if (pilotDTO.BirthDate != DateTime.MinValue)
            {
                pilot.BirthDate = pilotDTO.BirthDate;
            }
            if (pilotDTO.Experience != null)
            {
                pilot.Experience = pilotDTO.Experience.Value;
            }

            pilotRepository.Update(pilot);
        }
Exemplo n.º 8
0
        public async Task <bool> UpdateObjectAsync(int id, PilotDTO obj)
        {
            var result = db.Pilots.Update(id, _mapper.Map <Pilot>(obj));
            await db.SaveAsync();

            return(result);
        }
Exemplo n.º 9
0
 public void Post([FromBody] PilotDTO value)
 {
     if (ModelState.IsValid)
     {
         Service.Create(value);
     }
 }
Exemplo n.º 10
0
        [Test] // behaviour test
        public void Create_When_entity_is_invalid_Then_it_makes_no_calls_to_repository_and_unit_of_work()
        {
            // Arrange
            var pilotDTOToCreate = new PilotDTO()
            {
                FirstName  = "Pilot2",
                LastName   = "Pilot2",
                BirthDate  = new DateTime(1970, 10, 1),
                Experience = 15
            };

            var pilotRepositoryFake = A.Fake <IPilotRepository>();

            var unitOfWorkFake = A.Fake <IUnitOfWork>();

            A.CallTo(() => unitOfWorkFake.Set <Pilot>()).Returns(pilotRepositoryFake);

            var pilotService = new PilotService(unitOfWorkFake, AlwaysInValidValidator);

            // Act + Assert
            var exception = Assert.Throws <BadRequestException>(() => pilotService.Create(pilotDTOToCreate));

            // Assert. Just behaviour
            A.CallTo(() => pilotRepositoryFake.Create(A <Pilot> ._)).MustNotHaveHappened();
            A.CallTo(() => unitOfWorkFake.PilotRepository).MustNotHaveHappened();
            A.CallTo(() => unitOfWorkFake.Set <Pilot>()).MustNotHaveHappened();
            A.CallTo(() => unitOfWorkFake.SaveChanges()).MustNotHaveHappened();
        }
Exemplo n.º 11
0
        [Test] // behaviour test
        public void Create_When_entity_is_created_Then_it_makes_calls_to_repository_and_unit_of_work()
        {
            // Arrange
            var pilotDTOToCreate = new PilotDTO()
            {
                FirstName  = "Pilot1",
                LastName   = "Pilot1",
                BirthDate  = new DateTime(1970, 10, 1),
                Experience = 15
            };

            var pilotRepositoryFake = A.Fake <IPilotRepository>();

            var unitOfWorkFake = A.Fake <IUnitOfWork>();

            A.CallTo(() => unitOfWorkFake.Set <Pilot>()).Returns(pilotRepositoryFake);

            var pilotService = new PilotService(unitOfWorkFake, AlwaysValidValidator);

            // Act
            var result = pilotService.Create(pilotDTOToCreate);

            // Assert. Just behaviour
            A.CallTo(() => pilotRepositoryFake.Create(A <Pilot> ._)).MustHaveHappenedOnceExactly();
            A.CallTo(() => unitOfWorkFake.Set <Pilot>()).MustHaveHappenedOnceExactly();
            A.CallTo(() => unitOfWorkFake.SaveChanges()).MustHaveHappenedOnceExactly();
        }
        public void UpdateEntity_Should_Update_pilot_typeof_Pilot()
        {
            // Arrange
            PilotDTO pilotDTO = new PilotDTO
            {
                Id         = 1,
                FirstName  = "Bob",
                LastName   = "Henk",
                Experience = 10,
                BirthDate  = new DateTime(1998, 07, 12)
            };
            Pilot pilot = new Pilot
            {
                Id         = 1,
                FirstName  = "Bob",
                LastName   = "Henk",
                Experience = 10,
                BirthDate  = new DateTime(1998, 07, 12)
            };

            var pilotRepository = A.Fake <IRepository <Pilot> >();

            A.CallTo(() => pilotRepository.Get(A <int> ._)).Returns(new Pilot {
                Id = 1
            });

            var pilotService = new PilotService(pilotRepository);

            //Act
            pilotService.UpdateEntity(1, pilotDTO);
            var result = pilotRepository.Get(1);

            // Assert
            Assert.AreEqual(pilot, result);
        }
Exemplo n.º 13
0
        public void Create_When_entity_is_invalid_Then_bad_request_exception_is_thrown()
        {
            // Arrange
            var pilotMock = new Pilot()
            {
                Id         = 2,
                FirstName  = "Pilot2",
                LastName   = "Pilot2",
                BirthDate  = new DateTime(1970, 10, 1),
                Experience = 15
            };

            var pilotDTOToCreate = new PilotDTO()
            {
                FirstName  = "Pilot2",
                LastName   = "Pilot2",
                BirthDate  = new DateTime(1970, 10, 1),
                Experience = 15
            };

            var pilotRepositoryFake = A.Fake <IPilotRepository>();

            A.CallTo(() => pilotRepositoryFake.Create(A <Pilot> ._)).Returns(pilotMock);

            var unitOfWorkFake = A.Fake <IUnitOfWork>();

            A.CallTo(() => unitOfWorkFake.Set <Pilot>()).Returns(pilotRepositoryFake);

            var pilotService = new PilotService(unitOfWorkFake, AlwaysInValidValidator);

            // Act + Assert
            var exception = Assert.Throws <BadRequestException>(() => pilotService.Create(pilotDTOToCreate), "");

            Assert.AreEqual(exception.Message, "Is Invalid");
        }
        public void CreateEntity_Should_Create_pilot_typeof_Pilot()
        {
            // Arrange
            PilotDTO pilotDTO = new PilotDTO
            {
                Id         = 1,
                FirstName  = "Bob",
                LastName   = "Henk",
                Experience = 10,
                BirthDate  = new DateTime(1998, 07, 12)
            };
            Pilot pilot = new Pilot
            {
                Id         = 1,
                FirstName  = "Bob",
                LastName   = "Henk",
                Experience = 10,
                BirthDate  = new DateTime(1998, 07, 12)
            };

            var pilotRepository = new FakeRepository <Pilot>();
            var pilotService    = new PilotService(pilotRepository);

            // Act
            pilotService.CreateEntity(pilotDTO);
            var result = pilotRepository.Get(1);

            // Assert
            Assert.AreEqual(pilot, result);
        }
        public void UpdateEntity_Should_Update_pilot_in_db()
        {
            // Arrange
            var pilot = dispatcherContext.Pilots
                        .FirstOrDefault(c => c.FirstName == "TestName1" && c.LastName == "TestName1");

            PilotDTO pilotDTO = new PilotDTO
            {
                FirstName  = "TestName1",
                LastName   = "TestName1",
                BirthDate  = DateTime.Parse("12.06.1994"),
                Experience = 20
            };

            pilot = new Pilot
            {
                Id         = pilot.Id,
                FirstName  = "TestName1",
                LastName   = "TestName1",
                BirthDate  = DateTime.Parse("12.06.1994"),
                Experience = 20
            };

            // Act
            _pilotService.UpdateEntity(pilot.Id, pilotDTO);
            var pilotResult = _pilotRepository.Get(pilot.Id);

            // Assert
            Assert.AreEqual(pilot, pilotResult);
        }
        public async Task UpdateEntityAsync(int id, PilotDTO pilotDTO)
        {
            var pilot = await pilotRepository.GetAsync(id);

            if (pilot == null)
            {
                throw new ValidationException($"Pilot with this id {id} not found");
            }

            if (pilotDTO.CrewId > 0)
            {
                pilot.CrewId = pilotDTO.CrewId;
            }
            if (pilotDTO.FirstName != null)
            {
                pilot.FirstName = pilotDTO.FirstName;
            }
            if (pilotDTO.LastName != null)
            {
                pilot.LastName = pilotDTO.LastName;
            }
            if (pilotDTO.BirthDate != DateTime.MinValue)
            {
                pilot.BirthDate = pilotDTO.BirthDate;
            }
            if (pilotDTO.Experience != null)
            {
                pilot.Experience = pilotDTO.Experience.Value;
            }

            await pilotRepository.UpdateAsync(pilot).ConfigureAwait(false);
        }
Exemplo n.º 17
0
        public async Task Update(int id, PilotDTO modelDTO)
        {
            var source = uow.Pilots.Get(id);
            var dest   = mapper.Map <PilotDTO, Pilot>(modelDTO);
            await uow.Pilots.Update(dest);

            await uow.SaveAsync();
        }
Exemplo n.º 18
0
 public void UpdatePilot(PilotDTO pilot)
 {
     if (pilot != null)
     {
         Pilot updtPilot = mapper.Map <PilotDTO, Pilot>(pilot);
         unitOfWork.PilotRepository.Insert(updtPilot);
     }
 }
Exemplo n.º 19
0
 public void CreatePilot(PilotDTO pilot)
 {
     if (pilot != null)
     {
         Pilot newPilot = mapper.Map <PilotDTO, Pilot>(pilot);
         unitOfWork.PilotRepository.Create(newPilot);
     }
 }
Exemplo n.º 20
0
        private void Validation(PilotDTO pilot)
        {
            var validationResult = validator.Validate(pilot);

            if (!validationResult.IsValid)
            {
                throw new Exception(validationResult.Errors.First().ToString());
            }
        }
Exemplo n.º 21
0
 public bool AddObject(PilotDTO obj)
 {
     if (obj != null)
     {
         db.Pilots.Add(_mapper.Map <Pilot>(obj));
         return(true);
     }
     return(false);
 }
        public async Task <IActionResult> Get(int id)
        {
            PilotDTO ticket = await service.GetById(id);

            if (ticket == null)
            {
                return(NotFound());
            }
            return(Ok(ticket));
        }
Exemplo n.º 23
0
        public IActionResult Post([FromBody] PilotDTO Pilot)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var result = _service.AddObject(Pilot);

            return(result == true?StatusCode(200) : StatusCode(404));
        }
 public void TestSetup()
 {
     pilot = new PilotDTO()
     {
         Name       = "Yan",
         Surname    = "Gorshkov",
         Birthday   = new DateTime(1998, 8, 21),
         Experience = 2
     };
 }
        public async Task DeletePilot()
        {
            //act
            await pilotService.DeletePilot(pilot.Id);

            PilotDTO result = await pilotService.GetPilot(pilot.Id);

            //assert
            Assert.IsNull(result);
        }
Exemplo n.º 26
0
        public IActionResult Put(int id, [FromBody] PilotDTO Pilot)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var result = _service.Update(id, Pilot);

            return(result == true?StatusCode(200) : StatusCode(500));
        }
Exemplo n.º 27
0
        public async Task <IActionResult> Put(int id, [FromBody] PilotDTO Pilot)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var result = await _service.UpdateObjectAsync(id, Pilot);

            return(result == true?StatusCode(200) : StatusCode(500));
        }
Exemplo n.º 28
0
        public async Task <PilotDTO> AddPilot(PilotDTO pilot)
        {
            Validation(pilot);
            Pilot modelPilot = mapper.Map <PilotDTO, Pilot>(pilot);
            Pilot result     = await unitOfWork.Pilots.Create(modelPilot);

            await unitOfWork.SaveChangesAsync();

            return(mapper.Map <Pilot, PilotDTO>(result));
        }
Exemplo n.º 29
0
        public void PostPilotTestGoodResult_when_post_correct_then_HttpOK()
        {
            PilotDTO flight = new PilotDTO()
            {
                Name = "Name", Experience = 2, Surname = "Surname"
            };

            Assert.AreEqual(new HttpResponseMessage(HttpStatusCode.OK).StatusCode,
                            _controller.Post(flight).StatusCode);
        }
        public async Task AddPilot_When_correct_data_Then_check_exists()
        {
            //assing
            pilot.Id = pilotService.AddPilot(pilot).Result.Id;

            //act
            PilotDTO checkPilot = await pilotService.GetPilot(pilot.Id);

            //assert
            Assert.IsNotNull(checkPilot);
        }