コード例 #1
0
        public ActionResult AddVehicle(GarageAddNewVehicleViewModel model) {
            if (!ModelState.IsValid) {
                ModelState.AddModelError("", "Model validation Error");
                return View(model);
            }
            Vehicle newVehicle = new Vehicle {
                LicensePlate = model.LicensePlate,
                Transmission = model.Transmission,
                Branch = new Branch {BranchName = model.BranchSelected},
                Color = model.Color,
                DistanceTraveled = model.DistanceTraveled,
                Model = model.VehicleModel,
                ManufacturYear = model.ManufacturYear,
                Manufacturer = model.Manufacturer,
                Price = model.Price,
                Status = "Available"
            };

            string serverPath = Server.MapPath("~/Content/GarageImages");
            VehicleManagerResult vehicleManagerResult = VehicleManager.AddNewVehicle(newVehicle, model.ImageFile, serverPath);
            if (!vehicleManagerResult.Success) {
                ModelState.AddModelError("", vehicleManagerResult.ErrorMessage);
                BranchManagerResult getBranchesResult = BranchManager.GetBranchesNames();
                if (getBranchesResult.Success) {
                    model.BracnhList = getBranchesResult.BranchesNames.Select(x => new SelectListItem {Text = x, Value = x}).ToList();
                    return View(model);
                }
                TempData["AddVehicle"] = getBranchesResult.ErrorMessage;
                return RedirectToAction("Index", "Garage");
            }
            TempData["AddVehicle"] = "Vehicle Added";
            return RedirectToAction("Index", "Garage");
        }
コード例 #2
0
ファイル: VehicleManager.cs プロジェクト: alentor/alen-motors
        /// <summary>
        /// Add a new Vehicle
        /// </summary>
        /// <param name="newVehicle"> New Vehicle class</param>
        /// <param name="imageFile"> Vehicle Image</param>
        /// <param name="serverPath"> Serer path used used to save the image</param>
        /// <returns>Retuns true if the addition has Succeeded, else returns string with an error message</returns>
        public static VehicleManagerResult AddNewVehicle(Vehicle newVehicle, HttpPostedFileBase imageFile, string serverPath) {
            VehicleManagerResult vehicleManagerResult = new VehicleManagerResult();
            try {
                using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) {
                    string[] allowedExtensions = new[] {".Jpg", ".png", ".jpg", ".jpeg", ".gif"};
                    string fileExt = Path.GetExtension(imageFile.FileName); // Get file Extension
                    // Check if file in the right extension
                    if (!allowedExtensions.Contains(fileExt)) {
                        vehicleManagerResult.ErrorMessage = "Wrong file Format";
                        return vehicleManagerResult;
                    }
                    foreach (Branch branch in alenMotorsDbEntities.Branches.ToList()) {
                        if (branch.BranchName.Replace(" ", String.Empty) == newVehicle.Branch.BranchName) {
                            newVehicle.Branch = branch;
                        }
                    }

                    string fileName = Path.GetFileName(imageFile.FileName); // Get only file name
                    string path = System.IO.Path.Combine(serverPath, fileName); // Path to where the image will be saved to
                    newVehicle.ImageUrl = fileName;
                    if (alenMotorsDbEntities.Vehicles.ToList().Any(vehicle => vehicle.LicensePlate == newVehicle.LicensePlate)) {
                        vehicleManagerResult.ErrorMessage = "This license plate already exists";
                        return vehicleManagerResult;
                    }

                    Image newImage = Image.FromStream(imageFile.InputStream, true, true);
                    Image resizedImage = Generic.ResizeImage(newImage, 400, 250);
                    resizedImage.Save(path);
                    alenMotorsDbEntities.Vehicles.Add(newVehicle);
                    alenMotorsDbEntities.SaveChanges();
                    vehicleManagerResult.Success = true;
                    return vehicleManagerResult;
                }
            }
            catch (Exception ex) {
                vehicleManagerResult.ErrorMessage = ex.Message;
                return vehicleManagerResult;
            }
        }
