Example #1
0
 public void Add(Car newCar)
 {
     if (_db.Cars.Any(car => car.CarId == newCar.CarId))
     {
         throw new ArgumentOutOfRangeException("Car id already in use.");
     }
     _db.Cars.Add(newCar);
     _db.SaveChanges();
 }
Example #2
0
 public void Add(User newUser)
 {
     if (_db.Users.Any(user => user.UserId == newUser.UserId))
     {
         throw new ArgumentOutOfRangeException("User id already in use.");
     }
     _db.Users.Add(newUser);
     _db.SaveChanges();
 }
Example #3
0
        public IActionResult Post([FromBody] Car car)
        {
            if (car == null)
            {
                return(BadRequest());
            }

            _carDealershipDbContext.Cars.Add(car);
            _carDealershipDbContext.SaveChanges();

            return(CreatedAtRoute("GetCars", new { id = car.Id }, car));
        }
Example #4
0
        public void AddPurchase(PurchaseViewModel purchase)
        {
            Sale carPurchase = purchase.SaleDetails;

            carPurchase.DateSold     = DateTime.Now;
            carPurchase.User         = purchase.SaleDetails.User;
            carPurchase.PurchaseType = dealerDb.Purchase.SingleOrDefault(p => p.PurchaseTypeId == purchase.PurchaseTypeDetails.PurchaseTypeId);

            VehicleDetails vehicleSold = dealerDb.VehicleDetails.SingleOrDefault(v => v.VehicleId == purchase.VehiclePurchaseId);

            carPurchase.IsSold     = true;
            carPurchase.IsFeatured = false;
            carPurchase.Vehicle    = vehicleSold;
            carPurchase.VehicleId  = vehicleSold.VehicleId;

            dealerDb.SaveChanges();
            dealerDb.Sale.Add(carPurchase);
            dealerDb.SaveChanges();
        }
        public IActionResult AddCarToFavorites(Car car)
        {
            AspNetUsers thisUser = _context.AspNetUsers.Where(u => u.UserName == User.Identity.Name).First();

            FavoriteCars newCar = new FavoriteCars();

            newCar.CarId  = car.Id;
            newCar.Make   = car.Make;
            newCar.Model  = car.Model;
            newCar.Year   = car.Year;
            newCar.Color  = car.Color;
            newCar.UserId = thisUser.Id;
            newCar.User   = thisUser;

            if (ModelState.IsValid)
            {
                _context.FavoriteCars.Add(newCar);
                _context.SaveChanges();
                return(RedirectToAction("ListCars", "Car"));
            }
            return(View("ListCars"));
        }
Example #6
0
        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"));
        }
Example #7
0
        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();
            }
        }
        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();
            }
        }