public void CreateUser(UserRoleViewModel viewModel) { using (CarDealershipDbContext context = new CarDealershipDbContext()) { viewModel.AppUser.UserName = viewModel.AppUser.Email; var userMgr = new UserManager <AppUser>(new UserStore <AppUser>(context)); var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); if (userMgr.FindByName(viewModel.AppUser.UserName) == null) { var newuser = new AppUser() { FirstName = viewModel.AppUser.FirstName, LastName = viewModel.AppUser.LastName, UserName = viewModel.AppUser.UserName, Email = viewModel.AppUser.Email }; userMgr.Create(newuser, viewModel.Password); dealerDb.SaveChanges(); } var user = userMgr.FindByName(viewModel.AppUser.UserName); var role = dealerDb.Roles.SingleOrDefault(r => r.Id == viewModel.RoleId); userMgr.AddToRole(user.Id, role.Name); dealerDb.SaveChanges(); } }
public void Can_Update_Car() { using (var context = new CarDealershipDbContext(ContextOptions)) { var carService = new CarService(context); int carId = 1; Car updatedCar = new Car() { CarId = 2, Make = "Updated Car", Model = "Updated Car", Year = 2000, Color = "blue", Price = 6000, Owner = "Rodney", Image = "", Date = new DateTime(2021, 01, 01) }; carService.Update(carId, updatedCar); var car = carService.GetById(carId); Assert.NotNull(car); Assert.Equal("Updated Car", car.Make); } }
public void Can_Get_Car_ById() { using (var context = new CarDealershipDbContext(ContextOptions)) { var carService = new CarService(context); int carId = 1; var car = carService.GetById(carId); Assert.NotNull(car); Assert.Equal("Car 1", car.Make); } }
public void Can_Get_User_ById() { using (var context = new CarDealershipDbContext(ContextOptions)) { var userService = new UserService(context); int userId = 1; var user = userService.GetById(userId); Assert.NotNull(user); Assert.Equal("Rodney", user.FirstName); } }
public void Can_Delete_Car() { using (var context = new CarDealershipDbContext(ContextOptions)) { var carService = new CarService(context); int carId = 1; carService.Delete(carId); var cars = carService.GetAll(); Assert.Equal(2, cars.Count); Assert.Equal("Car 2", cars[0].Make); } }
public void Can_Get_All_Cars() { using (var context = new CarDealershipDbContext(ContextOptions)) { var carService = new CarService(context); var cars = carService.GetAll(); Assert.Equal(3, cars.Count); Assert.Equal("Valentin", cars[0].Owner); Assert.Equal("Vandit", cars[1].Owner); Assert.Equal("Rodney", cars[2].Owner); } }
public void Can_Get_All_Users() { using (var context = new CarDealershipDbContext(ContextOptions)) { var userService = new UserService(context); var users = userService.GetAll(); Assert.Equal(3, users.Count); Assert.Equal("Rodney", users[0].FirstName); Assert.Equal("Valentin", users[1].FirstName); Assert.Equal("Vandit", users[2].FirstName); } }
public void Can_Delete_User() { using (var context = new CarDealershipDbContext(ContextOptions)) { var userService = new UserService(context); int userId = 1; userService.Delete(userId); var users = userService.GetAll(); Assert.Equal(2, users.Count); Assert.Equal("Valentin", users[0].FirstName); } }
public ActionResult AddUser() { var context = new CarDealershipDbContext(); var roles = context.Roles; var model = new RegisterViewModel(); model.Roles = roles.Select(r => new SelectListItem { Text = r.Name, Value = r.Id }); return(View(model)); }
public void Cannot_Add_User_WithExistingUserId() { using (var context = new CarDealershipDbContext(ContextOptions)) { var userService = new UserService(context); User existingUser = new User() { UserId = 3, FirstName = "Vandit", LastName = "A", Email = "*****@*****.**", Username = "******", Password = "******", Role = "user" }; Assert.Throws <ArgumentOutOfRangeException>(() => userService.Add(existingUser)); } }
public void Cannot_Update_User_WithNonExistentId() { using (var context = new CarDealershipDbContext(ContextOptions)) { var userService = new UserService(context); int nonExistentUserId = 5; User updatedUser = new User() { UserId = 5, FirstName = "Rodney", LastName = "C", Email = "*****@*****.**", Username = "******", Password = "******", Role = "admin" }; Assert.Throws <ArgumentOutOfRangeException>(() => userService.Update(nonExistentUserId, updatedUser)); } }
public void Cannot_Add_Car_WithExistingCarId() { using (var context = new CarDealershipDbContext(ContextOptions)) { var carService = new CarService(context); Car existingCar = new Car() { CarId = 2, Make = "Car 2", Model = "Car 2", Year = 2000, Color = "blue", Price = 6000, Owner = "Rodney", Image = "", Date = new DateTime(2021, 01, 01) }; Assert.Throws <ArgumentOutOfRangeException>(() => carService.Add(existingCar)); } }
public void Can_Add_User() { using (var context = new CarDealershipDbContext(ContextOptions)) { var userService = new UserService(context); User newUser = new User() { UserId = 4, FirstName = "Fang", LastName = "H", Email = "*****@*****.**", Username = "******", Password = "******", Role = "user" }; userService.Add(newUser); var users = userService.GetAll(); Assert.Equal(4, users.Count); } }
public async Task <ActionResult> AddUser(RegisterViewModel model) { //get the database context var context = new CarDealershipDbContext(); if (ModelState.IsValid) { //get the user var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; var result = await UserManager.CreateAsync(user, model.Password); //successfully got user if (result.Succeeded) { //create a new role manager var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var role = roleManager.FindById(model.Role); UserManager.AddToRole(user.Id, role.Name); return(RedirectToAction("Users", "Admin")); } AddErrors(result); } var roles = context.Roles; //populate roles in model model.Roles = roles.Select(r => new SelectListItem { Text = r.Name, Value = r.Id }); // If we got this far, something failed, redisplay form return(View(model)); }
public void Cannot_Update_Car_WithNonExistentId() { using (var context = new CarDealershipDbContext(ContextOptions)) { var carService = new CarService(context); int nonExistentCarId = 99; Car updatedCar = new Car() { CarId = 99, Make = "Car 2", Model = "Car 2", Year = 2000, Color = "blue", Price = 6000, Owner = "Rodney", Image = "", Date = new DateTime(2021, 01, 01) }; Assert.Throws <ArgumentOutOfRangeException>(() => carService.Update(nonExistentCarId, updatedCar)); } }
public ActionResult EditUser(RegisterViewModel model) { //get user, roles var context = new CarDealershipDbContext(); var roles = context.Roles; var user = UserManager.FindById(model.Id); //get the current role in the db var oldRole = user.Roles.SingleOrDefault().RoleId; if (!string.IsNullOrEmpty(model.EditedPassword)) { user.PasswordHash = UserManager.PasswordHasher.HashPassword(model.EditedPassword); } //edit user user.FirstName = model.FirstName; user.LastName = model.LastName; user.Email = model.Email; user.UserName = model.Email; if (!user.Roles.Any(r => r.RoleId == model.Role)) { //clear all roles from the user var dbUser = context.Users.SingleOrDefault(u => u.Id == model.Id); dbUser.Roles.Clear(); context.SaveChanges(); //get new role from model, remove user from current role, add to new role var newRole = roles.Where(r => r.Id == model.Role).Select(r => r.Name).SingleOrDefault(); UserManager.RemoveFromRole(user.Id, oldRole); UserManager.AddToRole(user.Id, newRole); } UserManager.Update(user); return(RedirectToAction("Users", "Admin")); }
public void EditVehicle(EditVehicleViewModel editVehicle) { using (var newDb = new CarDealershipDbContext()) { //admin controller setup, follow assining of properties VehicleDetails toEdit = newDb.VehicleDetails.FirstOrDefault(v => v.VehicleId == editVehicle.Vehicle.VehicleId); //toEdit = editVehicle.Vehicle; //toEdit.VehicleId = editVehicle.Vehicle.VehicleId; toEdit.ModelName = newDb.CarModel.FirstOrDefault(m => m.ModelID == editVehicle.Vehicle.ModelName.ModelID); //toEdit.ModelName.ModelID = toEdit.ModelName.ModelID; toEdit.BodyName = newDb.BodyStyle.FirstOrDefault(b => b.BodyStyleId == editVehicle.Vehicle.BodyName.BodyStyleId); toEdit.Color = editVehicle.Vehicle.Color; toEdit.Interior = editVehicle.Vehicle.Interior; toEdit.TransmissionType = newDb.Transmission.FirstOrDefault(t => t.TransmissionId == editVehicle.Vehicle.TransmissionType.TransmissionId); toEdit.MSRP = editVehicle.Vehicle.MSRP; toEdit.SalePrice = editVehicle.Vehicle.SalePrice; toEdit.Description = editVehicle.Vehicle.Description; toEdit.VIN = editVehicle.Vehicle.VIN; toEdit.Mileage = editVehicle.Vehicle.Mileage; toEdit.IsFeatured = editVehicle.Vehicle.IsFeatured; toEdit.VehicleType = newDb.VehicleType.FirstOrDefault(y => y.VehicleTypeId == editVehicle.Vehicle.VehicleType.VehicleTypeId); if (editVehicle.VehicleImage != null) { toEdit.ImageUrl = editVehicle.Vehicle.ImageUrl; } toEdit.IsSold = false; newDb.VehicleDetails.AddOrUpdate(toEdit); newDb.SaveChanges(); } }
public ActionResult EditUser(string id) { var context = new CarDealershipDbContext(); var roles = context.Roles.ToList(); var editedUser = UserManager.FindById(id); var model = new RegisterViewModel(); model.Roles = roles.Select(r => new SelectListItem { Text = r.Name, Value = r.Id }); model.Id = editedUser.Id; model.FirstName = editedUser.FirstName; model.LastName = editedUser.LastName; model.Email = editedUser.Email; foreach (var role in editedUser.Roles) { model.Role = role.RoleId; } return(View(model)); }
public void Can_Update_User() { using (var context = new CarDealershipDbContext(ContextOptions)) { var userService = new UserService(context); int userId = 1; User updatedUser = new User() { UserId = 1, FirstName = "Rodney", LastName = "C", Email = "*****@*****.**", Username = "******", Password = "******", Role = "admin" }; userService.Update(userId, updatedUser); var user = userService.GetById(userId); Assert.NotNull(user); Assert.Equal("foodLover", user.Username); } }
public void Can_Add_Car() { using (var context = new CarDealershipDbContext(ContextOptions)) { var carService = new CarService(context); Car newCar = new Car() { CarId = 5, Make = "New car", Model = "New car", Year = 2000, Color = "blue", Price = 6000, Owner = "Rodney", Image = "", Date = new DateTime(2021, 01, 01) }; carService.Add(newCar); var cars = carService.GetAll(); Assert.Equal(4, cars.Count); } }
private void Seed() { using (var context = new CarDealershipDbContext(ContextOptions)) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); // -------------------------------------------------------- // Seed data for cars context.Cars.Add(new Car { CarId = 2, Make = "Car 1", Model = "Car 1", Year = 2000, Color = "blue", Price = 6000, Owner = "Valentin", Image = "", Date = new DateTime(2021, 01, 01) }); context.Cars.Add(new Car { CarId = 2, Make = "Car 2", Model = "Car 2", Year = 2000, Color = "blue", Price = 6000, Owner = "Vandit", Image = "", Date = new DateTime(2021, 01, 01) }); context.Cars.Add(new Car { CarId = 2, Make = "Car 2", Model = "Car 2", Year = 2000, Color = "blue", Price = 6000, Owner = "Rodney", Image = "", Date = new DateTime(2021, 01, 01) }); // -------------------------------------------------------- // Seed data for users context.Users.Add(new User { UserId = 1, FirstName = "Rodney", LastName = "C", Email = "*****@*****.**", Username = "******", Password = "******", Role = "admin" }); context.Users.Add(new User { UserId = 2, FirstName = "Valentin", LastName = "E", Email = "*****@*****.**", Username = "******", Password = "******", Role = "user" }); context.Users.Add(new User { UserId = 3, FirstName = "Vandit", LastName = "A", Email = "*****@*****.**", Username = "******", Password = "******", Role = "user" }); context.SaveChanges(); } }
public CarService(CarDealershipDbContext db) { _db = db; }
public CarController(CarDealershipDbContext context) { _context = context; }
public CarsController(CarDealershipDbContext carDealershipDbContext) { _carDealershipDbContext = carDealershipDbContext; }
public ColorsController(CarDealershipDbContext context) { this._context = context; }