コード例 #3
0
ファイル: VehicleManager.cs プロジェクト: alentor/alen-motors
 /// <summary>
 /// Edits a vehicle based on the passed information
 /// </summary>
 /// <param name="vehicleToEdit"></param>
 /// <param name="imageFile"></param>
 /// <param name="serverPath"></param>
 /// <returns>Returns true if the operation was successful else will return a stirng with the error message</returns>
 public static VehicleManagerResult EditVehicle(Vehicle vehicleToEdit, HttpPostedFileBase imageFile, string serverPath) {
     VehicleManagerResult vehicleManagerResult = new VehicleManagerResult();
     try {
         string path = null;
         string oldPath = null;
         // with Image update
         if (imageFile != null) {
             string[] allowedExtensions = new[] {".Jpg", ".png", ".jpg", ".jpeg", ".gif"};
             string fileExt = Path.GetExtension(imageFile.FileName); // Get file Extension
             // Check if file in the right extension
             if (!allowedExtensions.Contains(fileExt)) {
                 vehicleManagerResult.ErrorMessage = "Wrong file Format";
                 return vehicleManagerResult;
             }
             path = Path.Combine(serverPath, imageFile.FileName); // Path to where the image will be saved to
         }
         // without Image update
         using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) {
             foreach (Vehicle vehicle in alenMotorsDbEntities.Vehicles.ToList()) {
                 if (vehicle.VehicleID == vehicleToEdit.VehicleID) {
                     vehicle.DistanceTraveled = vehicleToEdit.DistanceTraveled;
                     vehicle.Color = vehicleToEdit.Color;
                     vehicle.ManufacturYear = vehicleToEdit.ManufacturYear;
                     vehicle.Manufacturer = vehicleToEdit.Manufacturer;
                     vehicle.LicensePlate = vehicleToEdit.LicensePlate;
                     vehicle.Price = vehicleToEdit.Price;
                     if (vehicleToEdit.Transmission != null) {
                         vehicle.Transmission = vehicleToEdit.Transmission;
                     }
                     if (vehicleToEdit.Status != null) {
                         vehicle.Status = vehicleToEdit.Status;
                     }
                     if (vehicleToEdit.Branch.BranchName != null) {
                         foreach (Branch branch in alenMotorsDbEntities.Branches.ToList()) {
                             if (branch.BranchName.Replace(" ", String.Empty) == vehicleToEdit.Branch.BranchName) {
                                 vehicle.Branch = branch;
                                 vehicle.BranchID = branch.BranchID;
                             }
                         }
                     }
                     if (path != null) {
                         Image newImage = Image.FromStream(imageFile.InputStream, true, true);
                         Image resizedImage = Generic.ResizeImage(newImage, 400, 250);
                         resizedImage.Save(path);
                         //newImage.Save(path);
                         //Generic.FormatPicture(imageFile, path, 400,400);
                         //imageFile.SaveAs(path);
                         oldPath = Path.Combine(serverPath, vehicle.ImageUrl);
                         if (File.Exists(oldPath)) {
                             File.Delete(oldPath);
                             vehicle.ImageUrl = imageFile.FileName;
                         }
                     }
                     alenMotorsDbEntities.SaveChanges();
                     vehicleManagerResult.Success = true;
                     return vehicleManagerResult;
                 }
             }
         }
     }
     catch (Exception ex) {
         vehicleManagerResult.ErrorMessage = ex.Message;
         return vehicleManagerResult;
     }
     return null;
 }
コード例 #4
0
 public ActionResult EditVehicle(GarageEditVehicleViewModel model) {
     if (!ModelState.IsValid) {
         ModelState.AddModelError("", "Model validation Error");
         return RedirectToAction("Index", "Garage");
     }
     Vehicle vehicleToEdit = new Vehicle {
         VehicleID = model.VehicleId,
         Branch = new Branch {BranchName = model.BrancSelected},
         Color = model.Color,
         DistanceTraveled = model.DistanceTraveled,
         ManufacturYear = model.ManufacturYear,
         Manufacturer = model.Manufacturer,
         Model = model.VehicleModel,
         LicensePlate = model.LicensePlate,
         Transmission = model.Transmission,
         Status = model.Status,
         Price = model.Price
     };
     string serverPath = Server.MapPath("~/Content/GarageImages");
     VehicleManagerResult vehicleManagerResult = VehicleManager.EditVehicle(vehicleToEdit, model.ImageFile, serverPath);
     if (vehicleManagerResult.Success) {
         TempData["EditVehicle"] = "Vehicle updated uccessfully ";
         return RedirectToAction("Index", "Garage");
     }
     TempData["EditVehicle"] = vehicleManagerResult.ErrorMessage;
     return RedirectToAction("Index", "Garage", new {vehicleID = model.VehicleId.ToString()});
 }