public async Task <ActionResult> CompleteSchedule(CompleteScheduleViewModel model) { if (ModelState.IsValid) { var service = await _serviceRepository.GetByIdAsync(model.ServiceId); var activeSchedule = _converterHelper.ToActiveSchedule(model, service); var vehicle = await _vehicleRepository.GetByIdAsync(model.VehicleId); var dealership = await _dealershipRepository.GetByIdAsync(model.DealershipId); var scheduleDetail = _converterHelper.ToScheduleDetail(vehicle, activeSchedule, dealership); try { await _scheduleDetailRepository.CreateAsync(scheduleDetail); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "There is already a schedule for that service"); return(View(model)); } } } return(View(model)); }
public async Task <Employee> ToEmplyoyeeFromEditViewModelAsync(EditEmployeeViewModel model, User user) { return(new Employee { Id = model.EmployeeId, IsActive = model.IsActive, Dealership = await _dealershipRepository.GetByIdAsync(model.DealershipId), Department = await _departmentRepository.GetByIdAsync(model.DepartmentId), User = user, }); }
public async Task <IActionResult> Edit(Dealership dealership) { if (ModelState.IsValid) { var dShip = await _dealershipRepository.GetByIdAsync(dealership.Id); if (dealership.IsActive == true) { try { var services = await _serviceRepository.GetAllServicesAsync(); foreach (var item in services) { await _servicesSuppliedRepository.AddServicesToDealershipAsync(item, dShip); } var departments = await _departmentRepository.GetDepartments(); foreach (var item in departments) { await _dealershipDepartmentRepository.AddDepartmentToDealershipAsync(item, dShip); } dShip.IsActive = true; dShip.UpdateDate = DateTime.Now; var zipcode = await _zipCodeRepository.GetByIdAsync(dealership.ZipCodeId); dShip.ZipCode = zipcode; await _dealershipRepository.UpdateAsync(dShip); } catch (DbUpdateConcurrencyException) { if (!await _dealershipRepository.ExistsAsync(dealership.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } else { try { var departments = _dealershipDepartmentRepository.GetDealershipDepartments(dShip.Id); await _dealershipDepartmentRepository.DeleteDEalershipDepartmentsAsync(departments); var services = _servicesSuppliedRepository.GetWithServicesByDealershipId(dShip.Id); await _dealershipServiceRepository.DeleteServicesFromDealershipAsync(services); dShip.IsActive = false; dealership.UpdateDate = DateTime.Now; var zipcode = await _zipCodeRepository.GetByIdAsync(dShip.ZipCodeId); dShip.ZipCode = zipcode; await _dealershipRepository.UpdateAsync(dShip); return(RedirectToAction("Index")); } catch (DbUpdateConcurrencyException) { if (!await _dealershipRepository.ExistsAsync(dealership.Id)) { return(NotFound()); } else { throw; } } } } return(View(dealership)); }
public async Task <IActionResult> CreateEmployee(CreateEmployeeViewModel model) { if (ModelState.IsValid) { var user = _converterHelper.ToEmployeeUser(model.UserName, model.User); if (await _userHelper.GetUserByEmailAsync(user.UserName) == null) { user.IsActive = true; user.CanLogin = true; var result = await _userHelper.AddUserAsync(user, model.Password); if (result != IdentityResult.Success) { this.ModelState.AddModelError(string.Empty, "The user could not be created"); return(this.View(model)); } var dealership = await _dealershipRepository.GetByIdAsync(model.DealershipId); var department = await _departmentRepository.GetByIdAsync(model.DepartmentId); var employee = _converterHelper.ToNewEmplyee(dealership, department, user); employee.CreationDate = DateTime.Now; try { employee.IsActive = true; await _employeeRepository.CreateAsync(employee); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ViewBag.Error = $"There is allready a Employee registered with the name {user.FullName} please insert another"; var dealerships = _dealershipRepository.GetAll(); var departments = _departmentRepository.GetAll(); var modelret = _converterHelper.ToCreateEmployeeVieModel(dealerships, departments); return(View(modelret)); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); var dealerships = _dealershipRepository.GetAll(); var departments = _departmentRepository.GetAll(); var modelret = _converterHelper.ToCreateEmployeeVieModel(dealerships, departments); return(View(modelret)); } } await _userHelper.CheckRoleAsync(employee.Role); var userRole = await _userHelper.IsUSerInRoleAsync(user, employee.Role); if (!userRole) { await _userHelper.AddUserToRoleAsync(user, employee.Role); } var myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user); var tokenLink = this.Url.Action("ConfirmEmail", "Accounts", new { userId = user.Id, token = myToken, }, protocol: HttpContext.Request.Scheme); _mailHelper.SendEmail(user.UserName, "Email Confirmation", $"<h1>Email Confirmation</h1>" + $"To allow the user, " + $"plase click in this link:</br></br><a href = \"{tokenLink}\">Confirm Email</a>" + $"<p>Your password is {model.Password}, you must change it in first login</p>"); this.ViewBag.Message = "The instructions for completing registration have been sent by email for the user"; return(View(model)); } this.ModelState.AddModelError(string.Empty, $"the user {user.UserName} allready exists"); var dealershipsret = _dealershipRepository.GetAll(); var departmentsret = _departmentRepository.GetAll(); var modelreturn = _converterHelper.ToCreateEmployeeVieModel(dealershipsret, departmentsret); return(View(modelreturn)); } var dealershipsstate = _dealershipRepository.GetAll(); var departmentsstate = _departmentRepository.GetAll(); var modelstate = _converterHelper.ToCreateEmployeeVieModel(dealershipsstate, departmentsstate); return(View(modelstate)); }