Beispiel #1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var repToDelete = await _context.Repair.Include(r => r.Car).FirstAsync(r => r.RepairID == id);

            _context.Entry(repToDelete)
            .Property("RowVersion").OriginalValue = Repair.RowVersion;
            _context.Entry(repToDelete.Car)
            .Property("RowVersion").OriginalValue = Repair.Car.RowVersion;
            try
            {
                if (repToDelete != null)
                {
                    if (repToDelete.Car != null)
                    {
                        _context.Car.Remove(repToDelete.Car);
                    }
                    _context.Repair.Remove(repToDelete);
                    await _context.SaveChangesAsync();
                }
                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateConcurrencyException)
            {
                return(RedirectToPage("./Delete",
                                      new { concurrencyError = true, id = id }));
            }
        }
Beispiel #2
0
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }


            var repairToUpdate = await _context.Repair.FindAsync(id);

            if (repairToUpdate == null)
            {
                return(NotFound());
            }

            _context.Entry(repairToUpdate)
            .Property("RowVersion").OriginalValue = Repair.RowVersion;

            if (await TryUpdateModelAsync <Repair>(repairToUpdate, "Repair",
                                                   s => s.Description, s => s.startTime, s => s.WorkPrice, s => s.ClientID, s => s.ProblemDescription, s => s.RepairState, s => s.ChangeOil))
            {
                _logger.LogInformation(repairToUpdate.CarID.ToString());
                var carToUpdate = await _context.Car.FindAsync(repairToUpdate.CarID);


                _context.Entry(carToUpdate)
                .Property("RowVersion").OriginalValue = Repair.Car.RowVersion;

                if (await TryUpdateModelAsync <Car>(carToUpdate, "Repair.Car",
                                                    s => s.Brand, s => s.Model, s => s.productionYear, s => s.EngineCapacity, s => s.EngineFuel, s => s.oilChangeDate, s => s.BodyType))
                {
                    /*
                     * carToUpdate.Brand = Repair.Car.Brand;
                     * carToUpdate.Model = Repair.Car.Model;
                     * carToUpdate.productionYear = Repair.Car.productionYear;
                     * carToUpdate.EngineCapacity = Repair.Car.EngineCapacity;
                     * carToUpdate.EngineFuel = Repair.Car.EngineFuel;
                     * carToUpdate.oilChangeDate = Repair.Car.oilChangeDate;
                     * carToUpdate.BodyType = Repair.Car.BodyType;
                     */
                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        var exceptionEntry = ex.Entries.Single();
                        var databaseEntry  = exceptionEntry.GetDatabaseValues();
                        if (databaseEntry == null)
                        {
                            ModelState.AddModelError(string.Empty, "Unable to save. " +
                                                     "The repair was deleted by another user.");
                            return(Page());
                        }

                        var dbValues = databaseEntry.ToObject();
                        ModelState.AddModelError(string.Empty,
                                                 "The Repair 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. If you still want to edit this record, click "
                                                 + "the Save button again.");
                        // Save the current RowVersion so next postback
                        // matches unless an new concurrency issue happens.
                        if (dbValues.GetType().Equals(typeof(Repair)))
                        {
                            var rep = (Repair)dbValues;
                            Repair.RowVersion = (byte[])rep.RowVersion;
                            ModelState.Remove("Repair.RowVersion");
                        }
                        else
                        {
                            var car = (Car)dbValues;
                            Repair.Car.RowVersion = (byte[])car.RowVersion;
                            ModelState.Remove("Repair.Car.RowVersion");
                        }



                        return(Page());

                        /*
                         * if (!RepairExists(Repair.RepairID))
                         * {
                         *  return NotFound();
                         * }
                         *
                         *
                         * if (!CarExists(Repair.Car.CarID))
                         * {
                         *  return NotFound();
                         * }
                         * throw;*/
                    }
                }
            }

            return(RedirectToPage("./Index"));
        }
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var partToUpdate = await _context.ReplacedPart
                               .Include(rp => rp.OldPartImage)
                               .Include(rp => rp.NewPartBill)
                               .FirstOrDefaultAsync(rp => rp.ReplacedPartID == id.Value);

            if (partToUpdate == null)
            {
                return(NotFound());
            }

            _context.Entry(partToUpdate)
            .Property("RowVersion").OriginalValue = ReplacedPart.RowVersion;

            if (await TryUpdateModelAsync <ReplacedPart>(partToUpdate, "ReplacedPart",
                                                         p => p.Name, p => p.Manufacturer, p => p.ProductionDate, p => p.Quantity, p => p.Price))
            {
                string wwwRoot = _hostEnviroment.WebRootPath;
                if (ReplacedPart.OldPartImage.File != null)
                {
                    string extension = Path.GetExtension(ReplacedPart.OldPartImage.File.FileName);
                    ReplacedPart.OldPartImage.FileName = Path.GetRandomFileName() + DateTime.Now.ToString("yyyyMMdd_HH_mm_ss") + extension;
                    string path = Path.Combine(wwwRoot + "/imgs/oldparts/", ReplacedPart.OldPartImage.FileName);

                    if (partToUpdate.OldPartImage != null)
                    {
                        var imagePath = Path.Combine(wwwRoot, "imgs/oldparts", partToUpdate.OldPartImage.FileName);
                        if (System.IO.File.Exists(imagePath))
                        {
                            System.IO.File.Delete(imagePath);
                            partToUpdate.OldPartImage.FileName = ReplacedPart.OldPartImage.FileName;
                            partToUpdate.OldPartImage.Title    = ReplacedPart.OldPartImage.Title;
                        }
                    }
                    else
                    {
                        var image = new FileModel
                        {
                            Title    = ReplacedPart.OldPartImage.Title,
                            FileName = ReplacedPart.OldPartImage.FileName
                        };
                        _context.Files.Add(image);
                        partToUpdate.OldPartImage = image;
                    }


                    using (var fileStream = new FileStream(path, FileMode.Create))
                    {
                        await ReplacedPart.OldPartImage.File.CopyToAsync(fileStream);
                    }
                }

                if (ReplacedPart.NewPartBill != null)
                {
                    string extension = Path.GetExtension(ReplacedPart.NewPartBill.File.FileName);
                    ReplacedPart.NewPartBill.FileName = Path.GetRandomFileName() + DateTime.Now.ToString("yyyyMMdd_HH_mm_ss") + extension;
                    string path = Path.Combine(wwwRoot + "/bills/", ReplacedPart.NewPartBill.FileName);

                    if (partToUpdate.NewPartBill != null)
                    {
                        var imagePath = Path.Combine(wwwRoot, "bills", partToUpdate.NewPartBill.FileName);
                        if (System.IO.File.Exists(imagePath))
                        {
                            System.IO.File.Delete(imagePath);
                            partToUpdate.NewPartBill.FileName = ReplacedPart.NewPartBill.FileName;
                            partToUpdate.NewPartBill.Title    = ReplacedPart.NewPartBill.Title;
                        }
                    }
                    else
                    {
                        var billModel = new FileModel
                        {
                            Title    = ReplacedPart.NewPartBill.Title,
                            FileName = ReplacedPart.NewPartBill.FileName
                        };
                        _context.Files.Add(billModel);
                        partToUpdate.NewPartBill = billModel;
                    }


                    using (var fileStream = new FileStream(path, FileMode.Create))
                    {
                        await ReplacedPart.NewPartBill.File.CopyToAsync(fileStream);
                    }
                }


                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save. " +
                                                 "The replaced part was deleted by another user.");
                        return(Page());
                    }

                    var dbValues = (ReplacedPart)databaseEntry.ToObject();
                    ModelState.AddModelError(string.Empty,
                                             "The Replaced Part 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. If you still want to edit this part, click "
                                             + "the Save button again.");


                    ReplacedPart.RowVersion = (byte[])dbValues.RowVersion;
                    // Clear the model error for the next postback.
                    ModelState.Remove("ReplacedPart.RowVersion");

                    return(Page());
                }
            }
            return(RedirectToPage("/Repairs/Index", "id", new { id = partToUpdate.RepairID }));
        }
        public async Task OnGetAsync(int?id, string sortOrder, int?pageIndex, string searchString, string currentFilter, string searchUser)
        {
            CurrentSort = sortOrder;
            RepairData  = new RepairIndexData();

            DateSort = String.IsNullOrEmpty(sortOrder) ? "date_desc" : "";
            //NameSort = sortOrder == "Date" ? "date_desc" : "Date";

            IQueryable <Repair> repairsIQ;

            if (searchString != null)
            {
                pageIndex = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            if (User.IsInRole("Client"))
            {
                repairsIQ = from r in _context.Repair
                            .Include(r => r.Client)
                            .Where(r => r.Client.UserName == User.Identity.Name)
                            .Include(r => r.AssignedMechanic)
                            //.Include(r => r.ReplacedParts)
                            select r;



                //RepairData.Repairs = await _context.Repair
                //    .Include(r => r.Client)
                //    .Where(r => r.Client.UserName == User.Identity.Name)
                //    .Include(r => r.AssignedMechanic)
                //    .Include(r => r.ReplacedParts)
                //    .AsNoTracking()
                //    .OrderByDescending(r => r.startTime)
                //    .ToListAsync();



                //Repair = await _context.Repair
                //    .Include(r => r.Client)
                //    .Where( r => r.Client.UserName == User.Identity.Name)
                //    .Include( r => r.AssignedMechanic)
                //    .AsNoTracking()
                //    .ToListAsync();
            }
            else
            {
                repairsIQ = from r in _context.Repair
                            .Include(r => r.Client)
                            //.Where(r => r.Client.UserName == User.Identity.Name)
                            .Include(r => r.AssignedMechanic)
                            // .Include(r => r.ReplacedParts)
                            select r;

                if (!String.IsNullOrEmpty(searchString))
                {
                    repairsIQ = repairsIQ.Where(s => s.Client.FirstName.Contains(searchString) ||
                                                s.Client.LastName.Contains(searchString));
                    id = null;
                }

                if (!String.IsNullOrEmpty(searchUser))
                {
                    repairsIQ = repairsIQ.Where(s => s.AssignedMechanic.UserName == searchUser);
                    id        = null;
                }
            }

            switch (sortOrder)
            {
            case "date_desc":
                repairsIQ = repairsIQ.OrderByDescending(s => s.startTime);
                break;

            default:
                repairsIQ = repairsIQ.OrderBy(s => s.startTime);
                break;
            }

            int pageSize = 5;

            RepairData.Repairs = await PaginatedList <Repair> .CreateAsync(
                repairsIQ, pageIndex ?? 1, pageSize);

            // Lazy loading for replaced parts
            if (id != null)
            {
                RepairID = id.Value;
                Repair rep = RepairData.Repairs.Single(r => r.RepairID == id.Value);
                //RepairData.ReplacedParts = rep.ReplacedParts;
                await _context.Entry(rep).Collection(x => x.ReplacedParts).LoadAsync();

                RepairData.ReplacedParts = rep.ReplacedParts;
                RepairData.blockNewParts = rep.InvoiceIssued;
            }
        }
Beispiel #5
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }


            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var visitToUpdate = await _context.Visit.FindAsync(id);

            if (visitToUpdate == null)
            {
                return(NotFound());
            }

            var acceptedDate = visitToUpdate.PlannedVisitDate;



            _context.Entry(visitToUpdate)
            .Property("RowVersion").OriginalValue = Visit.RowVersion;
            if (await TryUpdateModelAsync <Visit>(visitToUpdate, "Visit"
                                                  , s => s.PlannedVisitDate, s => s.AcceptedClient, s => s.AcceptedMechanic, s => s.VisitMechanicID, s => s.VisitPurpose))
            {
                if (DateTime.Compare(acceptedDate, visitToUpdate.PlannedVisitDate) != 0)
                {
                    if (User.IsInRole("Mechanic"))
                    {
                        visitToUpdate.AcceptedClient = false;
                    }
                    else
                    {
                        visitToUpdate.AcceptedMechanic = false;
                    }
                }


                try
                {
                    await _context.SaveChangesAsync();
                }

                catch (DbUpdateConcurrencyException ex)
                {
                    var exceptionEntry = ex.Entries.Single();
                    var databaseEntry  = exceptionEntry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to save. " +
                                                 "The visit was canceled by another user.");
                        return(Page());
                    }

                    var dbValues = (Visit)databaseEntry.ToObject();
                    ModelState.AddModelError(string.Empty,
                                             "The Visit 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. If you still want to edit this visit, click "
                                             + "the Save button again.");


                    Visit.RowVersion = (byte[])dbValues.RowVersion;
                    // Clear the model error for the next postback.
                    ModelState.Remove("Visit.RowVersion");

                    return(Page());
                }
            }


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