Exemplo n.º 1
0
        public ActionResult Edit(int id, string registration, int vehicletypeid, int vehiclecolorid, string brand, string model, int numberofwheels)
        {
            var vehicle = db.Vehicles.Find(id);

            if (vehicle == null)
            {
                return(HttpNotFound());
            }
            vehicle.VehicleColorId = vehiclecolorid;
            vehicle.Brand          = brand;
            vehicle.Model          = model;
            vehicle.NumberOfWheels = numberofwheels;
            if (ModelState.IsValid)
            {
                if (vehicle.Registration != registration)
                {
                    vehicle.Registration = RegistrationNormalizer.NormalizeForStorage(registration);
                    if (!registrationVerifier.Verify(vehicle.Registration))
                    {
                        ModelState.AddModelError(nameof(vehicle.Registration), registrationVerifier.LastErrorMessage);
                        MakeCreateDropDowns(vehicle, true);
                        return(View(vehicle));
                    }
                    if (db.Vehicles.Where(v => v.Id != vehicle.Id).Any(v => v.Registration == vehicle.Registration))
                    {
                        ModelState.AddModelError(nameof(vehicle.Registration), $"A vehicle with the registration '{RegistrationNormalizer.NormalizeForDisplay(vehicle.Registration)}' already exist in the garage");
                        MakeCreateDropDowns(vehicle, true);
                        return(View(vehicle));
                    }
                }
                if (vehicle.VehicleTypeId != vehicletypeid)
                {
                    vehicle.VehicleTypeId = vehicletypeid;                                                            // Update the field
                    vehicle.ParkingUnit   = -1;                                                                       // Go negative so it don't count anymore as we're moving it
                    db.SaveChanges();
                    vehicle.ParkingUnit = FindFirstFreeUnit(db.VehicleTypes.Single(t => t.Id == vehicletypeid).Size); // find the new parking spot this vehicle should be in
                }
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            MakeCreateDropDowns(vehicle, true);
            return(View(vehicle));
        }
Exemplo n.º 2
0
 public ActionResult Checkin([Bind(Include = "Id,Registration,VehicleTypeId, VehicleColorId, Brand, Model, NumberOfWheels")] Vehicle vehicle, string username, string password)
 {
     if (string.IsNullOrWhiteSpace(username))
     {
         ModelState.AddModelError("username", "The Username field is required.");
         MakeCreateDropDowns(vehicle);
         return(View(vehicle));
     }
     ViewBag.UserName = username;
     if (ModelState.IsValid)
     {
         if (!registrationVerifier.Verify(vehicle.Registration))
         {
             ModelState.AddModelError(nameof(vehicle.Registration), registrationVerifier.LastErrorMessage);
             MakeCreateDropDowns(vehicle);
             return(View(vehicle));
         }
         vehicle.Registration = RegistrationNormalizer.NormalizeForStorage(vehicle.Registration);
         if (db.Vehicles.Any(v => v.Registration == vehicle.Registration))
         {
             ModelState.AddModelError(nameof(vehicle.Registration), $"A vehicle with the registration '{RegistrationNormalizer.NormalizeForDisplay(vehicle.Registration)}' already exist in the garage");
             MakeCreateDropDowns(vehicle);
             return(View(vehicle));
         }
         var user = db.Members.FirstOrDefault(m => m.Username == username);
         if (user == null)
         {
             ModelState.AddModelError("username", $"No member with the username '{username}' could be found!");
             MakeCreateDropDowns(vehicle);
             return(View(vehicle));
         }
         if (user.Password != password)
         {
             ModelState.AddModelError("password", "Wrong password!");
             MakeCreateDropDowns(vehicle);
             return(View(vehicle));
         }
         vehicle.MemberId    = user.Id;
         vehicle.CheckinTime = DateTime.Now;
         vehicle.Type        = db.VehicleTypes.FirstOrDefault(t => t.Id == vehicle.VehicleTypeId);
         vehicle.ParkingUnit = FindFirstFreeUnit(vehicle.Units);
         if (vehicle.ParkingUnit + vehicle.Units > db.GarageConfiguration.ParkingUnits)
         {
             ModelState.AddModelError(nameof(vehicle.VehicleTypeId), $"Not enough space to park a {vehicle.Type.Type}");
             MakeCreateDropDowns(null);
             return(View(vehicle));
         }
         db.Vehicles.Add(vehicle);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     MakeCreateDropDowns(vehicle);
     return(View(vehicle));
 }