Exemplo n.º 1
0
        public IHttpResponse Add(IHttpRequest request, CreateCakeViewModel model)
        {
            const string formNameKey    = "name";
            const string formPriceKey   = "price";
            const string formPictureKey = "picture";

            if (CoreValidator.CheckForMissingKeys(request, formNameKey, formPriceKey, formPictureKey))
            {
                return(new BadRequestResponse());
            }

            if (CoreValidator.CheckIfNullOrEmpty(model.Name, model.Price.ToString(), model.PictureUrl))
            {
                return(this.ReturnResponseWithErrorMessage(EmptyFields, CakeAdd));
            }

            Product product = this.unitOfWork
                              .ProductRepository
                              .Create(model.Name, model.Price, model.PictureUrl);

            this.unitOfWork.ProductRepository.Add(product);
            this.unitOfWork.Save();

            this.ViewData["name"]     = model.Name;
            this.ViewData["price"]    = model.Price.ToString();;
            this.ViewData[Key.Result] = Value.Block;

            return(this.FileViewResponse(CakeAdd));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(CreateCakeViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                // Retrieve associated category (in case of unselection, category id will be set to default value "0")
                string categoryIdString   = Request.Form["Categories"];
                bool   categoryIdIsValid  = int.TryParse(categoryIdString, out int categoryIdParsed);
                int    categoryId         = categoryIdIsValid ? categoryIdParsed : 0;
                var    associatedCategory = context.Categories.Find(categoryId);

                var cake = new Cake(
                    viewModel.Name,
                    viewModel.Description,
                    viewModel.ImageUrl,
                    viewModel.Price
                    );

                if (associatedCategory != null)
                {
                    cake.Categories.Add(associatedCategory);
                }

                context.Add(cake);
                await context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(viewModel));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create(CreateCakeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                this.logger.LogDebug("Model is not valid.");

                var errors = this.ModelState.Values.SelectMany(p => p.Errors).Select(e => e.ErrorMessage).ToList();

                var errorModel = this.errorService.GetErrorModel(errors);

                return(View("Error", errorModel));
            }

            try
            {
                await this.cakeService.AddCakeToDb <CreateCakeViewModel>(model);
            }
            catch (Exception e)
            {
                ViewData["Errors"] = e.Message;

                return(View("Error"));
            }

            return(Redirect("/"));
        }
Exemplo n.º 4
0
        public async Task GetAllCakes_ShouldReturnAllCakesDeletedAndNot()
        {
            //Arrange
            var db = this.SetDb();

            var repo = new Repository <Product>(db);

            var cakeModel1 = new CreateCakeViewModel {
                Name = "Chocolate Peanut Cake", CategoryId = 1, Price = 35.50m, Description = "This Chocolate and Peanut Butter Drip Cake is completely sinful.", Image = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136591/Chocolate_and_Peanut_cake.jpg"
            };

            var cakeModel2 = new CreateCakeViewModel {
                Name = "Chocolate Drip Cake", CategoryId = 1, Price = 35.50m, Description = "This Chocolate and Peanut Butter Drip Cake is completely sinful.", Image = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136590/Chocolate_Drip_cake.jpg"
            };

            var cakeService = new CakeService(null, repo, this.Mapper);

            await cakeService.AddCakeToDb(cakeModel1);

            await cakeService.AddCakeToDb(cakeModel2);

            var deleteCake = await repo.GetByIdAsync(1);

            deleteCake.IsDeleted = true;
            deleteCake.Category  = new Category {
                Id = 1, Type = Models.Enums.CategoryType.Cake
            };

            var activeCake = await repo.GetByIdAsync(2);

            activeCake.Category = new Category {
                Id = 1, Type = Models.Enums.CategoryType.Cake
            };
            await repo.SaveChangesAsync();

            //Act

            var allActiveCakes = cakeService.GetAllCakes();

            var expectedCakesCount = 2;
            var actualCakesCount   = allActiveCakes.Count();

            //Assert
            Assert.Equal(expectedCakesCount, actualCakesCount);
        }
Exemplo n.º 5
0
        public async Task AddCakeToDb_WithDuplicateCake_ShouldThrow()
        {
            //Arrange
            var db   = this.SetDb();
            var repo = new Repository <Product>(db);

            var cakeModel1 = new CreateCakeViewModel {
                Name = "Chocolate Peanut Cake", CategoryId = 1, Price = 35.50m, Description = "This Chocolate and Peanut Butter Drip Cake is completely sinful.", Image = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136590/Chocolate_and_Peanut_cake.jpg"
            };

            var cakeModel2 = new CreateCakeViewModel {
                Name = "Chocolate Peanut Cake", CategoryId = 1, Price = 35.50m, Description = "This Chocolate and Peanut Butter Drip Cake is completely sinful.", Image = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136590/Chocolate_and_Peanut_cake.jpg"
            };

            var cakeService = new CakeService(null, repo, this.Mapper);

            //Act
            await cakeService.AddCakeToDb(cakeModel1);

            //Assert
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await cakeService.AddCakeToDb(cakeModel2));
        }
Exemplo n.º 6
0
        private async Task SeedProducts(CakeItDbContext db)
        {
            var repo = new Repository <Product>(db);

            var cakeModel1 = new CreateCakeViewModel {
                Name = "Chocolate Peanut Cake", CategoryId = 1, Price = 35.50m, Description = "This Chocolate and Peanut Butter Drip Cake is completely sinful.", Image = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136591/Chocolate_and_Peanut_cake.jpg"
            };

            var cakeModel2 = new CreateCakeViewModel {
                Name = "Chocolate Drip Cake", CategoryId = 1, Price = 35.50m, Description = "This Chocolate and Peanut Butter Drip Cake is completely sinful.", Image = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136590/Chocolate_Drip_cake.jpg"
            };

            var cakeService = new CakeService(null, repo, this.Mapper);

            await cakeService.AddCakeToDb(cakeModel1);

            await cakeService.AddCakeToDb(cakeModel2);

            await repo.SaveChangesAsync();

            //It works without SaveCanges()???
        }
Exemplo n.º 7
0
        public async Task AddCakeToDb_WithValidCake_ShouldReturnTrue()
        {
            //Arrange
            var db = this.SetDb();

            var repo = new Repository <Product>(db);

            var cakeModel = new CreateCakeViewModel {
                Name = "Chocolate Peanut Cake", CategoryId = 1, Price = 35.50m, Description = "This Chocolate and Peanut Butter Drip Cake is completely sinful.", Image = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136590/Chocolate_and_Peanut_cake.jpg"
            };

            var cakeService = new CakeService(null, repo, this.Mapper);

            //Act
            await cakeService.AddCakeToDb(cakeModel);

            //works with and without SaveChanges()????
            await repo.SaveChangesAsync();

            var actualName = repo.All().LastOrDefault().Name;

            //Assert
            Assert.Equal("Chocolate Peanut Cake", actualName);
        }