/// <summary> /// Updates an existing MediaMusic in the database /// </summary> /// <param name="mediaId">The ID of the media</param> /// <param name="title">The title of the media</param> /// <param name="description">The description</param> /// <param name="artist">The artist</param> /// <param name="genre">The genre</param> /// <param name="tracklist">The tracklist</param> /// <param name="tag">The tag</param> /// <param name="categoryId">The category ID</param> public void UpdateMediaMusic(int mediaId, string title, string description, string artist, string genre, string tracklist, string tag, int categoryId) { using (MediathekEntities context = new MediathekEntities()) { var media = context.MediaSet.OfType<MediaMusic>().FirstOrDefault(m => m.MediaId == mediaId); media.Title = title; media.Description = description; media.Artist = artist; media.Genre = genre; media.Tracklist = tracklist; media.Tag = tag; media.Category = context.Categories.FirstOrDefault(c => c.CategoryId == categoryId); context.SaveChanges(); } }
/// <summary> /// Create a new MediaBook in the database /// </summary> /// <param name="title">The title of the media</param> /// <param name="description">The description</param> /// <param name="director">The director</param> /// <param name="actors">Actors</param> /// <param name="runtime">The runtime of the video</param> /// <param name="tag">The tag</param> /// <param name="categoryId">The category ID</param> public void CreateMediaVideo(string title, string description, string director, string actors, int? runtime, string tag, int categoryId) { using (MediathekEntities context = new MediathekEntities()) { MediaVideo media = new MediaVideo(); media.Title = title; media.Description = description; media.Director = director; media.Actors = actors; media.Length = runtime; media.Tag = tag; media.Category = context.Categories.FirstOrDefault(c => c.CategoryId == categoryId); media.CreationDate = DateTime.Now; // set media state to "available" media.MediaState = context.MediaStates.FirstOrDefault(s => s.MediaSateId == 1); // set media type media.MediaType = context.MediaTypes.FirstOrDefault(mt => mt.MediaTypeId == 3); context.AddToMediaSet(media); context.SaveChanges(); } }
/// <summary> /// Updates an existing MediaBook in the database /// </summary> /// <param name="mediaId">The ID of the media</param> /// <param name="title">The title of the media</param> /// <param name="description">The description</param> /// <param name="director">The director</param> /// <param name="actors">Actors</param> /// <param name="runtime">The runtime of the video</param> /// <param name="tag">The tag</param> /// <param name="categoryId">The category ID</param> public void UpdateMediaVideo(int mediaId, string title, string description, string director, string actors, int? runtime, string tag, int categoryId) { using (MediathekEntities context = new MediathekEntities()) { var media = context.MediaSet.OfType<MediaVideo>().FirstOrDefault(m => m.MediaId == mediaId); media.Title = title; media.Description = description; media.Director = director; media.Actors = actors; media.Length = runtime; media.Tag = tag; media.Category = context.Categories.FirstOrDefault(c => c.CategoryId == categoryId); context.SaveChanges(); } }
/// <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> /// Create a new administrator account /// </summary> /// <param name="username">The username for the new account</param> /// <param name="password">The password for the new account</param> /// <param name="email">The email for the new account</param> /// <returns>The newly created administrator account</returns> public Administrator CreateAdministrator(string username, string password, string email) { using (MediathekEntities context = new MediathekEntities()) { var admin = new Administrator(); admin.Username = username; admin.Password = password; admin.Email = email; context.AddToAdministrators(admin); context.SaveChanges(); return admin; } }
/// <summary> /// Get the image of a specified media /// </summary> /// <param name="mediaId">The ID fo the media</param> /// <param name="arr">The byte array</param> /// <returns>Image as byte array</returns> public void SaveMediaImage(int mediaId, byte[] arr) { using (MediathekEntities context = new MediathekEntities()) { var media = context.MediaSet.FirstOrDefault(m => m.MediaId == mediaId); media.Image = arr; context.SaveChanges(); } }
/// <summary> /// Flags a media "order" as returned for a given user. /// </summary> /// <param name="userId">The ID of the user who has rented the media</param> /// <param name="mediaId">The ID of the media</param> /// <param name="checkOutDate">The checkout date of the media</param> public void ReturnMedia(int userId, int mediaId, DateTime checkOutDate) { using (MediathekEntities context = new MediathekEntities()) { var mr = (from m in context.MediaRentings.Include("Media") where m.UserId == userId && m.MediaId == mediaId && m.CheckOutDate == checkOutDate select m).FirstOrDefault(); if (mr != null) { // set checkin date to flag as returned mr.CheckInDate = DateTime.Now; // set the media's media state to available mr.Media.MediaState = context.MediaStates.FirstOrDefault(ms => ms.MediaSateId == 1); context.SaveChanges(); } } }
/// <summary> /// Create a new media /// </summary> /// <param name="mediaTypeId">The type ID of the media</param> /// <param name="title">The title of the media</param> /// <param name="description">The description of the media</param> /// <param name="categoryId">The category of the media</param> /// <returns>The ID of the created media</returns> public int CreateMedia(int mediaTypeId, string title, string description, int categoryId) { using (MediathekEntities context = new MediathekEntities()) { Media media = null; switch (mediaTypeId) { case 1: media = new MediaBook(); break; case 2: media = new MediaMusic(); break; case 3: media = new MediaVideo(); break; case 4: media = new MediaMisc(); break; default: throw new ConditionException(Localization.MsgErrorMediaTypeUnknown); } media.Title = title; media.Description = description; media.Category = context.Categories.FirstOrDefault(c => c.CategoryId == categoryId); media.CreationDate = DateTime.Now; // set media state to "available" media.MediaState = context.MediaStates.FirstOrDefault(s => s.MediaSateId == 1); // set media type media.MediaType = context.MediaTypes.FirstOrDefault(mt => mt.MediaTypeId == mediaTypeId); context.AddToMediaSet(media); context.SaveChanges(); // give back the ID of the created media return media.MediaId; } }
/// <summary> /// Delete all open reservations for a given media. /// </summary> /// <param name="mediaId">The ID of the media</param> public void DeleteOpenReservationsByMedia(int mediaId) { using (MediathekEntities context = new MediathekEntities()) { var reservations = context.MediaSet.Where(m => m.MediaId == mediaId) .SelectMany(m => m.Reservations); foreach (Reservation res in reservations) { if (res.EndDate == null) { context.DeleteObject(res); } } context.SaveChanges(); } }
/// <summary> /// Creates a given user account /// </summary> /// <param name="firstname">Fristname of new user</param> /// <param name="surname">Surname of new user</param> /// <param name="street">Street</param> /// <param name="zip">Zip</param> /// <param name="city">City</param> /// <param name="country">The country (ISO code)</param> /// <param name="email">Email address</param> /// <param name="password">The password of the user</param> /// <param name="newTitleId">The users title ID</param> /// <returns>ID of new user</returns> public int CreateUser(string firstname, string surname, string street, string zip, string city, string country, string email, string password, int? newTitleId) { using (MediathekEntities context = new MediathekEntities()) { User user = new User(); // set nav properites if (newTitleId != null) { user.Title = context.Titles.FirstOrDefault(t => t.TitleId == newTitleId); } else { user.Title = null; } // set normal properties user.Firstname = firstname; user.Surname = surname; user.Street = street; user.Zip = zip; user.City = city; user.CountryIso = country; user.Email = email; user.Password = password; // add to context and save context.AddToUsers(user); context.SaveChanges(); return user.UserId; } }
/// <summary> /// Updates a given user account /// </summary> /// <param name="userId">The ID of the user</param> /// <param name="firstname">Fristname of new user</param> /// <param name="surname">Surname of new user</param> /// <param name="street">Street</param> /// <param name="zip">Zip</param> /// <param name="city">City</param> /// <param name="country">The country (ISO code)</param> /// <param name="email">Email address</param> /// <param name="password">The password of the user</param> /// <param name="newTitleId">The users title ID</param> public void UpdateUser(int userId, string firstname, string surname, string street, string zip, string city, string country, string email, string password, int? newTitleId) { using (MediathekEntities context = new MediathekEntities()) { User user = context.Users.FirstOrDefault(u => u.UserId == userId); // set nav properites if (newTitleId != null) { user.Title = context.Titles.FirstOrDefault(t => t.TitleId == newTitleId); } else { user.Title = null; } // set normal properties user.Firstname = firstname; user.Surname = surname; user.Street = street; user.Zip = zip; user.City = city; user.CountryIso = country; user.Email = email; user.Password = password; // save context.SaveChanges(); } }
/// <summary> /// Mark a user as deleted /// </summary> /// <param name="id">The ID of the user</param> public void DeleteUserById(int id) { using (MediathekEntities context = new MediathekEntities()) { var usr = context.Users.Where(u => u.UserId == id); User user = (User)usr.First(); // set flag user.Deleted = true; // save changes context.SaveChanges(); } }
/// <summary> /// Delete an administrator account with the given administrator ID. /// Account is not realy deleted from database but marked as /// "delted". /// </summary> /// <param name="id">The ID of the administrator</param> public void DeleteAdministratorById(int id) { using (MediathekEntities context = new MediathekEntities()) { var adm = context.Administrators.Where(a => a.AdminId == id); Administrator admin = (Administrator)adm.First(); // set flag admin.Deleted = true; // save changes context.SaveChanges(); } }
/// <summary> /// Update the values of an existing administrator account /// </summary> /// <param name="id">The ID of the account</param> /// <param name="username">The new username of the account</param> /// <param name="password">The new password of the account</param> /// <param name="email">The email of the account</param> public void UpdateAdministrator(int id, string username, string password, string email) { using (MediathekEntities context = new MediathekEntities()) { var adm = context.Administrators.Where(a => a.AdminId == id); Administrator admin = (Administrator)adm.First(); admin.Username = username; admin.Password = password; admin.Email = email; context.SaveChanges(); } }
/// <summary> /// Create a new MediaMisc in the database /// </summary> /// <param name="title">The title of the media</param> /// <param name="description">The description</param> /// <param name="shortDescription">The short description</param> /// <param name="tag">The tag</param> /// <param name="categoryId">The category ID</param> public void CreateMediaMisc(string title, string description, string shortDescription, string tag, int categoryId) { using (MediathekEntities context = new MediathekEntities()) { MediaMisc media = new MediaMisc(); media.Title = title; media.Description = description; media.ShortDescription = shortDescription; media.Tag = tag; media.Category = context.Categories.FirstOrDefault(c => c.CategoryId == categoryId); media.CreationDate = DateTime.Now; // set media state to "available" media.MediaState = context.MediaStates.FirstOrDefault(s => s.MediaSateId == 1); // set media type media.MediaType = context.MediaTypes.FirstOrDefault(mt => mt.MediaTypeId == 4); context.AddToMediaSet(media); context.SaveChanges(); } }
/// <summary> /// Delete all open reservations for a given user. /// </summary> /// <param name="userId"></param> public void DeleteReservationsByUserId(int userId) { using (MediathekEntities context = new MediathekEntities()) { var reservations = context.Users.Where(u => u.UserId == userId) .SelectMany(u => u.Reservations); foreach (Reservation res in reservations) { context.DeleteObject(res); } context.SaveChanges(); } }
/// <summary> /// Updates an existing MediaMisc in the database /// </summary> /// <param name="mediaId">The ID of the media</param> /// <param name="title">The title of the media</param> /// <param name="description">The description</param> /// <param name="shortDescription">The short description</param> /// <param name="tag">The tag</param> /// <param name="categoryId">The category ID</param> public void UpdateMediaMisc(int mediaId, string title, string description, string shortDescription, string tag, int categoryId) { using (MediathekEntities context = new MediathekEntities()) { var media = context.MediaSet.OfType<MediaMisc>().FirstOrDefault(m => m.MediaId == mediaId); media.Title = title; media.Description = description; media.ShortDescription = shortDescription; media.Tag = tag; media.Category = context.Categories.FirstOrDefault(c => c.CategoryId == categoryId); context.SaveChanges(); } }
/// <summary> /// Change the password of a user /// </summary> /// <param name="userId">The ID of the user</param> /// <param name="oldPassword">The user's old password</param> /// <param name="newPassword">The user's new password</param> public void ChangeUserPassword(int userId, string oldPassword, string newPassword) { using (MediathekEntities context = new MediathekEntities()) { try { var user = (from u in context.Users where u.UserId == userId && u.Password.Equals(oldPassword) select u).FirstOrDefault(); user.Password = newPassword; context.SaveChanges(); } catch (NullReferenceException) { throw new ConditionException(Localization.MsgErrorOldPasswordInvalid); } } }
/// <summary> /// Mass rent media /// </summary> /// <param name="listMediaIds">List with media IDs that are being rented</param> /// <param name="userId">The ID of the user who is the owner of the lendings</param> /// <returns>Number of created entries</returns> public int CreateLending(List<int> listMediaIds, int userId) { using (MediathekEntities context = new MediathekEntities()) { int createCount = 0; foreach (int mediaId in listMediaIds) { if (IsMediaAvailableForRenting(mediaId, userId)) { MediaRenting mr = new MediaRenting(); mr.CheckOutDate = DateTime.Now; mr.Media = context.MediaSet.FirstOrDefault(m => m.MediaId == mediaId); mr.LeasingUser = context.Users.FirstOrDefault(u => u.UserId == userId); // set the media state to "unavaiable" mr.Media.MediaState = context.MediaStates.FirstOrDefault(ms => ms.MediaSateId == 2); context.AddToMediaRentings(mr); createCount++; // if user had open reservation for the current media, we close the reservation var res = from r in context.Reservations where r.User.UserId == userId && r.Media.MediaId == mediaId && r.EndDate == null select r; foreach (Reservation reservation in res) { reservation.EndDate = DateTime.Now; } } } // save to DB context.SaveChanges(); return createCount; } }
/// <summary> /// Create a new MediaBook in the database /// </summary> /// <param name="title">The title of the media</param> /// <param name="description">The description</param> /// <param name="author">The author</param> /// <param name="publisher">The publisher</param> /// <param name="publishingYear">The publishing year</param> /// <param name="tag">The tag</param> /// <param name="categoryId">The category ID</param> public void CreateMediaBook(string title, string description, string author, string publisher, int? publishingYear, string tag, int categoryId) { using (MediathekEntities context = new MediathekEntities()) { MediaBook media = new MediaBook(); media.Title = title; media.Description = description; media.Author = author; media.Publisher = publisher; media.PublishingYear = publishingYear; media.Tag = tag; media.Category = context.Categories.FirstOrDefault(c => c.CategoryId == categoryId); media.CreationDate = DateTime.Now; // set media state to "available" media.MediaState = context.MediaStates.FirstOrDefault(s => s.MediaSateId == 1); // set media type media.MediaType = context.MediaTypes.FirstOrDefault(mt => mt.MediaTypeId == 1); context.AddToMediaSet(media); context.SaveChanges(); } }
/// <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(); } } } }
/// <summary> /// Updates an existing MediaBook in the database /// </summary> /// <param name="mediaId">The ID of the media</param> /// <param name="title">The title of the media</param> /// <param name="description">The description</param> /// <param name="author">The author</param> /// <param name="publisher">The publisher</param> /// <param name="publishingYear">The publishing year</param> /// <param name="tag">The tag</param> /// <param name="categoryId">The category ID</param> public void UpdateMediaBook(int mediaId, string title, string description, string author, string publisher, int? publishingYear, string tag, int categoryId) { using (MediathekEntities context = new MediathekEntities()) { var media = context.MediaSet.OfType<MediaBook>().FirstOrDefault(m => m.MediaId == mediaId); media.Title = title; media.Description = description; media.Author = author; media.Publisher = publisher; media.PublishingYear = publishingYear; media.Tag = tag; media.Category = context.Categories.FirstOrDefault(c => c.CategoryId == categoryId); context.SaveChanges(); } }
/// <summary> /// Delete a reservation by its ID. /// </summary> /// <param name="reservationId">The ID of the reservation</param> public void DeleteReservation(int reservationId) { using (MediathekEntities context = new MediathekEntities()) { var res = from r in context.Reservations where r.ReservationId == reservationId select r; context.DeleteObject(res.First()); context.SaveChanges(); } }
/// <summary> /// Flag a media as deleted /// (Media isn't deleted from database, only flagged). /// </summary> /// <param name="mediaId"></param> public void FlagMediaAsDeleted(int mediaId) { using (MediathekEntities context = new MediathekEntities()) { var media = context.MediaSet.FirstOrDefault(m => m.MediaId == mediaId); // check if lendings exists media.Rentings.Load(); var rents = from r in media.Rentings where r.CheckInDate == null select r; if (rents.ToList().Count > 0) { throw new ConditionException(Localization.MsgMediaInUse); } else { media.Deleted = true; context.SaveChanges(); // delete all open reservations DeleteOpenReservationsByMedia(media.MediaId); } } }