public async Task <IActionResult> OnPostAsync(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            //*********************Authorization*************************************************************
            var isAuthorized = await AuthorizationService.AuthorizeAsync(User, Product, Operations.Delete);

            if (!isAuthorized.Succeeded)
            {
                return(new ChallengeResult());
            }
            //***********************************************************************************************
            Product = await _context.Product
                      .AsNoTracking()
                      .FirstOrDefaultAsync(p => p.ProductID == id);

            if (Product == null)
            {
                return(HandleDeletedProduct());
            }
            try
            {
                _context.Product.Remove(Product);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateException)
            {
                return(RedirectToAction("./Delete",
                                        new { id, saveChangesError = true }));
            }
        }
        public async Task <IActionResult> OnPostAsync(string[] sortedVendors, string[] selectedVendors, Guid productID,
                                                      string CurrentSort, string CurrentFilter, int PageIndex, string AddVendor)
        {
            var productToUpdate = await _context.Product
                                  .FindAsync(productID);

            if (await TryUpdateModelAsync <Product>(
                    productToUpdate,
                    "productToUpdate"))
            {
                UpdateOfferings(productID, sortedVendors, selectedVendors);
                await _context.SaveChangesAsync();
            }
            else
            {
                MakeDesignatedVendorList(Vendor, productID);
            }

            if (AddVendor == "SaveAndAdd")
            {
                return(RedirectToPage("./AddNewVendor",
                                      new { id = productID, currentSort = CurrentSort, currentFilter = CurrentFilter, pageIndex = PageIndex }));
            }
            else
            {
                return(RedirectToPage("./AddVendorsToProduct",
                                      new { id = productID, sortOrder = CurrentSort, currentFilter = CurrentFilter, pageIndex = PageIndex }));
            }
        }
        public async Task <IActionResult> OnPostAsync(int id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //**********************Authorization*******************************************************************
            var isAuthorized = await AuthorizationService.AuthorizeAsync(User,
                                                                         ProductCategory, Operations.Update);

            if (!isAuthorized.Succeeded)
            {
                return(new ChallengeResult());
            }
            //******************************************************************************************************

            var productCategoryToUpdate = await _context.ProductCategory
                                          .FirstOrDefaultAsync(pc => pc.ProductCategoryID == id);

            if (productCategoryToUpdate == null)
            {
                HandleDeletedProductCategory();
            }

            _context.Entry(productCategoryToUpdate).Property("RowVersion")
            .OriginalValue = ProductCategory.RowVersion;

            if (await TryUpdateModelAsync <ProductCategory>(
                    productCategoryToUpdate,
                    "productCategory",
                    pc => pc.ProductCategoryID, pc => pc.Name))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToPage("./Index"));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntries = ex.Entries.Single();
                    var clientValues     = (ProductCategory)exceptionEntries.Entity;
                    var databaseEntry    = exceptionEntries.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save. " +
                                                 "The department was deleted by another user.");
                        return(Page());
                    }
                    var dbValues = (ProductCategory)databaseEntry.ToObject();
                    SetDbErrorMessage(dbValues, clientValues, _context);
                    ProductCategory.RowVersion = (byte[])dbValues.RowVersion;
                    ModelState.Remove("ProductCategory.RowVersion");
                }
            }
            return(Page());
        }
