Example #1
0
        public async Task <BrandDetailsViewModel> CreateAsync(BrandCreateInputModel brandCreateInputModel)
        {
            var brand = new Brand
            {
                Name = brandCreateInputModel.Name,
            };

            bool doesBrandExist = await this.brandsRepository
                                  .All()
                                  .AnyAsync(b => b.Name == brand.Name);

            if (doesBrandExist)
            {
                throw new ArgumentException(
                          string.Format(ExceptionMessages.BrandAlreadyExists, brand.Name));
            }

            await this.brandsRepository.AddAsync(brand);

            await this.brandsRepository.SaveChangesAsync();

            var viewModel = await this.GetViewModelByIdAsync <BrandDetailsViewModel>(brand.Id);

            return(viewModel);
        }
Example #2
0
        public async Task <IActionResult> Create(BrandCreateInputModel brandCreateInputModel)
        {
            if (!this.User.IsInRole(GlobalConstants.AdministratorRoleName))
            {
                return(this.Redirect("/Identity/Account/AccessDenied"));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.Redirect("/Brand/Create"));
            }

            var checkBrand = await this.brandService.IfBrandExistsAsync(brandCreateInputModel.Name.ToUpper());

            if (checkBrand)
            {
                var error = new BrandError();
                error.ErrorMessage = GlobalConstants.BrandCreateErrorMessage;
                return(this.RedirectToAction("Error", "Brand", error));
            }

            var brandDtoModel = BrandCreateMapper.Map(brandCreateInputModel);

            await this.brandService.CreateBrand(brandDtoModel);

            return(this.Redirect("/Brand/Index"));
        }
Example #3
0
        public static BrandCreateDtoModel Map(BrandCreateInputModel brandCreateInputModel)
        {
            var brandDtoModel = new BrandCreateDtoModel()
            {
                Name         = brandCreateInputModel.Name.ToUpper(),
                ImageAddress = brandCreateInputModel.ImageAddress,
            };

            return(brandDtoModel);
        }
Example #4
0
        public IActionResult Create(BrandCreateInputModel input)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(input));
            }

            this.brandService.Create(input.Name);
            return(this.Redirect(nameof(All)));
        }
Example #5
0
        public async Task <IActionResult> Create(BrandCreateInputModel brandCreateInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(brandCreateInputModel));
            }

            await this.brandService.CreateAsync(brandCreateInputModel.To <BrandServiceModel>());

            return(this.RedirectToAction("All", "Brands"));
        }
        public async Task <IActionResult> Create(BrandCreateInputModel brandCreateInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(brandCreateInputModel));
            }

            await this.brandsService.CreateAsync(brandCreateInputModel);

            return(this.RedirectToAction("GetAll", "Brands", new { area = "Administration" }));
        }
Example #7
0
        public async Task CheckSettingOfBrandProperties()
        {
            var model = new BrandCreateInputModel
            {
                Name = "Sony",
            };

            await this.brandsService.CreateAsync(model);

            var brand = await this.brandsRepository.All().FirstOrDefaultAsync();

            Assert.Equal("Sony", brand.Name);
        }
Example #8
0
        public async Task TestAddingBrand()
        {
            var model = new BrandCreateInputModel
            {
                Name = "Nikon",
            };

            await this.brandsService.CreateAsync(model);

            var count = await this.brandsRepository.All().CountAsync();

            Assert.Equal(1, count);
        }
Example #9
0
        public async Task CheckIfAddingBrandReturnsViewModel()
        {
            var brand = new BrandCreateInputModel
            {
                Name = "Pentax",
            };

            var viewModel = await this.brandsService.CreateAsync(brand);

            var dbEntry = await this.brandsRepository.All().FirstOrDefaultAsync();

            Assert.Equal(dbEntry.Id, viewModel.Id);
            Assert.Equal(dbEntry.Name, viewModel.Name);
        }
Example #10
0
        public async Task CheckIfAddingBrandThrowsArgumentException()
        {
            this.SeedDatabase();

            var brand = new BrandCreateInputModel
            {
                Name = this.firstBrand.Name,
            };

            var exception = await Assert
                            .ThrowsAsync <ArgumentException>(async() => await this.brandsService.CreateAsync(brand));

            Assert.Equal(string.Format(ExceptionMessages.BrandAlreadyExists, brand.Name), exception.Message);
        }