public async Task <IActionResult> Create([Bind("ParkingSpotID,SpotName,OwnerID,Address,Price,NunOfOrders,filePath,SpotDescription,parkingPhoto")] ParkingSpotCreate parkingSpot) { // get the current user personal id var currentUser = (from userID in _identity.GeneralUser where userID.Id == User.FindFirstValue(ClaimTypes.NameIdentifier) select userID.UID).FirstOrDefault(); // with that I am generating the spotID var numOfSpots = 0; if (_context.ParkingSpot.Count() != 0) { numOfSpots = _context.ParkingSpot.Last().ParkingSpotID; } else { numOfSpots = _context.ParkingSpot.Count(); } if (ModelState.IsValid) { string FileName = null; if (parkingSpot.parkingPhoto != null) { string UploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "SpotImages"); FileName = Guid.NewGuid().ToString() + "_" + parkingSpot.parkingPhoto.FileName; string filePath = Path.Combine(UploadFolder, FileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await parkingSpot.parkingPhoto.CopyToAsync(fileStream); } } ParkingSpot newSpot = new ParkingSpot { ParkingSpotID = numOfSpots + 1, SpotName = parkingSpot.SpotName, OwnerID = currentUser, Address = parkingSpot.Address, Price = parkingSpot.Price, NunOfOrders = 0, SpotDescription = parkingSpot.SpotDescription, filePath = FileName }; _context.Add(newSpot); await _context.SaveChangesAsync(); return(RedirectToAction("Details", new { id = newSpot.ParkingSpotID })); } return(View(parkingSpot)); }
public async Task <IActionResult> Create([Bind("ReserveSpotID,UserID,SpotID,CreatedOn,ReservationDate,ReservationHour,Duration,carNumber")] ReserveSpot reserveSpot, [FromRoute] int parkingSpotID) { if (ModelState.IsValid) { //checks if the reservation date didn't pass or if the the reservation date is today if ((reserveSpot.ReservationDate.Date >= DateTime.Today)) { //checks if the reservation hour is valid for today's date if ((reserveSpot.ReservationHour <= System.DateTime.Now.Hour) && ((reserveSpot.ReservationDate.Date == DateTime.Today))) { return(RedirectToAction(nameof(InvalidHour))); } //getting the id of current user var currentUser = (from userID in _identitycontext.GeneralUser where userID.Id == User.FindFirstValue(ClaimTypes.NameIdentifier) select userID.UID).FirstOrDefault(); reserveSpot.ReserveSpotID = Guid.NewGuid(); reserveSpot.CreatedOn = System.DateTime.Now; reserveSpot.Spot = _context.ParkingSpot.FirstOrDefault(u => u.ParkingSpotID == parkingSpotID); reserveSpot.UserID = currentUser; //get all the reservations for the wanted day for a specific spot spot var myreservations = (from reservations in _context.ReserveSpot where reservations.Spot.ParkingSpotID == parkingSpotID && reservations.ReservationDate == reserveSpot.ReservationDate //replace select reservations); //validation that the reservation made for the desired date if (reserveSpot.ReservationHour + reserveSpot.Duration > 24) { return(RedirectToAction(nameof(SorryChooseDurationForTheDayYouSelected))); } foreach (var r in myreservations) { //case1: check if the desired res hour is between an already reserved time range if ((reserveSpot.ReservationHour < r.ReservationHour + r.Duration && reserveSpot.ReservationHour >= r.ReservationHour)) { return(RedirectToAction(nameof(SorrySpotIsTaken))); } //case2: the desired res hour is an availble hour but the duration slip into other reservation and its end time is before the already existing reservation fullfiled if ((reserveSpot.ReservationHour + reserveSpot.Duration <= r.ReservationHour + r.Duration) && (reserveSpot.ReservationHour + reserveSpot.Duration > r.ReservationHour)) { return(RedirectToAction(nameof(SorrySpotIsTaken))); } //case3: the desired res hour is an availble hour but the duration slip into other reservation if ((reserveSpot.ReservationHour <= r.ReservationHour) && (reserveSpot.ReservationHour + reserveSpot.Duration >= r.ReservationHour + r.Duration)) { return(RedirectToAction(nameof(SorrySpotIsTaken))); } } reserveSpot.Spot.NunOfOrders++; _context.Add(reserveSpot); await _context.SaveChangesAsync(); // finding current user by name var uName = User.Identity.Name; var thisUser = await _identitycontext.GeneralUser.FirstOrDefaultAsync(u => u.UserName.Equals(uName)); // validation thisUser really exists if (thisUser == null) { return(NotFound()); } // this section is to save the history of spots reserved to serve the ML algo string uHistory = thisUser.History; if (!string.IsNullOrEmpty(uHistory)) { uHistory += ","; } uHistory += reserveSpot.SpotID.ToString(); thisUser.History = uHistory; try { _identitycontext.Update(thisUser); await _identitycontext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw new Exception("error"); } return(RedirectToAction(nameof(Index))); } else { return(RedirectToAction(nameof(InvalidDate))); } } return(View(reserveSpot)); }