Beispiel #4
0
        public async Task <IActionResult> OnPostAsync(Guid id, string submitProduct)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var productToUpdate = await _context.Product.FindAsync(id);

            if (productToUpdate == null)
            {
                HandleDeletedProduct();
            }

            _context.Entry(productToUpdate).Property("RowVersion").OriginalValue = Product.RowVersion;

            if (await TryUpdateModelAsync <Product>(
                    productToUpdate,
                    "product",
                    p => p.ProductName, p => p.ProductDescr1,
                    p => p.ProductDescr2_Wt_Vol, p => p.Wholesaler, p => p.ProductCategoryID))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    if (submitProduct == "SaveAndEditVendors")
                    {
                        return(RedirectToPage("./AddVendorsToProduct", new { id }));
                    }
                    else
                    {
                        return(RedirectToPage("./Index"));
                    }
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var clientValues   = (Product)exceptionEntry.Entity;
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save. " +
                                                 "The product was deleted by another user.");
                        return(Page());
                    }

                    var dbValues = (Product)databaseEntry.ToObject();
                    await SetDbErrorMessage(dbValues, clientValues, _context);

                    Product.RowVersion = (byte[])dbValues.RowVersion;
                    ModelState.Remove("Product.RowVersion");
                }
                ProductCategorySL = new SelectList(Context.ProductCategory,
                                                   "ProductCategoryID", "Name", productToUpdate.ProductCategoryID);
            }
            return(Page());
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //************Authorization****************************************************
            var isAuthorized = await AuthorizationService.AuthorizeAsync(User,
                                                                         ProductCategory, Operations.Create);

            if (!isAuthorized.Succeeded)
            {
                return(new ChallengeResult());
            }

            //*****************************************************************************

            var productCategoryToUpdate = new ProductCategory();

            if (await TryUpdateModelAsync <ProductCategory>(
                    productCategoryToUpdate,
                    "productcategory",
                    pc => pc.ProductCategoryID, pc => pc.Name))
            {
                if (!_context.ProductCategory.Any(pc => pc.ProductCategoryID == productCategoryToUpdate.ProductCategoryID))
                {
                    try
                    {
                        _context.ProductCategory.Add(productCategoryToUpdate);
                        await _context.SaveChangesAsync();

                        Message = null;
                        return(RedirectToPage("./Index"));
                    }
                    catch (Exception ex)
                    {
                        RedirectToPage("./Create", new { ex.Message });
                    }
                }
                else
                {
                    Message = "The Product Category ID that you have chosen has already" +
                              " been taken. Please choose a different one.";
                    RedirectToPage("./Create", new { Message });
                }
            }

            return(null);
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
//**********************************Authorization***********************************************************
            var isAuthorized = await AuthorizationService.AuthorizeAsync(User, Vendor, Operations.Create);

            if (!isAuthorized.Succeeded)
            {
                return(new ChallengeResult());
            }
