Esempio n. 1
0
        /// <summary>
        /// POST: /Admin
        /// updates existing inventory product in inventory or creates a new one
        /// </summary>
        /// <returns> reloads Admin page </returns>
        public async Task <IActionResult> OnPost()
        {
            Product query = await _inv.GetOneByIdAsync(Product.ID);

            Product.ID = 0;

            if (Image != null)
            {
                // do all the blob stuff
                // 1. make a filepath
                var filePath = Path.GetTempFileName();
                // 2. open stream
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await Image.CopyToAsync(stream);
                }
                // 3. get container and blob
                var container = await ImageBlob.GetContainer("products");

                CloudBlob blob = await ImageBlob.GetBlob(Image.FileName, container.Name);

                // 4. upload image
                ImageBlob.UploadFile(container, Image.FileName, filePath);
                Product.Image = blob.Uri.ToString();
            }

            if (query == null || query.ID == 0)
            {
                await _inv.CreateAsync(Product);
            }
            else
            {
                query.Sku         = Product.Sku;
                query.Name        = Product.Name;
                query.Price       = Product.Price;
                query.QtyAvail    = Product.QtyAvail;
                query.Description = Product.Description;
                query.Meaty       = Product.Meaty;
                query.Category    = Product.Category;
                query.Image       = Product.Image;
                await _inv.UpdateAsync(query);
            }
            CurrentInventory = await _inv.GetAllAsync();

            return(RedirectToPage("../Admin/Index"));
        }