Beispiel #1
0
        public async Task <IActionResult> Create([Bind("ID,Name,GPU,RAM,Processor,ScreenSizeInch,RouteToImage,ImageBytes")] Notebook notebook)
        {
            if (ModelState.IsValid)
            {
                _context.Add(notebook);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(notebook));
        }
Beispiel #2
0
        public async void ItemsController_GetSelectedItemDataAsync()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);
            Item newItem = new Item()
            {
                Id           = 1,
                Title        = "First",
                About        = "About",
                CurrentPrice = 222.22m,
                Image        = "image/href"
            };
            await context.AddAsync(newItem);

            await context.SaveChangesAsync();

            OkObjectResult itemRes = await controller.Details(newItem.Id) as OkObjectResult;

            Assert.NotNull(itemRes);
            Item item = itemRes.Value as Item;

            Assert.Equal(newItem.Title, item.Title);
            Assert.Equal(newItem.Image, item.Image);
            Assert.Equal(newItem.About, item.About);
            Assert.Equal(newItem.CurrentPrice, item.CurrentPrice);
            Assert.Equal(newItem.PriceHistory.Count, item.PriceHistory.Count);
        }
Beispiel #3
0
        /// <summary>
        /// Adding parsed items in context or update them with new price
        /// </summary>
        /// <param name="context"> context from ItemsController </param>
        /// <param name="titles"> all items titles </param>
        /// <param name="abouts"> all items about infos </param>
        /// <param name="images"> all items image hrefs </param>
        /// <param name="prices"> all item prices </param>
        /// <returns></returns>
        private async Task HandleParsedDataAsync(ShopAppContext context,
                                                 List <HtmlNode> titles,
                                                 List <HtmlNode> abouts,
                                                 List <HtmlNode> images,
                                                 List <HtmlNode> prices)
        {
            IEnumerable <Item> items    = context.Items;
            List <Item>        newItems = new List <Item>();

            for (var i = 0; i < titles.Count(); i++)
            {
                var  imageHref = images[i].Attributes["src"].Value;
                Item newItem   = new Item()
                {
                    Title = titles[i].InnerText,
                    About = abouts[i].InnerText,
                    Image = $"https://hotline.ua{imageHref}"
                };
                decimal price;
                string  replacedPrice = prices[i].InnerText.Replace("&nbsp;", "");
                decimal.TryParse(replacedPrice, out price);
                newItem.CurrentPrice = price;
                Item oldItem = null;
                if (items.Any())
                {
                    oldItem = items.FirstOrDefault(item => item.Title == newItem.Title);
                }

                if (!(oldItem is null))
                {
                    if (oldItem.CurrentPrice != price)
                    {
                        var priceInHistory = new PriceInfo()
                        {
                            Price = price,
                            Date  = DateTime.Now,
                            Item  = oldItem
                        };
                        oldItem.PriceHistory.Add(priceInHistory);
                        oldItem.Image        = newItem.Image;
                        oldItem.About        = newItem.About;
                        oldItem.CurrentPrice = price;

                        context.Items.Update(oldItem);
                        await context.SaveChangesAsync();
                    }
                }
Beispiel #4
0
        public async void ItemsController_ThrowNotFoundExceptionItemNullIdAsync()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);
            Item newItem = new Item()
            {
                Id           = 1,
                Title        = "First",
                About        = "About",
                CurrentPrice = 222.22m,
                Image        = "image/href"
            };
            await context.AddAsync(newItem);

            await context.SaveChangesAsync();

            NotFoundResult itemRes = await controller.Details(null) as NotFoundResult;

            Assert.NotNull(itemRes);
        }
Beispiel #5
0
        public async void ItemsController_GetNotEmptyDataAsync()
        {
            context    = GetInMemoryContext();
            controller = new ItemsController(context);
            Item newItem = new Item()
            {
                Id           = 1,
                Title        = "First",
                About        = "About",
                CurrentPrice = 222.22m,
                Image        = "image/href"
            };
            await context.AddAsync(newItem);

            await context.SaveChangesAsync();

            var items = await controller.Index();

            Assert.NotEmpty(items);
        }