Beispiel #1
0
        private CakeService CreateCakeService()
        {
            var userId      = Guid.Parse(User.Identity.GetUserId());
            var cakeService = new CakeService(userId);

            return(cakeService);
        }
Beispiel #2
0
        //public IHttpActionResult DeleteCake(int id)
        //{
        //    var service = CreateCakeService();
        //    if (!service.DeleteCake(id))
        //        return InternalServerError();
        //    return Ok();
        //}
        // [Route("api/Cakes/{id}")]
        public IHttpActionResult GetById(int id)
        {
            CakeService cakeService = CreateCakeService();
            var         cake        = cakeService.GetCakeById(id);

            return(Ok(cake));
        }
Beispiel #3
0
        public async Task GetAllCakes_WithProductsInDb_ShouldReturnProduct()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act
            //Doesn't pass. Supose due to the Mapper;
            //var result = cakeService.GetAllCakes();

            //this works;
            var result = repo.All().Select(p => new CakeIndexViewModel
            {
                Id          = p.Id,
                Name        = p.Name,
                IsDeleted   = p.IsDeleted,
                CategoryId  = p.CategoryId,
                Rating      = p.Rating ?? 0,
                RatingVotes = p.RatingVotes ?? 0
            }).ToList();



            //Assert
            result.ShouldNotBeEmpty();
        }
Beispiel #4
0
        public async Task UpdateCake_WithdDuplicateName_ShouldNotUpdatedProduct()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            var entity = repo.All().SingleOrDefault(p => p.Id == 1);

            db.Entry(entity).State = EntityState.Detached;

            var model = this.Mapper.Map <Product, EditAndDeleteViewModel>(entity);

            model.Name = "Chocolate Drip Cake";

            //Act
            var result = await cakeService.UpdateCake(model);

            var actual = await cakeService.GetCakeById(1);

            string name = actual.Name;

            //Assert
            Assert.Equal("Chocolate Peanut Cake", name);
            Assert.Equal("Product with such name already exists.", result);
        }
Beispiel #5
0
        public async Task SoftDelete_WithInValidCustomCakeId_ShouldMarkCakeAsDeleted()
        {
            //Assert
            var db   = this.SetDb();
            var repo = new Repository <CustomCakeImg>(db);
            var mock = new Mock <ILogger <CustomCakeService> >();
            ILogger <CustomCakeService> logger = mock.Object;
            var productRepo = new Repository <Product>(db);

            var productService = new CakeService(null, productRepo, this.Mapper);

            var service = new CustomCakeService(productRepo, repo, this.Mapper, logger);

            CustomCakeOrderViewModel model = new CustomCakeOrderViewModel
            {
                Sponge           = "Vanilla",
                FirstLayerCream  = "Whipped",
                SecondLayerCream = "Whipped",
                Filling          = "No_Filling",
                SideDecoration   = "White_Chocolate_Cigarettes",
                TopDecoration    = "Habana",
                NumberOfSlices   = 6,
                Img = null,
            };

            var product = service.CreateCustomProduct(model);

            await productService.AddCakeToDb(product);

            //Act


            //Assert
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await productService.SoftDelete(2));
        }
Beispiel #6
0
        public async Task UpdateCake_WithdDuplicateImageUrl_ShouldNotUpdatedProduct()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            var entity = repo.All().SingleOrDefault(p => p.Id == 1);

            db.Entry(entity).State = EntityState.Detached;

            var model = this.Mapper.Map <Product, EditAndDeleteViewModel>(entity);

            model.Image = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136590/Chocolate_Drip_cake.jpg";

            //Act
            var result = await cakeService.UpdateCake(model);

            var actual = await cakeService.GetCakeById(1);

            string imageExpected = "https://res.cloudinary.com/cakeit/image/upload/ar_1:1,c_fill,g_auto,e_art:hokusai/v1544136591/Chocolate_and_Peanut_cake.jpg";
            string imageActual   = actual.Image;

            //Assert
            Assert.Equal(imageExpected, imageActual);
            Assert.Equal("Product with such image url already exists.", result);
        }
Beispiel #7
0
        public async Task UpdateCake_WithValidData_ShouldSaveUpdatedProductInDb()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            var entity = repo.All().SingleOrDefault(p => p.Id == 1);

            db.Entry(entity).State = EntityState.Detached;

            entity.Price = 45.00m;

            //Act
            var result = await cakeService.UpdateCake(this.Mapper.Map <Product, EditAndDeleteViewModel>(entity));

            var actual = await cakeService.GetCakeById(1);

            decimal price = actual.Price;

            //Assert
            Assert.Equal(45, price);
        }