//***********************************************************************************************************

            var vendorToUpdate = new Vendor();

            if (await TryUpdateModelAsync <Vendor>(
                    vendorToUpdate,
                    "vendor",
                    v => v.VendorID, v => v.VendorName, v => v.OwnerID, v => v.VendorAddress1,
                    v => v.VendorAddress2))
            {
                if (!_context.Vendor.Any(v => v.VendorID == vendorToUpdate.VendorID))
                {
                    try
                    {
                        _context.Vendor.Add(vendorToUpdate);
                        await _context.SaveChangesAsync();

                        Message = null;
                        return(RedirectToPage("./Index"));
                    }
                    catch (Exception ex)
                    {
                        RedirectToPage("./Create", new { ex.Message });
                    }
                }
                else
                {
                    Message = "The Vendor ID that you have chosen has already" +
                              " been taken. Please choose a different one.";
                    RedirectToPage("./Create", new { Message });
                }
            }

            return(null);
        }
        public async Task <IActionResult> OnPostAsync(Guid id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            ArchivedOffering = await _context.ArchivedOffering
                               .AsNoTracking()
                               .FirstOrDefaultAsync(ao => ao.ArchivedOfferingID == id);

            if (ArchivedOffering != null)
            {
                _context.ArchivedOffering.Remove(ArchivedOffering);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./AdminArchive"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //***********************Authorization************************************************************

            var isAuthorized = await AuthorizationService.AuthorizeAsync(User, Product, Operations.Create);

            if (!isAuthorized.Succeeded)
            {
                return(new ChallengeResult());
            }

            //************************************************************************************************

            var productToUpdate = new Product();

            if (await TryUpdateModelAsync <Product>(
                    productToUpdate,
                    "product",
                    p => p.ProductName, p => p.ProductDescr1,
                    p => p.ProductDescr2_Wt_Vol, p => p.Wholesaler, p => p.ProductCategoryID))
            {
                Guid guid = Guid.NewGuid();
                productToUpdate.ProductID = guid;
                _context.Product.Add(productToUpdate);

                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            return(null);
        }
Beispiel #9
0
        public async Task <IActionResult> OnPostAsync(Guid ProductId, string CurrentSort, string CurrentFilter,
                                                      int PageIndex, string includeVendor)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var isAuthorized = await AuthorizationService.AuthorizeAsync(User, Vendor, Operations.Create);

            if (!isAuthorized.Succeeded)
            {
                return(new ChallengeResult());
            }

            var vendorToAdd = new Vendor();

            if (await TryUpdateModelAsync <Vendor>(
                    vendorToAdd,
                    "vendor",
                    v => v.VendorID, v => v.VendorName, v => v.VendorAddress1, v => v.VendorAddress2,
                    v => v.OwnerID))
            {
                if (!_context.Vendor.Any(v => v.VendorID == vendorToAdd.VendorID))
                {
                    Message = null;
                    try
                    {
                        _context.Vendor.Add(vendorToAdd);

                        if (includeVendor == "selected")
                        {
                            var productToUpdate = await _context.Product
                                                  .Include(p => p.Offering)
                                                  .FirstOrDefaultAsync(p => p.ProductID == ProductId);

                            productToUpdate.Offering.Add(new Offering()
                            {
                                ProductID = this.ProductId,
                                VendorID  = Vendor.VendorID
                            });
                        }
                        else
                        {
                        }
                        await _context.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        RedirectToPage("./AddNewVendor", new {
                            id            = ProductId,
                            currentSort   = CurrentSort,
                            currentFilter = CurrentFilter,
                            pageIndex     = PageIndex,
                            message       = ex.Message
                        });
                    }
                }
                else
                {
                    Message = "The Product Category ID that you have chosen has already" +
                              " been taken. Please choose a different one.";
                    return(RedirectToPage("./AddNewVendor", new {
                        id = ProductId,
                        sortOrder = CurrentSort,
                        currentFilter = CurrentFilter,
                        pageIndex = PageIndex,
                        message = Message
                    }));
                }
            }

            return(RedirectToPage("./AddVendorsToProduct", new
            {
                id = ProductId,
                sortOrder = CurrentSort,
                currentFilter = CurrentFilter,
                pageIndex = PageIndex
            }));
        }
        public async Task <IActionResult> OnPostAsync(string CurrentSort, string CurrentFilter,
                                                      int PageIndex, IEnumerable <Offering> Offering)
        {
            foreach (var offering in Offering)
            {
                if (!ModelState.IsValid)
                {
                    return(Page());
                }
                var offeringToUpdate = await _context.Offering
                                       .FirstOrDefaultAsync(o => o.OfferingID == offering.OfferingID);

                _context.Entry(offeringToUpdate).Property("RowVersion").OriginalValue = offering.RowVersion;

                offeringToUpdate.ProductPricePerWeight = offering.ProductPricePerWeight;
                offeringToUpdate.AsOfDate = offering.AsOfDate;

                if (await TryUpdateModelAsync <Offering>(
                        offeringToUpdate,
                        "offering",
                        o => o.ProductPricePerWeight, o => o.AsOfDate))
                {
                }
            }
            try
            {
                await _context.SaveChangesAsync();

                await ArchiveOffering.Archive(_context);

                Message = null;
                return(RedirectToPage("./Offering", new
                {
                    sortOrder = CurrentSort,
                    currentFilter = CurrentFilter,
                    pageIndex = PageIndex
                }));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                var exceptionEntry = ex.Entries.Single();
                var clientValues   = (Offering)exceptionEntry.Entity;
                var databaseEntry  = exceptionEntry.GetDatabaseValues();

                var dbValues = (Offering)databaseEntry.ToObject();
                setDbErrorMessage(dbValues, clientValues, _context);

                ModelState.Remove("RowVersion");
                string message = "The record you attempted to edit "
                                 + "was modified by another user after you. The "
                                 + "edit operation was canceled and the current values in the database "
                                 + "have been displayed. TO EDIT THIS RECORD, RE-ENTER YOUR VALUES."
                                 + "Otherwise the other user would not be aware that you had overwritten those entries";

                return(RedirectToPage("./Offering", new
                {
                    message,
                    sortOrder = CurrentSort,
                    currentFilter = CurrentFilter,
                    pageIndex = PageIndex
                }));
            }
        }