Пример #1
0
        public async Task <IActionResult> DeleteBrand(BrandForDeleteDto brandForDelete)
        {
            var result = await repo.DeleteBrand(brandForDelete);

            if (result.IsSuccess)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest(result.Exception.GetType() == typeof(ArgumentNullException)
                                    ? "No item with this id found" : "Failed to delete item"));
            }
        }
Пример #2
0
        public async Task <BrandDeletedDto> DeleteBrand(BrandForDeleteDto brandForDelete)
        {
            var brandDeletedDto = new BrandDeletedDto();

            try
            {
                var brandToDelete = await context.Brands.FirstOrDefaultAsync(x => x.Id == brandForDelete.Id);

                context.Brands.Remove(brandToDelete);
                context.SaveChanges();

                brandDeletedDto.IsSuccess = true;
            }
            catch (Exception ex)
            {
                brandDeletedDto.Exception = ex;
            }

            return(brandDeletedDto);
        }
Пример #3
0
        public async Task Test_DeleteBrand_ShouldDeleteSuccessfully()
        {
            //arrange
            var brandForDelete = new BrandForDeleteDto {
                Id = 1
            };
            var resultModel = new BrandDeletedDto {
                IsSuccess = true
            };

            brandRepositoryMock.Setup(p => p.DeleteBrand(brandForDelete)).Returns(Task.FromResult(resultModel));

            //act
            var result = await subject.DeleteBrand(brandForDelete);

            var OkActionResult = result as OkResult;

            //assert
            Assert.IsInstanceOf <OkResult>(OkActionResult);
            brandRepositoryMock.Verify(x => x.DeleteBrand(brandForDelete), Times.Once);
        }
Пример #4
0
        public async Task Test_DeleteBrand_ThrowsException()
        {
            //arrange
            var brandForDelete = new BrandForDeleteDto {
                Id = 1
            };
            var resultModel = new BrandDeletedDto {
                IsSuccess = false, Exception = new Exception()
            };

            brandRepositoryMock.Setup(p => p.DeleteBrand(brandForDelete)).Returns(Task.FromResult(resultModel));

            //act
            var result = await subject.DeleteBrand(brandForDelete);

            var badRequest = result as BadRequestObjectResult;

            //assert
            Assert.IsInstanceOf <BadRequestObjectResult>(badRequest);
            brandRepositoryMock.Verify(x => x.DeleteBrand(brandForDelete), Times.Once);
        }