public async Task <IActionResult> GetReservations() { var user = await userManager.GetUserAsync(User); var reservations = context.Reservations .Where(r => r.UserId == user.Id) .Include(r => r.Screening) .ThenInclude(sc => sc.Movie) .Include(r => r.Screening) .ThenInclude(sc => sc.ScreeningRoom) .ToArray(); var badReservations = reservations.Where(r => r.Expiration.HasValue || !r.IsConfirmed); context.RemoveRange(badReservations); await context.SaveChangesAsync(); return(new OkObjectResult(reservations.Where(r => !r.Expiration.HasValue && r.IsConfirmed))); }
public async Task <IActionResult> CheckReservation([FromBody] int reservationId) { var myReservation = context.Reservations.FirstOrDefault(r => r.ReservationId == reservationId); if (myReservation == null) { return(new OkObjectResult("RESERVATION_BOOKED")); } if (myReservation.Expiration.HasValue) { return(BadRequest()); // nie warto sprawdzać potwierdzonej rezerwacji } var otherReservations = context.Reservations .Where(r => r.ScreeningId == myReservation.ScreeningId && r.SeatX == myReservation.SeatX && r.SeatY == myReservation.SeatY); var confirmed = await otherReservations.Where(r => r.IsConfirmed).FirstOrDefaultAsync(); // no niestety if (confirmed != null) { return(new OkObjectResult("RESERVATION_BOOKED")); } var leader = await otherReservations.Where(r => r.Expiration.HasValue).FirstOrDefaultAsync(); if (leader == null) // leader sobie poszedł { var min = otherReservations.Select(r => r.Priority).Min(); // wybieramy z najniższą wartością if (myReservation.Priority == min) // to my! { myReservation.Expiration = DateTime.Now.AddMinutes(1); context.Update(myReservation); await context.SaveChangesAsync(); return(new OkObjectResult(myReservation)); } else { return(new OkObjectResult("KEEP_WAITING")); // nie my, ale warto dalej próbować } } else // leader istnieje { if (leader.Expiration.Value > DateTime.Now) // i dalej ma prawo wyboru { return(new OkObjectResult("KEEP_WAITING")); } else { context.Remove(leader); // leader is dead! await context.SaveChangesAsync(); var min = otherReservations.Select(r => r.Priority).Min(); // wybieramy z najniższ wartości if (myReservation.Priority == min) { myReservation.Expiration = DateTime.Now.AddMinutes(1); context.Update(myReservation); await context.SaveChangesAsync(); return(new OkObjectResult(myReservation)); } else { if (leader.IsConfirmed)// jest rezerwacja zaklepana { var res = otherReservations.Where(r => !r.IsConfirmed); context.RemoveRange(res); await context.SaveChangesAsync(); return(new OkObjectResult("RESERVATION_BOOKED")); } else { return(new OkObjectResult("KEEP_WAITING")); // nie my, ale warto dalej próbować } } } } }