public void PhotoProductMemoryRepositoryRepository_Add_Update()
        {
            var initialProduct = new PhotoProduct {
                Name = "Alpha"
            };

            var repo = new PhotoProductMemoryRepository();

            repo.Add(initialProduct);

            var updateProduct = new PhotoProduct {
                Id = initialProduct.Id, Name = "Beta"
            };

            repo.Add(updateProduct);

            var repoProduct = repo.Get(updateProduct.Id);

            Assert.IsNotNull(repoProduct, "Product should be present in the repository.");
            Assert.AreEqual("Beta", repoProduct.Name, "Product should now be called Beta.");

            Assert.AreSame(updateProduct, repoProduct, "Objects should be the same.");

            Assert.AreNotSame(initialProduct, updateProduct, "Objects should be the same.");
        }
Exemple #2
0
        public void BasketsController_RemoveBasketItem()
        {
            //prepare dataset: a product, a basket and a basket item
            var product = new PhotoProduct {
                Name = "Alpha", Id = "42"
            };
            var basket = new Basket {
                Id = "Aruba", Created = DateTime.Now, Modified = DateTime.Now
            };
            var basketItem = basket.Add(product, 1337);

            //prepare the ceremonies: the repo's, services and the controller
            var productRepo = new PhotoProductMemoryRepository();

            productRepo.Add(product);
            var productService = new PhotoProductService(productRepo);

            var repo = new BasketMemoryRepository();

            repo.Add(basket);

            var service = new BasketService(repo, productService);

            var controller = new BasketsController(service, productService);

            //actual test
            var result = controller.RemoveBasketItem("Aruba", basket.Contents[0].Id);

            Assert.IsNotNull(result, "There should be a result.");
            Assert.IsInstanceOfType(result, typeof(OkResult));

            //check result in original basket
            Assert.AreEqual(0, basket.Contents.Count, "There should be no basket items anymore.");
        }
Exemple #3
0
        public async Task <IActionResult> Edit(int id, [Bind("ProductId,Title")] PhotoProduct photoProduct)
        {
            if (id != photoProduct.ProductId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(photoProduct);
                    await _context.SaveChangesAsync();

                    TempData["edit"] = "Product type has been updated";
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PhotoProductExists(photoProduct.ProductId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(photoProduct));
        }
Exemple #4
0
        public async Task <IActionResult> Create([Bind("ProductId,Title")] PhotoProduct photoProduct)
        {
            if (ModelState.IsValid)
            {
                _context.Add(photoProduct);
                await _context.SaveChangesAsync();

                TempData["save"] = "Product type has been saved";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(photoProduct));
        }
        public void PhotoProductMemoryRepositoryRepository_Add()
        {
            var product = new PhotoProduct {
                Name = "Alpha"
            };

            var repo = new PhotoProductMemoryRepository();

            repo.Add(product);

            var repoProduct = repo.Get(product.Id);

            Assert.AreSame(product, repoProduct, "Objects should be the same.");
        }
        public void Basket_Content_Add()
        {
            var basket = new Basket {
                Id = "Aruba"
            };
            var product = new PhotoProduct {
                Name = "Alpha", Id = "42"
            };

            basket.Add(product, 1337);

            Assert.AreEqual(1, basket.Contents.Count, "There should be 1 item.");
            Assert.IsNotNull(basket.Contents[0], "There should be a basket item.");
            Assert.AreEqual(product, basket.Contents[0].Content, "The product should be in the first basket item.");
        }
Exemple #7
0
        ///<remarks>Validation should have been done by the service. There's no validation here.</remarks>
        public void Add(PhotoProduct product)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }

            //ensure identifier
            if (String.IsNullOrEmpty(product.Id))
            {
                product.Id = Guid.NewGuid().ToString();
            }

            //make sure it is in the index
            if (!_index.Contains(product.Id))
            {
                _index.Add(product.Id);
            }

            //just add it
            _storage[product.Id] = product;
        }
        public void BasketService_Validate_InvalidProduct()
        {
            var productRepo    = new PhotoProductMemoryRepository();
            var productService = new PhotoProductService(productRepo);

            var repo    = new BasketMemoryRepository();
            var service = new BasketService(repo, productService);

            //1. create basket
            var basket = service.Create();

            //2. add invalid product
            var product = new PhotoProduct {
                Name = "I do not exist.", Id = "1337"
            };

            basket.Add(product, 1);

            //3. Validate it against the service
            var validationResult = service.Validate(basket);

            Assert.IsFalse(validationResult, "Id is not set, so the basket should be invalid.");
        }
Exemple #9
0
        public async Task <ActionResult <PhotoProductDto> > AddPhoto(IFormFile file, int productId)
        {
            var product = await _unitOfWork.ProductRepository.GetProductByIdAsync(productId);

            if (product == null)
            {
                return(NotFound());
            }

            var result = await _photoService.AddPhotoAsync(file);

            if (result.Error != null)
            {
                return(BadRequest(result.Error.Message));
            }

            var photo = new PhotoProduct
            {
                PhotoProductUrl = result.SecureUrl.AbsoluteUri,
                PublicId        = result.PublicId
            };

            if (product.PhotoProducts.Count == 0)
            {
                photo.IsMain = true;
            }

            product.PhotoProducts.Add(photo);

            if (await _unitOfWork.Complete())
            {
                return(_mapper.Map <PhotoProductDto>(photo));
            }

            return(BadRequest("Problem addding photo"));
        }