Пример #1
0
        public List<Car> GetAllCars()
        {
            List<Car> cars = new List<Car>();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand("SELECT * FROM Car", conn);
                command.Connection.Open();
                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Car newCar = new Car();
                    newCar.Id = reader.GetInt32(0);
                    newCar.Make = reader.GetString(1);
                    newCar.Model = reader.GetString(2);
                    newCar.Year = reader.GetString(3);
                    newCar.ImageUrl = reader.GetString(4);
                    newCar.Title = reader.GetString(5);
                    newCar.Description = reader.GetString(6);
                    newCar.Price = reader.GetDecimal(7);
                    cars.Add(newCar);
                }
            }

            return cars;
        }
        //[Authorize(Users='bob')]
        //[Authorize(Roles='bob')]
        // [Authorize]
        public HttpResponseMessage PostCars(Car car)
        {
            // var claimsUser = this.User as ClaimsPrincipal;
            //if (!claimsUser.HasClaim("CanEditCars", "true")) {
            //    return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "None Shall Pass");
            //}

            if (ModelState.IsValid) {
                if (car.Id == 0) {
                    _db.Cars.Add(car);
                    _db.SaveChanges();
                }
                else {
                    var original = _db.Cars.Find(car.Id);
                    original.Make = car.Make;
                    original.Model = car.Model;
                    original.Price = car.Price;
                    original.Picture = car.Picture;
                    original.BriefDescription = car.BriefDescription;
                    original.FullDescription = car.FullDescription;
                    _db.SaveChanges();
                }

                return Request.CreateResponse(HttpStatusCode.Created, car);
            }
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState);
        }
Пример #3
0
        public void AddCar(Car car)
        {
            var largest = cars.OrderByDescending(x => x.Id).First().Id;
            car.Id = largest + 1;
            cars.Add(car);

            //car.Id = cars.Max(x => x.ID)
        }
Пример #4
0
 public ActionResult Add(Car newCar)
 {
     if (ModelState.IsValid)
     {
         _repo.AddCar(newCar);
         return RedirectToAction("Index");
     }
     else
     {
         return View("Add");
     }
 }
Пример #5
0
 public ActionResult AddNewCar(Car newCar)
 {
     ViewBag.Message = "Add a new Car!";
     if (ModelState.IsValid)
     {
         DBCarRepository car = new DBCarRepository();
         car.AddCar(newCar);
         return RedirectToAction("Index");
     }
     else
     {
         return View("Add");
     }
 }
Пример #6
0
        public ActionResult Add(Car newCar)
        {
            ViewBag.Message = "Enter a new car for sale using the form below!";

            if (ModelState.IsValid)
            {
                _repo.AddCar(newCar);
                return RedirectToAction("Index");
            }
            else
            {
                return View("Add");
            }
        }
Пример #7
0
        public void TestAddToRepo()
        {
            Car c = new Car()
            {
                Description = "Spam",
                Title = "Spam",
                Year = "1987",
                Make = "Nissan",
                Model = "Stanza",
                Price = 9999.00M,
                ImageUrl = "http://carphotos.cardomain.com/ride_images/1/1287/3601/3216800003_large.jpg"
            };

            var _repo = new DBCarRepository();
            _repo.AddCar(c);
        }
Пример #8
0
        public void AddCar(Car car)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                var commandText = "INSERT INTO Car (Make, Model, Year, ImageUrl, Title, Description, Price) VALUES (@Make, @Model, @Year, @ImageUrl, @Title, @Description, @Price)";
                SqlCommand command = new SqlCommand(commandText, conn);
                command.Parameters.AddWithValue("@Make", car.Make);
                command.Parameters.AddWithValue("@Model", car.Model);
                command.Parameters.AddWithValue("@Year", car.Year);
                command.Parameters.AddWithValue("@ImageUrl", car.ImageUrl);
                command.Parameters.AddWithValue("@Description", car.Description);
                command.Parameters.AddWithValue("@Title", car.Title);
                command.Parameters.AddWithValue("@Price", car.Price);

                command.Connection.Open();
                command.ExecuteNonQuery();
            }
        }
