コード例 #1
0
        public IActionResult Delete(EntityViewModel vm)
        {
            bool errorMessageSet = false;
            var  userID          = User.FindFirstValue(ClaimTypes.NameIdentifier);

            //Check for absorption vendor selection.
            if (vm.AbsorptionEntity.EntityID != 0)
            {
                //Make sure the user owns both the absorbed and absorbing vendors
                if (!_vendorRepository.UserOwnsVendor(vm.EntityOfInterest.EntityID, userID) ||
                    !_vendorRepository.UserOwnsVendor(vm.AbsorptionEntity.EntityID, userID))
                {
                    //TODO: Perhaps a more specific message to the user?
                    return(NotFound());
                }

                //Convert the VMVendors to DBVendors
                Vendor absorbedVendor  = _vendorRepository.Get(vm.EntityOfInterest.EntityID, userID);
                Vendor absorbingVendor = _vendorRepository.Get(vm.AbsorptionEntity.EntityID, userID);

                //Ensure that the deleted vendor is not default.
                if (!absorbedVendor.IsDefault)
                {
                    //Make sure the absorbing vendor and the deleting vendor are not the same.
                    if (absorbedVendor.VendorID != absorbingVendor.VendorID)
                    {
                        //Update all transactions that currently point to the vendor being deleted to instead point to the absorbing vendor.
                        _vendorRepository.Absorption(absorbedVendor.VendorID, absorbingVendor.VendorID, userID);

                        //Delete the vendor to be deleted.
                        _vendorRepository.Delete(absorbedVendor.VendorID);

                        TempData["Message"] = "Vendor successfully deleted.";

                        return(RedirectToAction("Index"));
                    }
                    SetErrorMessage(vm, "Vendor being deleted and vendor absorbing cannot be the same.", errorMessageSet);
                    errorMessageSet = true;
                }
                SetErrorMessage(vm, "Deleting a default vendor is not allowed.", errorMessageSet);
                errorMessageSet = true;
            }
            SetErrorMessage(vm, "You must select a vendor to absorb transactions related to the vendor being deleted.", errorMessageSet);

            EntityViewModel failureStateVM = new EntityViewModel();

            failureStateVM.EntityOfInterest = vm.EntityOfInterest;
            failureStateVM.VendorSelectList = failureStateVM.InitVendorSelectList(_vendorRepository, userID);
            return(View(failureStateVM));
        }
コード例 #2
0
        public IActionResult Delete(int?id)
        {
            var userID = User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (id == null)
            {
                return(BadRequest());
            }

            //Confirm the user owns the vendor
            if (!_vendorRepository.UserOwnsVendor((int)id, userID))
            {
                return(NotFound());
            }

            //Get the vendor data
            Vendor vendorToDelete = _vendorRepository.Get((int)id, userID);

            //Don't allow users to delete a default vendor
            if (!vendorToDelete.IsDefault)
            {
                //Convert the DBVendor to a VMVendor
                EntityViewModel vm = new EntityViewModel();
                vm.EntityOfInterest = new EntityViewModel.Entity
                {
                    EntityID    = vendorToDelete.VendorID,
                    Name        = vendorToDelete.Name,
                    IsDisplayed = vendorToDelete.IsDisplayed
                };

                //Instantiate an absorption VMVendor
                vm.AbsorptionEntity = new EntityViewModel.Entity();

                //Fill the vendor select list with user owned vendors.
                vm.VendorSelectList = vm.InitVendorSelectList(_vendorRepository, userID);

                return(View(vm));
            }

            TempData["BadMessage"] = "Deleting default vendor is not allowed.";

            return(RedirectToAction("Index"));
        }