Beispiel #8
0
        public IHttpActionResult Get()
        {
            CakeService cakeService = CreateCakeService();
            var         cakes       = cakeService.GetCakes();

            return(Ok(cakes));
        }
Beispiel #9
0
        public void GetAllCakes_WithOutProductsInDb_ShouldReturnProduct()
        {
            //Arange
            var db   = this.SetDb();
            var repo = new Repository <Product>(db);

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

            //Act
            var result = this.Mapper.ProjectTo <CakeIndexViewModel>(repo.All()).ToList();

            //Assert
            result.ShouldBeEmpty();
        }
        public static void Main(string[] args)
        {
            CakeFactory  cakeFactory = new CakeFactory();
            CakeService  cakeService = new CakeService();
            int          num         = 0;
            IList <Cake> cakes       = cakeFactory.Cakes;
            string       choice;

            do
            {
                Console.WriteLine("enter your choice:\n1.Add Cake\n2.Delete Cake\n3.Update Cake\n4.Search Cake\n5.Display Cakes\nEnter your choice:");
                Console.WriteLine("===========================================================");
                Console.WriteLine();
                var input = Convert.ToInt32(Console.ReadLine());
                if (input == 1)
                {
                    Cake flavor = cakeService.AddCake(ref num, cakes);
                    cakeFactory.Cakes.Add(flavor);
                    Console.WriteLine("\nCake Added successfully\n");
                    cakeService.DisplayCakes(cakes);
                }
                else if (input == 3)
                {
                    cakeService.UpdateCake(cakes);
                }
                else if (input == 5)
                {
                    cakeService.DisplayCakes(cakes);
                }
                else if (input == 2)
                {
                    cakeService.DeleteCake(cakes);
                }
                else if (input == 4)
                {
                    cakeService.SearchCake(cakes);
                }
                else
                {
                    Console.WriteLine("select the correct option");
                }
                Console.WriteLine("\n===========================================================");
                Console.WriteLine();
                Console.WriteLine("if u want to continue press yes");
                choice = Console.ReadLine();
            } while (string.Compare(choice, "yes", true) == 0);
            Console.ReadLine();
        }
Beispiel #11
0
        public async Task AddRatingToCake_WithInValidRate_ShoulThrow()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act

            //Assert
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await service.AddRatingToCake(2, 6));
        }
Beispiel #12
0
        public async Task GetCakeToEdit_WithInValidCakeId_ShouldThrow()
        {
            //Assert
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act

            //Assert
            await Assert.ThrowsAsync <NullReferenceException>(async() => await service.GetCakeToEdit(3));
        }
Beispiel #13
0
        public async Task GetCakesById_WithInValidId_ShouldReturnNull()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act

            //Assert
            await Assert.ThrowsAnyAsync <NullReferenceException>(async() => await(cakeService.GetCakeById(3)));
        }
Beispiel #14
0
        public async Task ShowCakeDetails_WithInValidId_ShouldReturnNull()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act
            var cakeDetails = await service.ShowCakeDetails(3);

            //Assert
            Assert.Null(cakeDetails);
        }
Beispiel #15
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);
        }
Beispiel #16
0
        public async Task DeleteCake_WithInvalidId_ShouldDoNoting()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            EditAndDeleteViewModel cake = null;

            //Act

            //Assert
            await Assert.ThrowsAsync <NullReferenceException>(async() => await cakeService.DeleteCake(cake));
        }
Beispiel #17
0
        public async Task GetCakesById_WithValidId_ShouldReturnProduct()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act
            var result = await cakeService.GetCakeById(1);

            var productId = repo.All().SingleOrDefault(p => p.Name == result.Name).Id;

            //Assert
            Assert.Equal(1, productId);
            result.ShouldBeOfType <EditAndDeleteViewModel>();
        }
Beispiel #18
0
        public async Task GetCakeToEdit_WithValidCakeId_ShouldReturnProduct()
        {
            //Assert
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act
            var result = await service.GetCakeToEdit(2);

            var expectedCakeId = 2;
            var acutalCakeId   = result.Id;

            //Assert
            Assert.Equal(expectedCakeId, acutalCakeId);
        }
