コード例 #1
0
        public ActionResult DeleteConfirmed(int customerId, int carId)
        {
            Car car = carsRepo.GetCarByCustomerId(customerId, carId);

            if (car == null)
            {
                return(HttpNotFound());
            }

            // delete car's jobs first
            car.Jobs.ToList().ForEach(j =>
            {
                var job = jobsRepo.GetJobById(customerId, carId, j.JobId);
                // delete the job's children first
                job.SpareParts.ToList().ForEach(sp =>
                {
                    var sparePart = sparePartsRepo.GetSparePartById(job.JobId, sp.SparePartId);
                    sparePartsRepo.DeleteSparePart(sparePart.SparePartId);
                });
                sparePartsRepo.Save();

                //then delete the job itself
                jobsRepo.DeleteJob(job.JobId);
                jobsRepo.Save();
            });

            // then delete the car itself
            carsRepo.DeleteCar(carId);
            carsRepo.Save();

            return(RedirectToAction("CarsByCustomer", customerId));
        }
        public ActionResult DeleteConfirmed(int customerId)
        {
            var customer = customersRepo.GetCustomerById(customerId);

            if (customer == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }

            customer.Cars.ToList().ForEach(c =>
            {
                var car = carsRepo.GetCarByCustomerId(customerId, c.CarId);
                car.Jobs.ToList().ForEach(j =>
                {
                    var job = jobsRepo.GetJobById(customerId, c.CarId, j.JobId);
                    job.SpareParts.ToList().ForEach(sp =>
                    {
                        var sparePart = sparePartsRepo.GetSparePartById(job.JobId, sp.SparePartId);
                        sparePartsRepo.DeleteSparePart(sparePart.SparePartId);
                    });
                    sparePartsRepo.Save();

                    jobsRepo.DeleteJob(job.JobId);
                });
                jobsRepo.Save();

                carsRepo.DeleteCar(c.CarId);
            });

            carsRepo.Save();

            customersRepo.DeleteCustomer(customerId);
            customersRepo.Save();

            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public ActionResult Edit(EditJobViewModel viewModel, int customerId, int carId, int jobId)
        {
            var currentUser = manager.FindById(User.Identity.GetUserId());
            var customer    = customersRepo.GetCustomerById(customerId);
            var car         = carsRepo.GetCarByCustomerId(customerId, carId);

            if (ModelState.IsValid)
            {
                var job = jobsRepo.GetJobById(customerId, carId, jobId);
                job.Mileage      = viewModel.Mileage;
                job.Description  = viewModel.Description;
                job.LastModified = DateTime.Now;
                job.IsPaid       = viewModel.Paid;
                job.IsFinished   = viewModel.Finished;

                /// If the view model spare parts list is empty, we must initialize it,
                /// because the Model Binder assumes it is null by default.
                viewModel.SpareParts = viewModel.SpareParts ?? new List <EditSparePartViewModel>();

                /// Finding all the removed parts during the edit (if any)
                List <int> toBeDeletedIds = new List <int>();
                foreach (var sparePart in job.SpareParts)
                {
                    var existingSparePart = viewModel.SpareParts
                                            .Where(sp => sp.SparePartId == sparePart.SparePartId)
                                            .SingleOrDefault();
                    if (existingSparePart == null)
                    {
                        toBeDeletedIds.Add(sparePart.SparePartId);
                    }
                }

                /// Deleting all the removed parts (if any) from the DB and the entity
                toBeDeletedIds.ForEach(id => sparePartsRepo.DeleteSparePart(id));
                sparePartsRepo.Save();
                job.SpareParts.RemoveAll(x => toBeDeletedIds.Contains(x.SparePartId));

                job.SpareParts.OrderBy(x => x.SparePartId).ToList();
                viewModel.SpareParts.OrderBy(x => x.SparePartId).ToList();


                // if the viewmodel contains more spare parts than the current job
                // add new empty entries to the spare parts list
                int elementsDifference = viewModel.SpareParts.Count - job.SpareParts.Count;
                for (int i = 0; i < elementsDifference; i++)
                {
                    job.SpareParts.Add(new SparePart());
                }

                // copy viewmodel data into the job entity
                for (int i = 0; i < viewModel.SpareParts.Count; i++)
                {
                    job.SpareParts[i].Name     = viewModel.SpareParts[i].Name;
                    job.SpareParts[i].Code     = viewModel.SpareParts[i].Code;
                    job.SpareParts[i].Price    = viewModel.SpareParts[i].Price;
                    job.SpareParts[i].Quantity = viewModel.SpareParts[i].Quantity;
                    job.SpareParts[i].Supplier = suppliersRepo.GetSupplierById(
                        viewModel.SpareParts[i].Suppliers.SelectedSupplierId);
                }

                jobsRepo.UpdateJob(job);
                jobsRepo.Save();
                return(RedirectToAction("Details", new { customerId = customerId, carId = carId, jobId = jobId }));
            }

            return(View(viewModel));
        }
コード例 #4
0
 public void Delete(int Id)
 {
     _sparePartRepository.DeleteSparePart(Id);
 }