Ejemplo n.º 1
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));
        }
        public async Task <IActionResult> DeleteSchedule(int?Id)
        {
            var userName = this.User.Identity.Name;

            var user = await _userHelper.GetUserByEmailAsync(userName);


            if (user.IsActive == false)
            {
                return(RedirectToAction("EditUser", "Accounts"));
            }

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

            var scheduleDetail = await _scheduleDetailRepository.GetScheduleDetailByIdAsync(Id.Value);


            var model = _converterHelper.ToDeleteScheduleViewModel(scheduleDetail);

            return(View(model));
        }