public async Task <IActionResult> Edit(int id, [Bind("ParkingSpotID,SpotName,OwnerID,Address,Price,NunOfOrders,filePath,SpotDescription")] ParkingSpot parkingSpot, IFormFile file) { if (id != parkingSpot.ParkingSpotID) { return(NotFound()); } if (ModelState.IsValid) { try { // if the file did changed than .. if (file != null) { // creating the entire path to the project using the hostingENV string UploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "SpotImages"); // generating unique filename per file var FileName = Guid.NewGuid().ToString() + "_" + file.FileName; // so the whole path to the file also will be unique using the combine method string filePath = Path.Combine(UploadFolder, FileName); // handeling the saving of a picture using fileStream format using (var fileStream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fileStream); } parkingSpot.filePath = FileName; } else { // without doing those linq calls we saw that the edit causing disapearnce of data! parkingSpot.filePath = (from spotID in _context.ParkingSpot where spotID.ParkingSpotID.ToString() == parkingSpot.ParkingSpotID.ToString() select spotID.filePath).FirstOrDefault(); parkingSpot.OwnerID = (from owner in _context.ParkingSpot where owner.ParkingSpotID.ToString() == parkingSpot.ParkingSpotID.ToString() select owner.OwnerID).FirstOrDefault(); } _context.Update(parkingSpot); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ParkingSpotExists(parkingSpot.ParkingSpotID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(parkingSpot)); }
public async Task <IActionResult> Edit(Guid id, [Bind("ReserveSpotID,UserID,SpotID,CreatedOn,ReservationDate,ReservationHour,Duration,carNumber")] ReserveSpot reserveSpot) { if (id != reserveSpot.ReserveSpotID) { return(NotFound()); } if (ModelState.IsValid) { 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))); } try { var userid = (from users in _identitycontext.GeneralUser where users.UserName == User.Identity.Name select users.UID).FirstOrDefault(); var spotid = (from myid in _context.ReserveSpot where myid.ReserveSpotID == id select myid.SpotID).FirstOrDefault(); reserveSpot.UserID = userid; reserveSpot.Spot = _context.ParkingSpot.FirstOrDefault(u => u.ParkingSpotID == spotid); reserveSpot.SpotID = reserveSpot.Spot.ParkingSpotID; reserveSpot.CreatedOn = System.DateTime.Now; var res = from reserve in _context.ReserveSpot where reserve.ReserveSpotID != id select reserve; var spots = from sp in res where sp.Spot.ParkingSpotID == reserveSpot.SpotID && sp.ReservationDate == reserveSpot.ReservationDate select sp; //validation that the reservation made for the desired date if (reserveSpot.ReservationHour + reserveSpot.Duration > 24) { return(RedirectToAction(nameof(SorryChooseDurationForTheDayYouSelected))); } foreach (var r in spots) { //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))); } } _context.Update(reserveSpot); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ReserveSpotExists(reserveSpot.ReserveSpotID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } else { return(RedirectToAction(nameof(InvalidDate))); } } ViewData["SpotID"] = new SelectList(_context.ReserveSpot, "SpotID", "SpotID", reserveSpot.SpotID); ViewData["UserID"] = new SelectList(_context.ReserveSpot, "UserID", "UserID", reserveSpot.UserID); return(View(reserveSpot)); }