コード例 #1
0
ファイル: Mapping.cs プロジェクト: ZameAlex/BSA2018_Hometask4
 public Plane MapPlane(PlaneDto value)
 {
     return(new Plane
     {
         Id = value.ID,
         Name = value.Name,
         Type = unitOfWork.Types.Get(value.Type),
         Created = value.Created,
         Expired = value.Expires
     });
 }
コード例 #2
0
        public async Task <IActionResult> Put(int id, [FromBody] PlaneDto entity)
        {
            var result = await _planesSrvice.UpdateAsync(entity, id);

            if (result == null)
            {
                return(NotFound());
            }

            return(Ok(result));
        }
コード例 #3
0
ファイル: Mapping.cs プロジェクト: ZameAlex/BSA2018_Hometask7
 public async Task <Plane> MapPlane(PlaneDto value)
 {
     return(new Plane
     {
         Id = value.ID,
         Name = value.Name,
         Type = MapType(value.Type),
         Created = value.Created,
         Expired = DateTime.Now.AddDays(value.Expires.Days)
     });
 }
コード例 #4
0
        public async Task <IActionResult> Post([FromBody] PlaneDto entity)
        {
            var result = await _planesSrvice.AddAsync(entity);

            if (result == null)
            {
                return(NotFound());
            }

            return(Ok(result));
        }
コード例 #5
0
 public IActionResult Delete([FromBody] PlaneDto plane)
 {
     try
     {
         service.Delete(plane);
         return(NoContent());
     }
     catch (Exception ex)
     {
         return(NotFound(ex));
     }
 }
コード例 #6
0
 public IActionResult Put(int id, [FromBody] PlaneDto value)
 {
     if (ModelState.IsValid)
     {
         var item = service.Update(value);
         if (item != null)
         {
             return(Ok(item));
         }
     }
     return(BadRequest());
 }
コード例 #7
0
        public async Task <int> Create(PlaneDto Plane)
        {
            var validationResult = validator.Validate(Plane);

            if (validationResult.IsValid)
            {
                return(await unit.Planes.Create(await mapper.MapPlane(Plane)));
            }
            else
            {
                throw new ValidationException(validationResult.Errors);
            }
        }
コード例 #8
0
        public void Create_Plane_When_model_is_not_valid_Then_throws_ValidatorExceprion()
        {
            var planeService = new PlaneService(unitOfWork, mapper, new PlaneValidator());
            var plane        = new PlaneDto()
            {
                Name    = "Bobo",
                Type    = 1,
                Created = new DateTime(2013, 08, 03),
                Expires = new TimeSpan(29, 0, 0, 0)
            };

            Assert.Throws <FluentValidation.ValidationException>(() => planeService.Create(plane));
        }
コード例 #9
0
        public async Task <IActionResult> Delete([FromBody] PlaneDto plane)
        {
            try
            {
                await service.Delete(plane);

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(NotFound(ex));
            }
        }
コード例 #10
0
        public int Create(PlaneDto Plane)
        {
            var validationResult = validator.Validate(Plane);

            if (validationResult.IsValid)
            {
                return(unit.Planes.Create(mapper.MapPlane(Plane)));
            }
            else
            {
                throw new ValidationException(validationResult.Errors);
            }
        }
コード例 #11
0
        public async Task <IActionResult> Update([FromBody] PlaneDto plane)
        {
            if (ModelState.IsValid)
            {
                await _service.Update(plane);

                return(Ok(plane));
            }
            else
            {
                return(new BadRequestObjectResult(ModelState));
            }
        }
コード例 #12
0
 public IActionResult Put(int id, [FromBody] PlaneDto value)
 {
     if (ModelState.IsValid)
     {
         try
         {
             service.Update(value);
         }
         catch (Exception) { return(BadRequest()); }
         return(Ok());
     }
     return(BadRequest());
 }
コード例 #13
0
        public void Create_When_PlaneModel_is_valid_Then_returns_id()
        {
            var expectedPlane = new PlaneDto()
            {
                Name    = "name",
                Created = DateTime.Now,
                Expires = new TimeSpan(33, 0, 0, 0),
                Type    = 1
            };
            var id          = service.Create(expectedPlane);
            var actualPlane = service.Get(id);

            Assert.AreEqual(expectedPlane.Created, actualPlane.Created);
        }
コード例 #14
0
        public void Update_Plane_When_id_is_not_in_db_Then_throws_NullReferenceException()
        {
            var planeService = new PlaneService(unitOfWork, mapper, new PlaneValidator());
            var id           = 1123;
            var plane        = new PlaneDto()
            {
                Name    = "Bobo",
                Type    = 1,
                Created = new DateTime(2013, 08, 03),
                Expires = new TimeSpan(750, 0, 0, 0)
            };

            Assert.Throws <NullReferenceException>(() => planeService.Update(plane, id));
        }
コード例 #15
0
        public async Task <IActionResult> Put([FromBody] PlaneDto value)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await Task.Run(() => service.Update(mapper.Map <PlaneDto, Plane>(value)));

                    await service.SaveChangesAsync();
                }
                catch (Exception) { return(BadRequest()); }
                return(Ok());
            }
            return(BadRequest());
        }
コード例 #16
0
 public IActionResult Post([FromBody] PlaneDto value)
 {
     try
     {
         return(Ok(service.Create(value)));
     }
     catch (ValidationException e)
     {
         return(BadRequest(e.Message));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex));
     }
 }
