public async Task <IActionResult> ValidateForgotPassword(LoginViewModel loginViewModel) { Users user = GetUser(loginViewModel.UserName); if (user != null) { string pwd = GeneratePwd(); using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress("*****@*****.**"); mail.To.Add(user.Email); mail.Subject = "Password renewal - Fateh Taxi Management System"; mail.Body = "<h1>Pasword Renewal</h1><p>Your new password is <b>" + pwd + "</b></p><p>Please login using this password and reset on login.</p>"; mail.IsBodyHtml = true; using (SmtpClient smtp = new SmtpClient(_config.GetValue <string>("Email:Smtp:Host"), Convert.ToInt32(_config.GetValue <string>("Email:Smtp:Port")))) { smtp.UseDefaultCredentials = true; smtp.Credentials = new NetworkCredential(_config.GetValue <string>("Email:Smtp:Username"), _config.GetValue <string>("Email:Smtp:Password")); smtp.EnableSsl = true; smtp.Send(mail); } user.IsPasswordReset = true; user.Password = Encryption.Encrypt(pwd); _context.Update(user); await _context.SaveChangesAsync(); } } return(RedirectToAction("Login", "Users")); }
public async Task <IActionResult> Edit(int id, [Bind("OwnerId,LastName,FirstName,Email,PhoneNumber")] Owner owner) { if (id != owner.OwnerId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(owner); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!OwnerExists(owner.OwnerId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(owner)); }
public async Task <IActionResult> AddShift(ScheduleViewModel scheduleViewModel) { if (scheduleViewModel.ScheduleId != 0) {//Edit if (ModelState.IsValid) { Schedule schedule = ConvertViewModeltoSchedule(scheduleViewModel); scheduleViewModel.DisplayEdit = false; try { _context.Update(schedule); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { scheduleViewModel.ScheduleId = 0; if (!ScheduleExists(schedule.ScheduleId)) { return(NotFound()); } else { throw; } } } } else {//Add if (ModelState.IsValid) { Schedule schedule = ConvertViewModeltoSchedule(scheduleViewModel); _context.Add(schedule); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { scheduleViewModel.Error = "Schedule could not be added"; } } scheduleViewModel = Load(scheduleViewModel); return(View("Index", scheduleViewModel)); }
public async Task <IActionResult> Edit(int id, AddTaxiViewModel taxiViewModel) { if (id != taxiViewModel.EditTaxi.TaxiId) { return(NotFound()); } IEnumerable <Taxi> allTaxi = _context.Taxi.Include(x => x.MakeNavigation).Include(y => y.ModelNavigation).ToList(); var allTaxis = new List <TaxiViewModel>(); foreach (var taxi in allTaxi) { var viewModel = MapToTaxiViewModel(taxi); allTaxis.Add(viewModel); } taxiViewModel.Taxis = allTaxis; taxiViewModel.DisplayEdit = false; taxiViewModel.SearchModel = new SearchViewModel(); taxiViewModel.Makes = _context.CarMake.ToList(); taxiViewModel.Models = _context.CarModel.ToList(); taxiViewModel.NewTaxi = new TaxiViewModel(); if (ModelState.IsValid) { try { var currentTaxi = _context.Taxi.FirstOrDefault(x => x.TaxiId == taxiViewModel.EditTaxi.TaxiId); var taxi = MapTaxiViewModelToTaxi(taxiViewModel.EditTaxi, currentTaxi); var regoExists = allTaxis.Any(x => x.Registration == taxiViewModel.EditTaxi.Registration); if (regoExists && currentTaxi.Registration != taxiViewModel.EditTaxi.Registration) { taxiViewModel.EditTaxi = new TaxiViewModel(); taxiViewModel.Error = "The taxi with registration" + currentTaxi.Registration + " already exists."; return(View("Index", taxiViewModel)); } _context.Update(taxi); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TaxiExists(taxiViewModel.EditTaxi.TaxiId)) { return(NotFound()); } else { throw; } } taxiViewModel.EditTaxi = new TaxiViewModel(); return(RedirectToAction(nameof(Index))); } return(RedirectToAction("Index", "Taxis", taxiViewModel)); }
public async Task <IActionResult> SaveDriver(OwnerDriversViewModel owner) { if (owner?.EditDriver?.DriverId == 0) { return(RedirectToAction("Index", "Drivers")); } Driver currentDriver = _context.Driver.Include(x => x.User).FirstOrDefault(x => x.DriverId == owner.EditDriver.DriverId); string userId = HttpContext.Session.GetString("CurrentUserId"); if (string.IsNullOrEmpty(userId)) { return(RedirectToAction("Login", "Users")); } if (owner.EditDriver.Email != currentDriver.Email) { var emailExists = _context.Users.Any(x => x.Email == owner.EditDriver.Email); if (emailExists) { owner.Error = "Driver's email cannot be updated"; return(View("Index", owner)); } } var currentOwner = _context.Owner.FirstOrDefault(m => m.UserId == int.Parse(userId)); var existsInOD = _context.OwnerDriver.Any(x => x.DriverId == owner.EditDriver.DriverId && x.OwnerId == currentOwner.OwnerId); Driver upadteDriver = MapViewModelToDriver(owner.EditDriver, currentDriver); upadteDriver.User.Email = currentDriver.Email; _context.Update(upadteDriver); _context.SaveChanges(); return(RedirectToAction("Index", "Drivers")); }
public async Task <ActionResult> AddEarningsAsync(EarningsViewModel earningsViewModel) { earningsViewModel = validateEarning(earningsViewModel); if (earningsViewModel.Error != null) { return(RedirectToAction("MyEarnings", "Earnings", new { earningsViewModel.ShiftDate, earningsViewModel.EarningsOn, earningsViewModel.UserId, earningsViewModel.WeeklyEarnings, earningsViewModel.MonthlyEarnings, earningsViewModel.DriverId })); } var earning = MapViewModelToEarnings(earningsViewModel); var earningExists = _context.Earnings.FirstOrDefault( x => x.DriverId == earning.DriverId && x.ShiftDate == earning.ShiftDate); if (ModelState.IsValid && earningExists == null) { _context.Add(earning); await _context.SaveChangesAsync(); } else { earningExists.Earning = earning.Earning; earningExists.IncomeEarned = earning.IncomeEarned; earningExists.Expenditure = earning.Expenditure; earningExists.TaxiId = earning.TaxiId; _context.Update(earningExists); await _context.SaveChangesAsync(); } return(RedirectToAction("MyEarnings", "Earnings", new { earningsViewModel.ShiftDate, earningsViewModel.EarningsOn, earning.UserId, earningsViewModel.WeeklyEarnings, earningsViewModel.MonthlyEarnings, earningsViewModel.DriverId })); }