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)); }
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)); }
private ActionResult GetPagedVehiclesList(string orderBy, string currentFilter, string searchString, int?selectedvehicletype, int page, string view) { if (!db.IsConfigured) { return(RedirectToAction("Index", "Setup")); } if (!db.Vehicles.Any()) { return(RedirectToAction("Checkin")); } var vehicles = db.Vehicles.Include(v => v.Owner).Include(v => v.Type); if (selectedvehicletype != null) { vehicles = vehicles.Where(v => v.Type.Id == selectedvehicletype); ViewBag.selectedvehicletype = selectedvehicletype; } if (searchString != null) { page = 1; // If the search string is changed during paging, the page has to be reset to 1 } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; if (!string.IsNullOrEmpty(searchString)) { searchString = RegistrationNormalizer.NormalizeForStorage(searchString); vehicles = vehicles.Where(v => v.Registration.Contains(searchString)); } switch (orderBy) { case "user": vehicles = vehicles.OrderBy(v => v.Owner.Name); break; case "user_dec": vehicles = vehicles.OrderByDescending(v => v.Owner.Name); break; case "type": vehicles = vehicles.OrderBy(v => v.Type.Type); break; case "type_dec": vehicles = vehicles.OrderByDescending(v => v.Type.Type); break; case "registration": vehicles = vehicles.OrderBy(v => v.Registration); break; case "registration_dec": vehicles = vehicles.OrderByDescending(v => v.Registration); break; case "spot": vehicles = vehicles.OrderBy(v => v.ParkingUnit); break; case "spot_dec": vehicles = vehicles.OrderByDescending(v => v.ParkingUnit); break; case "brand": vehicles = vehicles.OrderBy(v => v.Brand); break; case "brand_dec": vehicles = vehicles.OrderByDescending(v => v.Brand); break; case "model": vehicles = vehicles.OrderBy(v => v.Model); break; case "model_dec": vehicles = vehicles.OrderByDescending(v => v.Model); break; case "wheels": vehicles = vehicles.OrderBy(v => v.NumberOfWheels); break; case "wheels_dec": vehicles = vehicles.OrderByDescending(v => v.NumberOfWheels); break; case "color": vehicles = vehicles.OrderBy(v => v.VehicleColorId); break; case "color_dec": vehicles = vehicles.OrderByDescending(v => v.VehicleColorId); break; case "time": vehicles = vehicles.OrderBy(v => v.CheckinTime); break; default: vehicles = vehicles.OrderByDescending(v => v.CheckinTime); break; } ViewBag.CurrentSort = orderBy; ViewBag.VehicleTypes = new SelectList(db.VehicleTypes, "Id", "Type"); HasVacantSpots(); return(View(view, new PagedList.PagedList <Vehicle>(vehicles.ToList(), page, db.GarageConfiguration.VehiclesPerPage))); }