コード例 #17
0
        public void Update_Plane_When_model_is_valid_Then_get_right_value_through_mapping()
        {
            var planeService = new PlaneService(unitOfWork, mapper, new PlaneValidator());
            var id           = 1;
            var plane        = new PlaneDto()
            {
                Name    = "Bobo",
                Type    = 1,
                Created = new DateTime(2013, 08, 03),
                Expires = new TimeSpan(750, 0, 0, 0)
            };

            planeService.Update(plane, id);
            var planeResult = planeService.Get(id);

            Assert.AreEqual(plane.Created, planeResult.Created);
        }
コード例 #18
0
        public void Update_UnValid()
        {
            //arange
            PlanesService service = new PlanesService(unitOfWork, mapper, validator);

            var DtoToMake = new PlaneDto
            {
                Id = 1, DepartureId = default(int), Name = "", ReleaseDate = new DateTime(1995, 1, 22)
            };

            //act
            service.Update(DtoToMake);

            var actual = (unitOfWork.Set <Plane>() as FakeRpository <Plane>).updatedItem;

            //assert
            Assert.IsNull(actual);
        }
コード例 #19
0
 public IActionResult Put(int id, [FromBody] PlaneDto plane)
 {
     try
     {
         service.Update(plane, id);
         return(Ok("success"));
     }
     catch (NotFoundException ex)
     {
         return(NotFound(ex));
     }
     catch (ValidationException e)
     {
         return(BadRequest(e.Message));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex));
     }
 }
コード例 #20
0
        public void Make_UnValid()
        {
            //arange
            PlanesService service = new PlanesService(unitOfWork, mapper, validator);


            var DtoToMake = new PlaneDto
            {
                Id = 1, DepartureId = 1, Name = "dfgdghdfhgdfghfdghdfhdfgh4456", ReleaseDate = new DateTime(1995, 1, 22)
            };


            //act
            service.Make(DtoToMake);

            var actual = (unitOfWork.Set <Plane>() as FakeRpository <Plane>).createdItem;

            //assert
            Assert.IsNull(actual);
        }
コード例 #21
0
        public void Update(PlaneDto Plane, int id)
        {
            var validationResult = validator.Validate(Plane);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors);
            }
            try
            {
                unit.Planes.Update(mapper.MapPlane(Plane), id);
            }
            catch (ArgumentNullException)
            {
                throw new NotFoundException();
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #22
0
        public void Update_Valid()
        {
            //arange
            var entity = new PlaneDto
            {
                Id          = 1,
                DepartureId = 1,
                PlaneTypeId = 1,
                Name        = "432432",
                ReleaseDate = new DateTime(1995, 1, 22)
            };

            //act
            service.Update(entity);

            var result = service.GetById(1);

            //assert
            Assert.NotNull(result);
            Assert.AreEqual(result.Name, entity.Name);
        }
コード例 #23
0
        public void Make_Valid()
        {
            //arange
            var entity = new PlaneDto
            {
                DepartureId = 3,
                PlaneTypeId = 3,
                Name        = "456456",
                ReleaseDate = new DateTime(1995, 1, 22)
            };

            //act
            service.Make(entity);

            var result = service.Get()
                         .ToList()
                         .SingleOrDefault(x => x.Name == entity.Name);

            //assert
            Assert.NotNull(result);
            Assert.AreEqual(result.Name, entity.Name);
        }
コード例 #24
0
        public async Task Update(PlaneDto Plane, int id)
        {
            var validationResult = validator.Validate(Plane);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors);
            }
            try
            {
                Plane.ID = id;
                await unit.Planes.Update(await mapper.MapPlane(Plane), id);
            }
            catch (ArgumentNullException)
            {
                throw new NotFoundException();
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #25
0
        public void Update_When_PlaneModel_is_not_valid_Then_throws_ValidatorException()
        {
            var plane1 = new PlaneDto()
            {
                Name    = "name",
                Created = DateTime.Now,
                Expires = new TimeSpan(2, 0, 0, 0),
                Type    = 1
            };
            var plane2 = new PlaneDto()
            {
                Name    = "name",
                Created = DateTime.Now,
                Expires = new TimeSpan(31, 0, 0, 0),
                Type    = 0
            };
            var plane3 = new PlaneDto()
            {
                Name    = "name",
                Expires = new TimeSpan(31, 0, 0, 0),
                Type    = 1
            };
            var plane4 = new PlaneDto()
            {
                ID      = -1,
                Name    = "name",
                Created = DateTime.Now,
                Expires = new TimeSpan(31, 0, 0, 0),
                Type    = 1
            };
            var id = 1;

            Assert.Throws <FluentValidation.ValidationException>(() => service.Update(plane1, id));
            Assert.Throws <FluentValidation.ValidationException>(() => service.Update(plane2, id));
            Assert.Throws <FluentValidation.ValidationException>(() => service.Update(plane3, id));
            Assert.Throws <FluentValidation.ValidationException>(() => service.Update(plane4, id));
        }
コード例 #26
0
 public void Delete(PlaneDto Plane)
 {
     unit.Planes.Delete(mapper.MapPlane(Plane));
 }
コード例 #27
0
 public async Task Delete(PlaneDto Plane)
 {
     await unit.Planes.Delete(await mapper.MapPlane(Plane));
 }
コード例 #28
0
 public PlaneDto FindPlane(PlaneDto planeDto)
 {
     return(PlaneMapper.toDto(PlaneRepository.GetPlaneByID(planeDto.PlaneId)));
 }
コード例 #29
0
 public PlaneDto Put(int id, [FromBody] PlaneDto entity)
 {
     return(_planesSrvice.Update(entity));
 }
コード例 #30
0
 public ActionResult <PlaneDto> Post([FromBody] PlaneDto entity)
 {
     return(_planesSrvice.Make(entity));
 }