public async Task <IActionResult> DeleteSchedule(DeleteScheduleViewModel model)
        {
            if (ModelState.IsValid)
            {
                var activeSchedule = await _activeScheduleRepository.GetByIdAsync(model.ActiveScheduleId);

                var scheduleDetail = await _scheduleDetailRepository.GetScheduleDetailAsync(model.ActiveScheduleId);

                if (activeSchedule == null)
                {
                    return(View(model));
                }

                try
                {
                    await _scheduleDetailRepository.DeleteAsync(scheduleDetail);

                    await _activeScheduleRepository.DeleteAsync(activeSchedule);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception)
                {
                    throw;
                }
            }

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> FinishRepair(FinishRepairViewModel model)
        {
            if (model == null)
            {
                return(View(model));
            }

            var repairShedule = await _repairScheduleRepository.GetRepairScheduleFinishAsync(model.RepairScheduleID);

            var scheduleDetail = await _scheduleDetailRepository.GetScheduleDetailByIdAsync(model.ScheduleDetailId);

            var repair = await _repairRepository.GetByIdAsync(model.RepairId);

            var activeSchedule = await _activeScheduleRepository.GetByIdAsync(scheduleDetail.ActiveSchedule.Id);

            var repairHistory = _converterHelper.ToRepairHistory(repairShedule, scheduleDetail);

            var vehicle = await _vehicleRepository.GetUserVehicle(model.VehicleId);

            var user = await _userHelper.GetUserByIdAsync(vehicle.User.Id);



            try
            {
                await _repairHistoryRepository.CreateAsync(repairHistory);

                await _repairScheduleRepository.DeleteAsync(repairShedule);

                await _scheduleDetailRepository.DeleteAsync(scheduleDetail);

                await _repairRepository.DeleteAsync(repair);

                await _activeScheduleRepository.DeleteAsync(activeSchedule);

                return(RedirectToAction("DealershipRepairs"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.InnerException.Message);
            }

            if (user == null)
            {
                ModelState.AddModelError(string.Empty, "Unable to send email for the user");
            }
            else
            {
                _mailHelper.SendEmail(user.UserName, "Repair finished", $"<h1>Your repair information</h1></br><p>Your vehicle {vehicle.LicencePlate} is ready to be picked up at the workshop</p> </br>");
                ViewBag.Message = "An email was sent to the user with this information";
            }



            return(View(model));
        }