public async Task Transaction_Tests()
        {
            var newBrand = new Brand
            {
                Name    = "x",
                Country = new Country()
                {
                    Name        = "France",
                    CountryCode = "FR"
                }
            };

            var newBrand2 = new Brand
            {
                Name = "xx",
            };

            //Dont' work in unit test due to leak of support of transaction of sqllite
            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadUncommitted
            }))
            {
                try
                {
                    _ = await brandService.CreateAsync(newBrand);

                    var localSearch = await brandService.QueryAsync(new BaseBrandQuery()
                    {
                    });

                    localSearch.Results.Should().HaveCount(1);

                    _ = await brandService.CreateAsync(newBrand2);

                    scope.Complete();

                    newBrand.Id.Should().BePositive();
                    newBrand2.Id.Should().BePositive();
                }
                catch (Exception e)
                {
                    scope.Dispose();
                }
            }

            var search = await brandService.QueryAsync(new BaseBrandQuery()
            {
            });

            //this should be 0 count
            search.Results.Should().HaveCount(1);
        }
        public async Task Transaction_Tests()
        {
            var newBrand = new Brand
            {
                Name    = "x",
                Country = new Country()
                {
                    Name        = "France",
                    CountryCode = "FR"
                }
            };

            var newBrand2 = new Brand
            {
                Name = "xx",
            };

            //Dont' work in unit test due to leak of support of transaction of sqllite
            using (var scope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    _ = await brandService.CreateAsync(newBrand);

                    var localSearch = await brandService.QueryAsync(new BaseBrandQuery()
                    {
                    });

                    localSearch.Results.Should().HaveCount(1);

                    _ = await brandService.CreateAsync(newBrand2);

                    scope.Complete();

                    newBrand.Id.Should().BePositive();
                    newBrand2.Id.Should().BePositive();
                }
                catch
                {
                    scope.Dispose();
                }
            }

            var search = await brandService.QueryAsync(new BaseBrandQuery()
            {
            });

            search.Results.Should().HaveCount(0);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> CreateBrand(BrandDto brandDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (brandDto.Id != 0)
                {
                    return(BadRequest("The Id should be empty"));
                }

                var createdBrand = await _brandsService.CreateAsync(brandDto);

                var integrationEventData = JsonConvert.SerializeObject(new
                {
                    id        = createdBrand.Id,
                    brandName = createdBrand.BrandName
                });
                PublishToMessageQueue("brands.add", integrationEventData);

                _logger.LogInformation($"Brand added, id {createdBrand.Id}");

                //Fetch the brand from data source
                return(CreatedAtRoute("GetBrandById", new { id = createdBrand.Id }, createdBrand));
            }
            catch (DbQueryResultNullException e)
            {
                return(NotFound(e.Message));
            }
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!this.ModelState.IsValid)
            {
                return(this.Page());
            }

            await _brandService.CreateAsync(Input);

            return(this.RedirectToPage(PageConstants.BrandsSuccess));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> AddBrand([FromBody] Brand brand)
        {
            if (ModelState.IsValid)
            {
                var brandId = await brandService.CreateAsync(brand);

                return(Ok(brandId));
            }

            throw new HttpStatusCodeException(HttpStatusCode.BadRequest, "Brand data is incorrect");
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Post([FromBody] BrandForm model)
        {
            if (ModelState.IsValid)
            {
                var brand = new Brand
                {
                    Name        = model.Name,
                    SeoTitle    = model.Slug,
                    IsPublished = model.IsPublished
                };

                await _brandService.CreateAsync(brand);

                return(CreatedAtAction(nameof(Get), new { id = brand.Id }, null));
            }
            return(BadRequest(ModelState));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> CreateBrand(BrandDto brandDto)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (brandDto.Id != 0)
                {
                    return(BadRequest("The Id should be empty"));
                }

                var createdBrand = await _brandsService.CreateAsync(brandDto);

                //Fetch the brand from data source
                return(CreatedAtRoute("GetBrandById", new { id = createdBrand.Id }, createdBrand));
            }
            catch (DbQueryResultNullException e)
            {
                return(NotFound(e.Message));
            }
        }
        public async Task CreationUniqueName_ErrorTest()
        {
            Brand brand = new Brand()
            {
                Name    = "Citroen",
                Country = new Country()
                {
                    Name        = "France",
                    CountryCode = "FR"
                }
            };

            var brandCreationResult = await brandService.CreateAsync(brand);

            brandCreationResult.Result.Should().Be(CommandState.Success);
            brand.Id.Should().BePositive();

            Car car = new Car()
            {
                Name    = "C4",
                BrandId = brand.Id
            };

            var carCreationResult = await carService.CreateAsync(car);

            carCreationResult.Result.Should().Be(CommandState.Success);

            Car car2 = new Car()
            {
                Name    = "C4",
                BrandId = brand.Id
            };

            Func <Task> act = async() => await carService.CreateAsync(car2);

            act.Should().Throw <DbUpdateException>();
        }
Ejemplo n.º 9
0
 public async Task <IActionResult> Post(Brand model)
 {
     return(Json(await brandService.CreateAsync(model)));
 }