public IActionResult AddVehicle(Vehicle newVehicle) { var availableSpots = _context.spots.Where(s => !s.isOccupied).Select(s => s).ToList().OrderBy(s => s.size); if (availableSpots != null && availableSpots.Count() > 0) { Spot selected; if (availableSpots.FirstOrDefault().size == newVehicle.type) { selected = availableSpots.FirstOrDefault(); } else if (availableSpots.LastOrDefault().size == newVehicle.type) { selected = availableSpots.LastOrDefault(); } else { selected = availableSpots.Where(s => s.size == newVehicle.type || s.size > newVehicle.type).Select(s => s).FirstOrDefault(); } selected.isOccupied = true; _context.spots.Update(selected); newVehicle.spotId = selected.id; _context.Vehicles.Add(newVehicle); var lot = _context.lot.FirstOrDefault(); lot.availableSpots -= 1; _context.lot.Update(lot); _context.SaveChanges(); } return(View(newVehicle)); }
public ParkingLotController(ParkingLotContext context) { _context = context; if (_context.lot == null || _context.lot.Count() == 0) { List <Spot> spots = new List <Spot>(); for (int i = 1; i <= 3; i++) { spots.Add(new Spot { isOccupied = false, size = i }); } _context.spots.AddRange(spots); _context.lot.Add(new Lot { availableSpots = spots.Count, name = "Poonam's Lot", address = "Downtown" }); _context.SaveChanges(); } }
public void CommitChanges() { _context.SaveChanges(); }