/// <summary> /// Adds/inserts a new reservation for the given user for the given media. /// </summary> /// <param name="mediaId">The ID of the media</param> /// <param name="userId">The ID of the user</param> /// <exception cref="ConditionException">ConditionException thrown /// when user has already to many open reservations</exception> public void CreateReservation(int mediaId, int userId) { // check if max. reservation limit reached string limitStr = GetSetting(Constants.SettingMaxOpenReservations); int limit; if (!int.TryParse(limitStr, out limit)) { limit = 10; // set default } int openReservations = OpenReservations(userId); if (openReservations < limit) { if (!IsReservationOpen(mediaId, userId)) { using (MediathekEntities context = new MediathekEntities()) { Reservation res = new Reservation(); res.CreationDate = DateTime.Now; //res.Media = new Media() { MediaId = mediaId }; //res.User = new User() { UserId = userId }; res.Media = context.MediaSet.FirstOrDefault(m => m.MediaId == mediaId); res.User = context.Users.FirstOrDefault(u => u.UserId == userId); context.AddToReservations(res); context.SaveChanges(); } } } else { throw new ConditionException("Es existieren bereits zu viele offene Reservationen"); } }
/// <summary> /// Mass creation of reservations for a given user ID. /// The reservation limit is ignored. /// If a reservation already exists, new new reservation will created. /// </summary> /// <param name="listMediaIds">List with media IDs</param> /// <param name="userId">The user ID that gets the reservations</param> public void CreateReservation(List<int> listMediaIds, int userId) { using (MediathekEntities context = new MediathekEntities()) { foreach (int mediaId in listMediaIds) { if (!IsReservationOpen(mediaId, userId)) { Reservation res = new Reservation(); res.CreationDate = DateTime.Now; //res.Media = new Media() { MediaId = mediaId }; //res.User = new User() { UserId = userId }; res.Media = context.MediaSet.FirstOrDefault(m => m.MediaId == mediaId); res.User = context.Users.FirstOrDefault(u => u.UserId == userId); context.AddToReservations(res); context.SaveChanges(); } } } }