public IActionResult Edit(int id, [Bind("Id,Number,Description,Price,Size,Available,StudioId")] RehearsalRoom rehearsalRoom) { if (id != rehearsalRoom.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _reservationService.UpdateRoom(rehearsalRoom); } catch (DbUpdateConcurrencyException) { if (!RehearsalRoomExists(rehearsalRoom.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["StudioId"] = new SelectList(_reservationService.Studios, "Id", "Address", rehearsalRoom.StudioId); return(View(rehearsalRoom)); }
public bool AddRoom(RehearsalRoom room) { _context.Rooms.Add(room); try { _context.SaveChanges(); } catch { return(false); } return(true); }
public bool UpdateRoom(RehearsalRoom room) { _context.Rooms.Update(room); try { _context.SaveChanges(); } catch (Exception) { return(false); } return(true); }
//public IActionResult Create([Bind("Id,Number,Description,Price,Size,Available,StudioId")] RehearsalRoom rehearsalRoom) public IActionResult Create(RehearsalRoom @rehearsalRoom) { rehearsalRoom.Studio = _reservationService.GetStudio(rehearsalRoom.StudioId); if (ModelState.IsValid) { if (_reservationService.AddRoom(@rehearsalRoom) && _reservationService.NewRoomAdded(rehearsalRoom.StudioId)) { TempData["SuccessAlert"] = "Terem sikeresen létrehozva!"; return(RedirectToAction(nameof(Index))); } } IEnumerable <ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors); ViewData["StudioId"] = new SelectList(_reservationService.Studios, "Id", "Address", rehearsalRoom.StudioId); TempData["DangerAlert"] = "Terem létrehozása sikertelen!"; return(View(rehearsalRoom)); }
/// <summary> /// Creates a new reservation and adds the room by the given ID including the studio /// and sets the start date to NOW and the end date to NOW+2hours. /// </summary> /// <param name="roomId">The ID of the room on which you want to make a reservation.</param> /// <returns>The new reservation.</returns> public ReservationViewModel NewReservation(int?roomId) { if (roomId == null) { return(null); } RehearsalRoom room = Rooms.FirstOrDefault(l => l.Id == roomId); ReservationViewModel reservation = new ReservationViewModel { Room = room }; reservation.Day = DateTime.Today; reservation.StartHour = DateTime.Now.Hour + 1; reservation.EndHour = DateTime.Now.Hour + 3; return(reservation); }
public ReservationDateError Validate(DateTime start, DateTime end, int roomId, string action, int reservationId, Dictionary <string, bool> equipments) { if (end < start) { return(ReservationDateError.EndInvalid); } if (end == start) { return(ReservationDateError.LengthInvalid); } RehearsalRoom currentRoom = _context.Rooms.Include(l => l.Reservations).Include(l => l.Studio).Include(l => l.Studio.Equipments).FirstOrDefault(l => l.Id == roomId); RehearsalStudio currentStudio = currentRoom.Studio; List <int> oldConflictingReservations = new List <int>(); foreach (var resEqPair in _context.ReservationEquipmentPairs) //végigmegyünk a már meglévő foglalásokon, amikhez eszközt is béreltek { if (resEqPair.StudioId == currentStudio.Id && equipments.ContainsKey(resEqPair.EquipmentName)) //ha ebbe a stúdióba szól a foglalás, és olyan eszközt béreltek hozzá, amit mi is akarunk { Reservation oldReservation = _context.Reservations.FirstOrDefault(l => l.Id == resEqPair.ReservationId); //akkor megkeressük ezt a foglalást if (oldReservation.IsConflicting(start, end)) //ha ez a foglalás konfliktál a mienkkel, akkor hozzáadjuk { oldConflictingReservations.Add(oldReservation.Id); //ebbe gyűjtjük azokat, amiknek meg kell nézni egyenként hogy milyen eszközt béreltek hozzá } } } int conflictingReservationsWithSameEquipments = 0; foreach (string eqName in equipments.Keys) //végigmegyünk azokon az eszközökön, amiket most bérelni szeretnénk { /* * minden eszköznévre végig kell számolni, hogy hányszor lett kibérelve. * ha ez annyi, mint amennyi a stúdióban rendelkezésre áll, akkor hibát kell dobni * */ conflictingReservationsWithSameEquipments = _context.ReservationEquipmentPairs .Where(l => oldConflictingReservations.Contains(l.ReservationId) && l.EquipmentName == eqName).Count(); int availableInStudio = _context.Studios .FirstOrDefault(l => l.Id == currentStudio.Id) .Equipments.FirstOrDefault(l => l.Name == eqName) .QuantityAvailable; if (conflictingReservationsWithSameEquipments >= availableInStudio) { return(ReservationDateError.EquipmentNotAvailable); } conflictingReservationsWithSameEquipments = 0; } /* * itt kell csekkolni az adatbázist az equipment reservations pair táblában */ //if (selectedRoom == null) // return ReservationDateError.None; //menjünk végig az adott stúdió összes termének összes foglalásán, válasszuk ki az aktuálissal konfliktálókat //majd ezekből gyűjtsük össze, hogy melyik eszközből hányat béreltek ki az adott időpontban //ha annyiszor van már kibérelve, ahány összesen elérhető a próbahelyen, akkor hibát kell dobni a foglalásra //RehearsalStudio studio = _reservationService.GetStudioByRoomId(roomId); /* * List<Reservation> reservations = new List<Reservation>(); * * foreach (var room in currentStudio.Rooms) * { * foreach (var reservation in _context.Reservations.Include(l => l.Equipments).Where(l => l.RehearsalRoomId == room.Id)) * { * if (reservation.IsConflicting(start, end)) //ebben benne lesz az aktuális foglalás is * { * reservations.Add(reservation); * } * } * } * * Dictionary<string, int> reservedEquipmentsInCurrentTime = new Dictionary<string, int>(); * * foreach (var reservation in reservations) * { * foreach (var equipment in reservation.Equipments) * { * if (!reservedEquipmentsInCurrentTime.ContainsKey(equipment.Name)) * { * reservedEquipmentsInCurrentTime[equipment.Name] = 1; * } * else * { * reservedEquipmentsInCurrentTime[equipment.Name] += 1; * } * } * } * * foreach (var equipment in reservedEquipmentsInCurrentTime) //végigmegyek az adott időpontban már lefoglalt eszközökön * { * //és megnézem hogy az aktuális foglalásban van-e olyan, amit más máskorra is kibéreltek * if (equipments.ContainsKey(equipment.Key) && equipments[equipment.Key] == true && equipment.Value > currentStudio.Equipments.FirstOrDefault(l => l.Name == equipment.Key).QuantityAvailable) //itt az kell hogy equipment.Value > összeselérhető, mert úgy gyűjtöttem a konfliktálókat, hogy az aktuális is benne van * { * //és ha valamit már máskorra is kibéreltek annyiszor, ahány az eszközből rendelkezésre áll, akkor hibát dobunk * return ReservationDateError.EquipmentNotAvailable; * } * } */ switch (action) { case "create": if (_context.Reservations.Where(l => l.RehearsalRoomId == currentRoom.Id && l.End >= start) .ToList() .Any(l => l.IsConflicting(start, end))) { return(ReservationDateError.Conflicting); } break; case "edit": if (_context.Reservations.Where(l => l.Id != reservationId && l.RehearsalRoomId == currentRoom.Id && l.End >= start) .ToList() .Any(l => l.IsConflicting(start, end))) { return(ReservationDateError.Conflicting); } //itt azért nem kell nézni a reservationId-t, mert önmagával konfliktálhat az a foglalás, //amit éppen módosítani akarok. //pl. van 14-16-ig egy foglalásom és 14-15-re szeretném módosítani, vagy csak a bérelt eszközöket akarom módosítani break; } return(ReservationDateError.None); }
public async Task <IActionResult> Edit(int reservationId, ReservationViewModel reservationViewModel) { Reservation reservation = _reservationService.Reservations.FirstOrDefault(l => l.Id == reservationId); reservation.Start = new DateTime( reservationViewModel.Day.Year, reservationViewModel.Day.Month, reservationViewModel.Day.Day, reservationViewModel.StartHour, 0, 0 ); reservation.End = new DateTime( reservationViewModel.Day.Year, reservationViewModel.Day.Month, reservationViewModel.Day.Day, reservationViewModel.EndHour, 0, 0 ); string userId = _userManager.GetUserId(User); User user = await _userManager.FindByIdAsync(userId); reservation.User = user; reservation.BandName = reservationViewModel.BandName; DateTime day = reservationViewModel.Day; DateTime startDate = new DateTime(day.Year, day.Month, day.Day, reservation.Start.Hour, 0, 0); DateTime endDate = new DateTime(day.Year, day.Month, day.Day, reservation.End.Hour, 0, 0); switch (_reservationService.ValidateReservation(startDate, endDate, "edit", reservation.Id, reservationViewModel.Equipments, reservation.RehearsalRoomId)) { case ReservationDateError.StartInvalid: ModelState.AddModelError("StartHour", "A kezdés dátuma nem megfelelő!"); break; case ReservationDateError.EndInvalid: ModelState.AddModelError("EndHour", "A megadott foglalási idő érvénytelen (a foglalás vége korábban van, mint a kezdete)!"); break; case ReservationDateError.Conflicting: ModelState.AddModelError("StartHour", "A megadott időpontban a terembe már van foglalás!"); break; case ReservationDateError.LengthInvalid: ModelState.AddModelError("EndHour", "Üres az időintervallum!"); break; case ReservationDateError.EquipmentNotAvailable: ModelState.AddModelError("Endhour", "A bérelni kívánt eszközből a megadott időpontban már az összes foglalt!"); break; } RehearsalRoom room = _reservationService.GetRoom(reservation.RehearsalRoomId); RehearsalStudio studio = _reservationService.Studios.FirstOrDefault(l => l.Rooms.Contains(room)); /* * foreach (var e in reservationViewModel.Equipments) //ebben az összes stúdióban bérelhető eszköz benne van * { * Equipment eq = studio.Equipments.FirstOrDefault(l => l.Name == e.Key); * //ha már benne van a foglalásban de lemondtuk: * if (!e.Value && reservation.Equipments.Contains(eq)) * { * reservation.Equipments.Remove(eq); * } * else if (e.Value && !reservation.Equipments.Contains(eq)) //ehelyett az egész helyett kellene a pairs táblát módosítani * { * reservation.Equipments.Add(eq); * } * } */ if (ModelState.IsValid) { try { _reservationService.UpdateReservation(reservation); _reservationService.UpdateReservationEquipmentTable(reservationId, studio.Id, reservationViewModel.Equipments); } catch (DbUpdateConcurrencyException) { if (!ReservationExists(reservation.Id)) { return(NotFound()); } else { throw; } } TempData["SuccessAlert"] = "Foglalását sikeresen módosította!"; return(RedirectToAction(nameof(Index))); } reservationViewModel.Room = reservation.RehearsalRoom; reservationViewModel.Id = reservation.Id; TempData["DangerAlert"] = "Foglalás módosítása sikertelen!"; List <int> hours = new List <int>(); //itt attól függően legyenek benne a hours-ban az órák hogy melyik napra akar a felhasználó foglalni //some killing dynamic stuff..... for (int i = reservation.RehearsalRoom.Studio.OpeningHourMonday; i <= reservation.RehearsalRoom.Studio.ClosingHourMonday; ++i) { hours.Add(i); } ViewData["Hours"] = new SelectList(hours); return(View(reservationViewModel)); }
// GET: Reservations/Edit/5 public async Task <IActionResult> Edit(int?reservationId) { if (reservationId == null) { return(NotFound()); } var reservation = _reservationService.GetReservation(reservationId); if (reservation == null) { return(NotFound()); } string userId = _userManager.GetUserId(User); User user = await _userManager.FindByIdAsync(userId); reservation.User = user; ViewData["RehearsalRoomId"] = new SelectList(_reservationService.Rooms, "Id", "Number", reservation.RehearsalRoomId); List <int> hours = new List <int>(); //itt attól függően legyenek benne a hours-ban az órák hogy melyik napra akar a felhasználó foglalni //some killing dynamic stuff..... for (int i = reservation.RehearsalRoom.Studio.OpeningHourMonday; i <= reservation.RehearsalRoom.Studio.ClosingHourMonday; ++i) { hours.Add(i); } ViewData["Hours"] = new SelectList(hours); ReservationViewModel viewModel = new ReservationViewModel { UserOwnName = reservation.User.UserOwnName, UserPhoneNumber = reservation.User.PhoneNumber, UserEmail = reservation.User.Email, Room = reservation.RehearsalRoom, BandName = reservation.BandName, Day = reservation.Start, StartHour = reservation.Start.Hour, EndHour = reservation.End.Hour, Id = reservation.Id }; Dictionary <string, int> equipments = new Dictionary <string, int>(); /* * foreach (var e in reservation.Equipments) * { * viewModel.Equipments[e.Name] = true; * } */ foreach (var pair in _reservationService.GetReservationEquipmentPairsForReservarion(reservationId)) { viewModel.Equipments[pair.EquipmentName] = true; } RehearsalRoom room = _reservationService.GetRoom(reservation.RehearsalRoomId); RehearsalStudio studio = _reservationService.Studios.FirstOrDefault(l => l.Rooms.Contains(room)); foreach (var e in studio.Equipments) { if (!viewModel.Equipments.ContainsKey(e.Name)) //ha nincs foglalva az aktuális foglaláshoz { viewModel.Equipments[e.Name] = false; } } return(View(viewModel)); }