public async Task <IActionResult> AddTour(Tour tour) { var hotelPricePerNight = (await db.Hotels.FirstOrDefaultAsync(h => h.HotelId == tour.HotelId)).PricePerNight; tour.Price = hotelPricePerNight * (tour.AdultNumber + tour.ChildNumber / 2); tour.User = await db.Users.FirstOrDefaultAsync(u => u.Login == User.Identity.Name); tour.TimeBooking = DateTime.Now; await db.Tours.AddAsync(tour); await db.SaveChangesAsync(); return(RedirectToAction("tours")); }
public async Task <IActionResult> Admins(long userId, bool toAdmin = false) { var user = await db.Users.FirstOrDefaultAsync(u => u.UserId == userId); if (toAdmin) { user.RoleId = 1; } else { user.RoleId = 2; } await db.SaveChangesAsync(); return(RedirectToAction("Admins")); }
public async Task <IActionResult> Register(RegisterModel model) { if (ModelState.IsValid) { User user = await db.Users.FirstOrDefaultAsync(u => u.Login == model.Login); if (user == null) { var anyData = await db.Users.AnyAsync(); long lastId = anyData ? await db.Users.MaxAsync(u => u.UserId) : 0; var hashedPassword = new Security().Base64Encode(model.Password); var role = await db.Roles.FirstOrDefaultAsync(r => r.Name == "user"); User newUser = new User { UserId = lastId + 1, Login = model.Login, Password = hashedPassword, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, Role = role }; db.Users.Add(newUser); await db.SaveChangesAsync(); await Authenticate(newUser); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("", "Некорректные логин и/или пароль"); } } return(View(model)); }