Ejemplo n.º 1
0
        public ActionResult EditCar(Car car)
        {
            var newCar = _db.Cars.Find(car.Id);

            newCar.Name       = car.Name;
            newCar.ChangeDate = DateTime.Now;

            _db.Entry(newCar).State = EntityState.Modified;
            _db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult Create([Bind(Include = "CarModelID,CarMakeID,CarTypeID,Model,Color")] CarModel carModel,
                                   HttpPostedFileBase upload)
        {
            if (upload == null)
            {
                ModelState.AddModelError("FileURL", "Please upload an image.");
            }

            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    //carImage.CarModel = carModel;
                    var entry = db.CarImages.FirstOrDefault(e => e.CarModelID == carModel.CarModelID);
                    if (entry != null)
                    {
                        entry.FileName    = System.IO.Path.GetFileName(upload.FileName);
                        entry.ContentType = upload.ContentType;
                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            entry.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        db.Entry(entry).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        var carImage = new CarImage
                        {
                            FileName    = System.IO.Path.GetFileName(upload.FileName),
                            ContentType = upload.ContentType
                        };
                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            carImage.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        carImage.CarModel = carModel;
                        db.CarImages.Add(carImage);
                    }
                }
                db.CarModels.Add(carModel);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.CarMakeID = new SelectList(db.CarMakes, "CarMakeID", "Make", carModel.CarMakeID);
            ViewBag.CarTypeID = new SelectList(db.CarTypes, "ID", "Type", carModel.CarTypeID);
            return(View(carModel));
        }
        public async Task <string> PutBrand(int id, Brand brand)
        {
            if (id != brand.Id)
            {
                return("0");
            }

            _context.Entry(brand).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BrandExists(id))
                {
                    return("0");
                }
                else
                {
                    throw;
                }
            }
            _context.SaveChanges();
            return("{\"id\":" + id + ",\"stt\":\"" + brand.Status + "\"}");
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.ID)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> PutCarSpecification(int id, CarSpecification carSpecification)
        {
            if (id != carSpecification.CarId)
            {
                return(BadRequest());
            }


            _context.Entry(carSpecification).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_context.CarSpecifications.Any(e => e.CarId == id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 6
0
        public IHttpActionResult PutCarStock(int id, CarStock carStock)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != carStock.Id)
            {
                return(BadRequest());
            }

            db.Entry(carStock).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarStockExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <string> PutCategory(int id, Category category)
        {
            if (id != category.ID)
            {
                return("0");
            }

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return("0");
                }
                else
                {
                    throw;
                }
            }
            _context.SaveChanges();
            return("{\"id\":" + id + ",\"stt\":\"" + category.Status + "\"}");
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> PutCar([FromRoute] int id, [FromBody] Car car)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != car.ID)
            {
                return(BadRequest());
            }

            _context.Entry(car).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> PutCarItem(long id, CarItem carItem)
        {
            if (id != carItem.Id)
            {
                return(BadRequest("The car with this ID does not exist!"));
            }
            if (carItem.Year < 2000)
            {
                return(BadRequest("Year must be greater than 2000"));
            }
            else if (carItem.Price < 100)
            {
                return(BadRequest(" The price must be greater than 100"));
            }

            _context.Entry(carItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> PutCar(long id, Car car)
        {
            if (id != car.Id)
            {
                return(BadRequest());
            }

            _context.Entry(car).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 11
0
        public ActionResult Edit(int carId, Car editCar)
        {
            Car dbcar = (from str in db.Cars
                         where str.Id == carId
                         select str).First();

            try
            {
                var isNewManufacturer = (from str in db.Manufacturers
                                         where str.Name == editCar.Manufacturer.Name
                                         select str).FirstOrDefault();

                if (isNewManufacturer == null)
                {
                    Manufacturer newManufacturer = new Manufacturer()
                    {
                        Name = editCar.Manufacturer.Name
                    };
                    db.Manufacturers.Add(newManufacturer);

                    db.SaveChanges();
                }
                else
                {
                    dbcar.Manufacturer = isNewManufacturer;
                }


                var isNewVehicleType = (from str in db.VehicleTypes
                                        where str.Name == editCar.VehicleType.Name
                                        select str).FirstOrDefault();

                if (isNewVehicleType == null)
                {
                    VehicleType newVehicleType = new VehicleType()
                    {
                        Name = editCar.VehicleType.Name, Manufacturer = editCar.Manufacturer
                    };
                    db.VehicleTypes.Add(newVehicleType);
                }
                else
                {
                    dbcar.VehicleType = isNewVehicleType;
                }

                dbcar.Cost      = editCar.Cost;
                dbcar.Power     = editCar.Power;
                dbcar.TypeCoupe = dbcar.TypeCoupe;
                dbcar.Year      = editCar.Year;

                db.Entry(dbcar).State = EntityState.Modified;
                db.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }
            catch (Exception)
            {
                return(View());
            }
        }
        public async Task <string> PutUser(int id, User user)
        {
            if (id != user.ID)
            {
                return("0");
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return("0");
                }
                else
                {
                    throw;
                }
            }

            _context.SaveChanges();
            return("{\"id\":" + id + ",\"stt\":\"" + user.Status + "\"}");
        }
Ejemplo n.º 13
0
 Task ICarAccessLayer.DeleteCar(int?carId)
 {
     return(Task.Run(() =>
     {
         try
         {
             Car car = _context.Cars.Find(carId);
             car.IsDeleted = true;
             _context.Entry(car).State = EntityState.Modified;
             _context.SaveChanges();
         }
         catch (Exception e)
         {
             throw new Exception(e.Message);
         }
     }));
 }
Ejemplo n.º 14
0
        public Car EditCar(Car car)
        {
            Car editedCar = db.Cars.FirstOrDefault(x => x.Id == car.Id);

            db.Entry(editedCar).CurrentValues.SetValues(car);
            db.SaveChanges();
            return(car);
        }
Ejemplo n.º 15
0
        public async Task <IActionResult> PutCarModel(CarModel car)
        {
            _context.Entry(car).State = EntityState.Modified;

            await _context.SaveChangesAsync();

            return(NoContent());
        }
Ejemplo n.º 16
0
        public ActionResult DeleteConfirm(int id)
        {
            Car c = new Car {
                Id = id
            };

            db.Entry(c).State = EntityState.Deleted;
            db.SaveChanges();

            //Car car = db.Cars.Find(id);
            //if (car != null)
            //{
            //    db.Cars.Remove(car);
            //    db.SaveChanges();
            //}
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 17
0
 public void Update(Car entity)
 {
     using (CarContext context = new CarContext())
     {
         var updatedEntity = context.Entry(entity);
         updatedEntity.State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Ejemplo n.º 18
0
 public void Add(Car entity)
 {
     using (CarContext context = new CarContext())
     {
         var addedEntity = context.Entry(entity);
         addedEntity.State = EntityState.Added;
         context.SaveChanges();
     }
 }
Ejemplo n.º 19
0
 public void Delete(Car entity)
 {
     using (CarContext context = new CarContext())
     {
         var deletedEntity = context.Entry(entity);
         deletedEntity.State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
Ejemplo n.º 20
0
 public ActionResult Edit([Bind(Include = "ID,Make,Model,Year,Color")] Car car)
 {
     if (ModelState.IsValid)
     {
         db.Entry(car).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(car));
 }
Ejemplo n.º 21
0
 public ActionResult Edit([Bind(Include = "CarID,Make,Model,color,year,price,TCCRating,HwyMPG")] Car car)
 {
     if (ModelState.IsValid)
     {
         db.Entry(car).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(car));
 }
Ejemplo n.º 22
0
 public ActionResult Edit([Bind(Include = "ID,Type")] CarType carType)
 {
     if (ModelState.IsValid)
     {
         db.Entry(carType).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(carType));
 }
Ejemplo n.º 23
0
 public ActionResult Edit([Bind(Include = "ID,Producer,Model,Year,EngineType,EngineCapacity,EnginePower,Occupied,Image")] Car car)
 {
     if (ModelState.IsValid)
     {
         db.Entry(car).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(car));
 }
Ejemplo n.º 24
0
 public ActionResult Edit([Bind(Include = "Id,Name,Phone,Email,Gender")] Info info)
 {
     if (ModelState.IsValid)
     {
         db.Entry(info).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(info));
 }
Ejemplo n.º 25
0
 public ActionResult Edit([Bind(Include = "Id,Name,Model,YearOfProduct")] CarModel carModel)
 {
     if (ModelState.IsValid)
     {
         db.Entry(carModel).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(carModel));
 }
 public ActionResult Edit([Bind(Include = "Id,Name,Decription")] Brand brand)
 {
     if (ModelState.IsValid)
     {
         db.Entry(brand).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(brand));
 }
Ejemplo n.º 27
0
        public Car Update(Car Car)
        {
            WebApiConfig.Logger.info("enter CarRepository->Update with id = " + Car.Id.ToString());

            db.Entry(Car).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
            WebApiConfig.Logger.info("return from CarRepository->Update with id = " + Car.Id.ToString());

            return(Car);
        }
Ejemplo n.º 28
0
 public ActionResult Edit([Bind(Include = "Id,Name,Year,BrandId")] Car car)
 {
     if (ModelState.IsValid)
     {
         db.Entry(car).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.BrandId = new SelectList(db.Brand, "Id", "Name", car.BrandId);
     return(View(car));
 }
 public ActionResult Edit([Bind(Include = "Id,Name,CarMarkaID")] CarModel carModel)
 {
     if (ModelState.IsValid)
     {
         db.Entry(carModel).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.CarMarkaID = new SelectList(db.CarMarkas, "Id", "Name", carModel.CarMarkaID);
     return(View(carModel));
 }
        public ActionResult Edit([Bind(Include = "Id,Name,Decription,FileName,UserId")] Post post)
        {
            if (ModelState.IsValid)
            {
                post.UserId          = User.Identity.GetUserId();
                db.Entry(post).State = EntityState.Modified;

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(post));
        }