Beispiel #19
0
        public async Task SoftDelete_WithValidId_ShouldMarkCakeAsIsDeleted()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act
            await service.SoftDelete(1);

            var expected = true;
            var actual   = repo.All().SingleOrDefault(p => p.Id == 1).IsDeleted;

            //Assert
            Assert.Equal(expected, actual);
        }
Beispiel #20
0
        public async Task AddRatingToCake_WithValidData_ShoulChangeRatingVotesOfProduct()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act
            await service.AddRatingToCake(2, 5);

            var expectedRatingVote = 1;
            var actualRatingVote   = repo.All().SingleOrDefault(p => p.Id == 2).RatingVotes;

            //Assert
            Assert.Equal(expectedRatingVote, actualRatingVote);
        }
Beispiel #21
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()???
        }
Beispiel #22
0
        public async Task AddRatingToCake_WithAddingRate_ShoulIncreaseRatingVotes()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            //Act
            await service.AddRatingToCake(2, 5);

            await service.AddRatingToCake(2, 3);

            var expectedRatingVotes = 2;
            var actualRatingVotes   = repo.All().SingleOrDefault(p => p.Id == 2).RatingVotes;

            //Assert
            Assert.Equal(expectedRatingVotes, actualRatingVotes);
        }
        public async Task RemoveFromCart_WithItemCategoryTwo_ShouldReturnEmptyCart()
        {
            //Arrange
            var db = this.SetDb();

            var repo              = new Repository <CustomCakeImg>(db);
            var productRepo       = new Repository <Product>(db);
            var productService    = new CakeService(null, productRepo, this.Mapper);
            var customCakeService = new CustomCakeService(productRepo, repo, this.Mapper, null);

            var wrapper     = new TestCartSessionWrapper();
            var cartManager = new CartManager(wrapper);

            var cartService = new CartService(productRepo, cartManager);

            CustomCakeOrderViewModel model = new CustomCakeOrderViewModel
            {
                Sponge           = "Vanilla",
                FirstLayerCream  = "Whipped",
                SecondLayerCream = "Whipped",
                Filling          = "No_Filling",
                SideDecoration   = "White_Chocolate_Cigarettes",
                TopDecoration    = "Habana",
                NumberOfSlices   = 6,
                Img = null,
            };

            var product = customCakeService.CreateCustomProduct(model);

            await productService.AddCakeToDb(product);

            await cartService.AddToCart(1);

            //Act
            await cartService.RemoveFromCart(1);

            //Assert
            Assert.Empty(cartManager.GetCartItem());
        }
Beispiel #24
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));
        }
Beispiel #25
0
        public async Task SoftDelete_WithValidCustomCakeId_ShouldMarkCakeAsDeleted()
        {
            //Assert
            var db   = this.SetDb();
            var repo = new Repository <CustomCakeImg>(db);
            var mock = new Mock <ILogger <CustomCakeService> >();
            ILogger <CustomCakeService> logger = mock.Object;
            var productRepo = new Repository <Product>(db);

            var productService = new CakeService(null, productRepo, this.Mapper);

            var service = new CustomCakeService(productRepo, repo, this.Mapper, logger);

            CustomCakeOrderViewModel model = new CustomCakeOrderViewModel
            {
                Sponge           = "Vanilla",
                FirstLayerCream  = "Whipped",
                SecondLayerCream = "Whipped",
                Filling          = "No_Filling",
                SideDecoration   = "White_Chocolate_Cigarettes",
                TopDecoration    = "Habana",
                NumberOfSlices   = 6,
                Img = null,
            };

            var product = service.CreateCustomProduct(model);

            await productService.AddCakeToDb(product);

            //Act
            await productService.SoftDelete(1);

            var expectedIsDeleted = true;
            var actualIsDeleted   = (await productRepo.GetByIdAsync(1)).IsDeleted;

            //Assert
            Assert.Equal(expectedIsDeleted, actualIsDeleted);
        }
Beispiel #26
0
        public async Task DeleteCake_WithValidId_ShouldDeleteProduct()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

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

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

            var cake = await cakeService.GetCakeById(1);

            //Act
            await cakeService.DeleteCake(cake);

            var actualProductsCount = repo.All().Where(p => p.IsDeleted == true).Count();;

            var expectedProductsCount = 1;

            //Assert
            Assert.Equal(expectedProductsCount, actualProductsCount);
        }
Beispiel #27
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);
        }
Beispiel #28
0
 public CakeModule(CakeService service)
 {
     _service = service;
     _service.SetBaseModule(this);
 }