Пример #9
0
        public void EditCar(Car car)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                var commandText = "UPDATE Car SET Make = @Make, Model = @Model, Year = @Year, ImageUrl = @ImageUrl, Title = @Title, Description = @Description, Price = @Price WHERE Id = @Id";
                SqlCommand command = new SqlCommand(commandText, conn);
                command.Parameters.AddWithValue("@Id", car.Id);
                command.Parameters.AddWithValue("@Make", car.Make);
                command.Parameters.AddWithValue("@Model", car.Model);
                command.Parameters.AddWithValue("@Year", car.Year);
                command.Parameters.AddWithValue("@ImageUrl", car.ImageUrl);
                command.Parameters.AddWithValue("@Description", car.Description);
                command.Parameters.AddWithValue("@Title", car.Title);
                command.Parameters.AddWithValue("@Price", car.Price);

                command.Connection.Open();
                command.ExecuteNonQuery();
            }
        }
 public void AddCar(Car car)
 {
     using (CarDBEntities CarDB = new CarDBEntities())
     {
         var newCar = new EFCar();
         {
             newCar.Id = car.Id;
             newCar.Make = car.Make;
             newCar.Model = car.Model;
             newCar.Year = car.Year;
             newCar.ImageUrl = car.ImageUrl;
             newCar.Title = car.Title;
             newCar.Description = car.Description;
             newCar.Price = (decimal)car.Price;
         };
         CarDB.EFCars.Add(newCar);
         CarDB.SaveChanges();
     }
 }
Пример #11
0
        public void TestEditRepo()
        {
            var _repo = new DBCarRepository();
            var car = _repo.GetAllCars().FirstOrDefault(x => x.Make == "Nissan" && x.Model == "Stanza" && x.Year == "1987");

            if (car != null)
            {
                Car c = new Car()
                {
                    Id = car.Id,
                    Description = "Well, well, well, what do we have here? A car with four doors? Check. A car with a solid roof? Check. A car with rearview mirrors so you can really connect with that Pearl Jam song? Psh, DOUBLE CHECK. We here at SWCG understand that your discriminating tastes demand that you're able to stand up to authority in style, and this 1987 Nissan Stanza virtually screams: \"Live free or die!\". Rest assured, the seat belts will help ensure that you can live free AND live. That's the SWCG guarentee. You're welcome. ",
                    Title = "PATRIOTIC Nissan Stanza from the year of our Lord 1987",
                    Year = "1987",
                    Make = "Nissan",
                    Model = "Stanza",
                    Price = 9999.00M,
                    ImageUrl = "http://carphotos.cardomain.com/ride_images/1/1287/3601/3216800003_large.jpg"
                };

                _repo.EditCar(c);
            }
        }
        public List<Car> GetAllCars()
        {
            List<Car> cars = new List<Car>();

            using (CarDBEntities CarDB = new CarDBEntities())
            {
                foreach(var item in CarDB.EFCars)
                {
                    Car newCar = new Car();
                    newCar.Id = item.Id;
                    newCar.Make = item.Make;
                    newCar.Model = item.Model;
                    newCar.Year = item.Year;
                    newCar.ImageUrl = item.ImageUrl;
                    newCar.Title = item.Title;
                    newCar.Description = item.Description;
                    newCar.Price = item.Price;
                    cars.Add(newCar);
                }
            }
            return cars;
        }
Пример #13
0
        public Car GetCarByModel(string name)
        {
            Car newCar = new Car();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand("SELECT * FROM Car WHERE Model = '" + name + "'", conn);
                command.Connection.Open();
                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    newCar = new Car();
                    newCar.Id = reader.GetInt32(0);
                    newCar.Make = reader.GetString(1);
                    newCar.Model = reader.GetString(2);
                    newCar.Year = reader.GetString(3);
                    newCar.ImageUrl = reader.GetString(4);
                    newCar.Title = reader.GetString(5);
                    newCar.Description = reader.GetString(6);
                    newCar.Price = reader.GetDecimal(7);
                }
            }
            return newCar;
        }
Пример #14
0
 public ActionResult EditCar(Car editCar)
 {
     ViewBag.Message = "Edit your car!";
     if (ModelState.IsValid)
     {
         DBCarRepository car = new DBCarRepository();
         car.EditCar(editCar);
         return RedirectToAction("Index");
     }
     else
     {
         return View("Edit");
     }
 }
 public void EditCar(Car car)
 {
     throw new NotImplementedException();
 }
Пример #16
0
 public void EditCar(Car car)
 {
     this.DeleteCar(car.Id);
     cars.Add(car);
 }
Пример #17
0
 public void AddCar(Car car)
 {
     car.Id = cars.Max(x => x.Id) + 1;
     cars.Add(car);
 }