/// <summary> /// Adds the track playlist. /// </summary> /// <param name="trackPlayList">The track playlist.</param> public void AddTrackPlayList(List<TrackPlay> trackPlayList) { using (RENTIT21Entities context = new RENTIT21Entities()) { foreach (TrackPlay tp in trackPlayList) { context.TrackPlays.Add(tp); } context.SaveChanges(); } }
/// <summary> /// Adds a MP3 File to a Book /// </summary> /// <param name="bookId"></param> /// <param name="filePath">Path of the File</param> /// <param name="narrator"></param> public void AddAudio(int bookId, string filePath, string narrator) { using (RENTIT21Entities proxy = new RENTIT21Entities()) { var books = from book in proxy.SMUbooks where book.id == bookId select book; if (books.Any() == false) { throw new ArgumentException("No book with bookId = " + bookId); } SMUbook theBook = books.First(); theBook.audioNarrator = narrator; theBook.audioFilePath = filePath; proxy.SaveChanges(); } }
/// <summary> /// Adds a Book object to the database /// </summary> /// <param name="title"></param> /// <param name="author"></param> /// <param name="description"></param> /// <param name="genre"></param> /// <param name="dateAdded"></param> /// <param name="price"></param> /// <returns>Book Id</returns> public int AddBook(string title, string author, string description, string genre, DateTime dateAdded, double price) { using (RENTIT21Entities proxy = new RENTIT21Entities()) { SMUbook theBook = new SMUbook() { title = title, author = author, description = description, genre = genre, price = price, dateAdded = dateAdded, SMUrentals = new Collection<SMUrental>(), }; proxy.SMUbooks.Add(theBook); proxy.SaveChanges(); return theBook.id; } }
/// <summary> /// Creates a vote. /// </summary> /// <param name="rating">The rating.</param> /// <param name="userId">The user id.</param> /// <param name="trackId">The track id.</param> /// <exception cref="System.ArgumentException"> /// no track with track id [+trackId+] /// or /// no user with user id [+userId+] /// </exception> public void CreateVote(int rating, int userId, int trackId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var tracks = from track in context.Tracks where track.Id == trackId select track; if (tracks.Any() == false) throw new ArgumentException("no track with track id [" + trackId + "]"); Track theTrack = tracks.First(); var users = from user in context.Users where user.Id == userId select user; if (users.Any() == false) throw new ArgumentException("no user with user id [" + userId + "]"); User theUser = users.First(); Vote vote = new Vote() { TrackId = trackId, UserId = userId, Value = rating, Date = DateTime.UtcNow, User = theUser, Track = theTrack }; // Add Vote to database and assign it an id context.Votes.Add(vote); context.SaveChanges(); // Update the user with the new vote. theUser.Votes.Add(vote); //Update the track if (rating < 0) { theTrack.DownVotes += 1; } else if (rating > 0) { theTrack.UpVotes += 1; } context.SaveChanges(); } }
/// <summary> /// Creates the track entry. /// </summary> /// <param name="channelId">The channel id.</param> /// <param name="track">The track.</param> /// <returns></returns> /// <exception cref="System.ArgumentException">No channel with channel id [ + channelId + ].</exception> public Track CreateTrackEntry(int channelId, Track track) { using (RENTIT21Entities context = new RENTIT21Entities()) { var channels = from c in context.Channels where c.Id == channelId select c; if (channels.Any() == false) throw new ArgumentException("No channel with channel id [" + channelId + "]."); track.Channel = channels.First(); context.Tracks.Add(track); context.SaveChanges(); } return track; }
/// <summary> /// Creates a user in the database /// </summary> /// <param name="email">string email</param> /// <param name="name"> string name</param> /// <param name="password">string password</param> /// <param name="isAdmin">boolean indicating admin status</param> /// <returns>user Id</returns> public int SignUp(string email, string name, string password, bool isAdmin) { SMUuser user = new SMUuser(); using (RENTIT21Entities proxy = new RENTIT21Entities()) { user.email = email; user.username = name; user.password = password; user.isAdmin = isAdmin; proxy.SMUusers.Add(user); proxy.SaveChanges(); } return user.id; }
/// <summary> /// Deletes the vote. /// </summary> /// <param name="userId">The user id.</param> /// <param name="trackId">The track id.</param> public void DeleteVote(int userId, int trackId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var votes = from v in context.Votes where v.UserId == userId && v.TrackId == trackId select v; if (votes.Any() == false) throw new ArgumentException("No vote with userId [" + userId + "] and trackId [" + trackId + "]"); Vote vote = votes.First(); //remove the vote from the user vote.User.Votes.Remove(vote); //remove the vote from the track if (vote.Value < 0) { vote.Track.DownVotes -= 1; } else if (vote.Value > 0) { vote.Track.UpVotes -= 1; } vote.Track.Votes.Remove(vote); //remove the vote itself context.Votes.Remove(vote); context.SaveChanges(); } }
/// <summary> /// Removes the user from the database. This method also removed all votes by the user /// </summary> /// <param name="userId">The user id.</param> /// <exception cref="System.ArgumentException">No user with user id [+userId+]</exception> /// <exception cref="System.Exception">End of \RemoveUser\. User was not get removed from the database.</exception> public void DeleteUser(int userId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var users = from user in context.Users where user.Id == userId select user; if (users.Any() == false) throw new ArgumentException("No user with user id [" + userId + "]"); User theUser = users.First(); // Remove all votes from the user var votes = from vote in context.Votes where vote.UserId == theUser.Id select vote; foreach (Vote vote in votes) { context.Votes.Remove(vote); } context.Users.Remove(theUser); context.SaveChanges(); // Verify deletion succeeded users = from user in context.Users where user.Id == theUser.Id select user; if (users.Any() == true) throw new Exception("End of \"RemoveUser\". User was not get removed from the database."); } }
/// <summary> /// Deletes the track plays associated with channel id. /// </summary> /// <param name="channelId">The channel id.</param> /// <param name="datetime">The datetime.</param> public void DeleteTrackPlaysByChannelId(int channelId, DateTime datetime) { using (RENTIT21Entities context = new RENTIT21Entities()) { var trackplays = from tp in context.TrackPlays where tp.Track.ChannelId == channelId && tp.TimePlayed > datetime select tp; foreach (TrackPlay tp in trackplays) { context.TrackPlays.Remove(tp); } context.SaveChanges(); } }
/// <summary> /// Removes the track. /// </summary> /// <param name="track">The track.</param> public void DeleteTrackEntry(ITU.DatabaseWrapperObjects.Track track) { using (RENTIT21Entities context = new RENTIT21Entities()) { var channels = from channel in context.Channels where channel.Id == track.ChannelId select channel; var tracks = from atrack in context.Tracks where atrack.Id == track.Id select atrack; if (tracks.Any() == false) throw new ArgumentException("The track entry is not in the database"); Track theTrack = tracks.First(); // If the track is associated with a channel, remove it from the channel as well if (channels.Any() == true) { Channel theChannel = channels.First(); theChannel.Tracks.Remove(theTrack); } context.Tracks.Remove(theTrack); context.SaveChanges(); } }
/// <summary> /// Unsubscribes the user form the channel. /// </summary> /// <param name="userId">The user id.</param> /// <param name="channelId">The channel id.</param> /// <exception cref="System.ArgumentException"> /// No user with userId [ + userId + ] /// or /// No channel with channelId [ + channelId + ] /// </exception> public void UnSubscribe(int userId, int channelId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var users = from user in context.Users where user.Id == userId select user; if (users.Any() == false) throw new ArgumentException("No user with userId [" + userId + "]"); var channels = from channel in context.Channels where channel.Id == channelId select channel; if (channels.Any() == false) throw new ArgumentException("No channel with channelId [" + channelId + "]"); User theUser = users.First(); Channel theChannel = channels.First(); theChannel.Subscribers.Remove(theUser); context.SaveChanges(); } }
/// <summary> /// Creates the user. /// </summary> /// <param name="username">The username.</param> /// <param name="email">The email.</param> /// <param name="password">The password.</param> /// <returns>The id of the user.</returns> public User SignUp(string username, string email, string password) { using (RENTIT21Entities context = new RENTIT21Entities()) { var users = from u in context.Users where u.Username.Equals(username) select u; if (users.Any()) { throw new ArgumentException("Username is already taken."); } users = from u in context.Users where u.Email.Equals(email) select u; if (users.Any()) { throw new ArgumentException("Email is already in use."); } User user = new User() { Username = username, Email = email, Password = password, Comments = new Collection<Comment>(), Channels = new Collection<Channel>(), SubscribedChannels = new Collection<Channel>(), Votes = new Collection<Vote>() }; context.Users.Add(user); context.SaveChanges(); return user; } }
public void IncrementChannelPlays(int channelId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var channels = from c in context.Channels where c.Id == channelId select c; if (channels.Any()) { Channel channel = channels.First(); if (channel.Hits.HasValue) { channel.Hits = channel.Hits + 1; } else { channel.Hits = 1; } } context.SaveChanges(); } }
/// <summary> /// Creates a channel. /// </summary> /// <param name="channelName">Name of the channel.</param> /// <param name="userId">The id of the user creating the channel.</param> /// <param name="description">The description of the channel.</param> /// <param name="genreIds">The genre ids.</param> /// <returns> /// The created channel. /// </returns> /// <exception cref="System.ArgumentException"> /// Channel with channelname [ + channelName + ] already exists /// or /// No user with userId [ + userId + ] /// </exception> /// <exception cref="System.Exception">Channel got created and saved in the database but is not in the database.... O.ô.</exception> public Channel CreateChannel(string channelName, int userId, string description, int[] genreIds) { using (RENTIT21Entities context = new RENTIT21Entities()) { // Check that no channel with channelName already exists var channels = from channel in context.Channels where channel.Name.ToLower().Equals(channelName.ToLower()) select channel; if (channels.Any() == true) throw new ArgumentException("Channel with channelname [" + channelName + "] already exists"); var users = from user in context.Users where user.Id == userId select user; if (users.Any() == false) throw new ArgumentException("No user with userId [" + userId + "]"); //Set the genres of the channel var genres = from genre in context.Genres where genreIds.Contains(genre.Id) select genre; // Create the channel object Channel theChannel = new Channel() { Comments = new Collection<Comment>(), Description = description, Genres = genres.Any() ? genres.ToList() : new List<Genre>(), UserId = userId, Name = channelName, ChannelOwner = users.First(), Subscribers = new Collection<User>(), Hits = null, Rating = null, Tracks = new Collection<Track>() }; context.Channels.Add(theChannel); context.SaveChanges(); channels = from channel in context.Channels.Where(channel => channel.Name == channelName && channel.UserId == userId) select channel; if (channels.Any() == true) { theChannel = channels.First(); } else { // The channel does not exist in the database, either something f****d up or another thread removed it already. throw new Exception("Channel got created and saved in the database but is not in the database.... O.ô."); } return theChannel; } }
/// <summary> /// Adds an image to the Book /// </summary> /// <param name="bookId"></param> /// <param name="fullPath"></param> public void AddImage(int bookId, string fullPath) { using (RENTIT21Entities context = new RENTIT21Entities()) { var books = from b in context.SMUbooks where b.id == bookId select b; if (books.Any() == false) { throw new ArgumentException("No book with bookId = " + bookId); } SMUbook book = books.First(); book.imageFilePath = fullPath; context.SaveChanges(); } }
/// <summary> /// Updates the database with info about a User /// </summary> /// <param name="userId">int userId</param> /// <param name="email">string email</param> /// <param name="username">string username</param> /// <param name="password">string password</param> /// <param name="isAdmin">boolean indicating admin status</param> /// <returns>The updated user</returns> public User UpdateUserInfo(int userId, string email, string username, string password, bool? isAdmin) { using (RENTIT21Entities proxy = new RENTIT21Entities()) { var users = from u in proxy.SMUusers where u.id == userId select u; if (users.Any() == false) { throw new ArgumentException("No SMUuser with userid/password combination = " + userId + "/" + password); } SMUuser user = users.First(); if (email != null) user.email = email; if (username != null) user.username = username; if (password != null) user.password = password; if (isAdmin != null) user.isAdmin = (bool)isAdmin; proxy.SaveChanges(); return user.GetUser(); } }
/// <summary> /// Updates Info about a Book /// </summary> /// <param name="bookId"></param> /// <param name="title"></param> /// <param name="author"></param> /// <param name="description"></param> /// <param name="genre"></param> /// <param name="price"></param> /// <returns>Book object</returns> public Book UpdateBook(int bookId, String title, String author, String description, String genre, double? price) { SMUbook theBook; using (RENTIT21Entities proxy = new RENTIT21Entities()) { var books = from book in proxy.SMUbooks where book.id == bookId select book; if (books.Any() == false) { throw new ArgumentException("No book with bookId = " + bookId); } theBook = books.First(); if (title != null) theBook.title = title; if (author != null) theBook.author = author; if (description != null) theBook.description = description; if (genre != null) theBook.genre = genre; if (price != null) theBook.price = (double)price; proxy.SaveChanges(); } return theBook.GetBook(); }
/// <summary> /// Removes the channel from the database. /// </summary> /// <param name="channel">The channel.</param> /// <exception cref="System.ArgumentException">No channel with channel found</exception> /// <exception cref="System.Exception">End of DeleteChannel, but channel entry is still in database</exception> public void DeleteChannel(DatabaseWrapperObjects.Channel channel) { using (RENTIT21Entities context = new RENTIT21Entities()) { int channelId = channel.Id; var channels = from c in context.Channels where c.Id == channel.Id select c; if (channels.Any() == false) throw new ArgumentException("No channel with channel found"); Channel dbChannel = channels.First(); dbChannel.Subscribers.Clear(); dbChannel.Genres.Clear(); context.Channels.Remove(dbChannel); context.SaveChanges(); channels = from c in context.Channels where c.Id == channelId select c; if (channels.Any() == true) throw new Exception("End of DeleteChannel, but channel entry is still in database"); } }
/// <summary> /// Deletes the database data. /// </summary> public void DeleteDatabaseData() { const string deleteAllDataSql = "EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' " + "EXEC sp_MSForEachTable 'ALTER TABLE ? DISABLE TRIGGER ALL' " + "EXEC sp_MSForEachTable 'DELETE FROM ?' " + "EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL' " + "EXEC sp_MSForEachTable 'ALTER TABLE ? ENABLE TRIGGER ALL'"; using (RENTIT21Entities context = new RENTIT21Entities()) { context.Database.ExecuteSqlCommand(deleteAllDataSql); context.SaveChanges(); } }
/// <summary> /// Updates the channel. /// </summary> /// <param name="channelId">The channel id.</param> /// <param name="ownerId">The owner id. Can be null.</param> /// <param name="channelName">Name of the channel. Can be null.</param> /// <param name="description">The description. Can be null.</param> /// <param name="hits">The hits. Can be null.</param> /// <param name="rating">The rating. Can be null.</param> /// <param name="streamUri">The stream URI.</param> /// <param name="genreIds">The genre ids.</param> /// <exception cref="System.ArgumentException">No channel with channel id [ + channelId + ] /// or /// No user with user id [ + ownerId + ]</exception> public void UpdateChannel(int channelId, int? ownerId, string channelName, string description, double? hits, double? rating, string streamUri, int[] genreIds) { using (RENTIT21Entities context = new RENTIT21Entities()) { var channels = from channel in context.Channels where channel.Id == channelId select channel; if (channels.Any() == false) throw new ArgumentException("No channel with channel id [" + channelId + "]"); Channel theChannel = channels.First(); if (ownerId != null) { var users = from user in context.Users where user.Id == ownerId select user; if (users.Any() == false) throw new ArgumentException("No user with user id [" + ownerId + "]"); theChannel.UserId = (int)ownerId; theChannel.ChannelOwner = users.First(); } if (channelName != null) theChannel.Name = channelName; if (description != null) theChannel.Description = description; if (hits != null) theChannel.Hits = (int)hits; if (rating != null) theChannel.Rating = rating; if (streamUri != null) theChannel.StreamUri = streamUri; if (genreIds != null) { //Set the genres of the channel theChannel.Genres.Clear(); if (genreIds.Length > 0) { var genres = from genre in context.Genres where genreIds.Contains(genre.Id) select genre; foreach (Genre g in genres) { theChannel.Genres.Add(g); } } } context.SaveChanges(); } }
/// <summary> /// Deletes the track entry. /// </summary> /// <param name="trackId">The track id.</param> public void DeleteTrackEntry(int trackId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var track = from t in context.Tracks where t.Id == trackId select t; if (track.Any()) { context.Tracks.Remove(track.First()); context.SaveChanges(); } } }
/// <summary> /// Updates the track. /// </summary> /// <param name="track">The track.</param> /// <exception cref="System.ArgumentException">No track with id [ + track.Id + ].</exception> public void UpdateTrack(Track track) { using (RENTIT21Entities context = new RENTIT21Entities()) { var tracks = from t in context.Tracks where t.Id == track.Id select t; if (tracks.Any() == false) throw new ArgumentException("No track with id [" + track.Id + "]."); Track tdb = tracks.First(); tdb.Artist = track.Artist; tdb.DownVotes = track.DownVotes; tdb.Length = track.Length; tdb.Name = track.Name; tdb.Path = track.Path; tdb.UpVotes = track.UpVotes; context.SaveChanges(); } }
/// <summary> /// Deletes the trackplays associated with track id. /// </summary> /// <param name="trackId">The track id.</param> public void DeleteTrackPlaysByTrackId(int trackId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var trackplays = from tp in context.TrackPlays where tp.TrackId == trackId select tp; foreach (TrackPlay tp in trackplays) { context.TrackPlays.Remove(tp); } context.SaveChanges(); } }
/// <summary> /// Updates the user. /// </summary> /// <param name="userId">The user id.</param> /// <param name="username">The username. Can be null.</param> /// <param name="password">The password. Can be null.</param> /// <param name="email">The email. Can be null</param> /// <exception cref="System.ArgumentException">No user with user id[ + userId + ]</exception> public void UpdateUser(int userId, string username, string password, string email) { using (RENTIT21Entities context = new RENTIT21Entities()) { var users = from user in context.Users where user.Id == userId select user; if (users.Any() == false) throw new ArgumentException("No user with user id[" + userId + "]"); User theUser = users.First(); if (username != null) theUser.Username = username; if (password != null) theUser.Password = password; if (email != null) theUser.Email = email; context.SaveChanges(); } }
/// <summary> /// Deletes all comments from a specific user. /// </summary> /// <param name="userId">The id of the user</param> public void DeleteUserComments(int userId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var comments = from c in context.Comments where c.UserId == userId select c; foreach (Comment c in comments) { context.Comments.Remove(c); } context.SaveChanges(); } }
/// <summary> /// Deletes all votes for a specific user. /// </summary> /// <param name="userId">The user id.</param> public void DeleteVotesForUser(int userId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var votes = from v in context.Votes where v.UserId == userId select v; foreach (Vote v in votes) { //remove the vote from the track v.Track.Votes.Remove(v); if (v.Value < 0) { v.Track.DownVotes -= 1; } else if (v.Value > 0) { v.Track.UpVotes -= 1; } // remove the vote itself context.Votes.Remove(v); } context.SaveChanges(); } }
/// <summary> /// Creates a comment. /// </summary> /// <param name="comment">The comment.</param> /// <param name="userId">The user id.</param> /// <param name="channelId">The channel id.</param> /// <exception cref="System.ArgumentException"> /// No user with user id [+userId+] /// or /// No channel with channel id [ + channelId + ] /// </exception> public void CreateComment(string comment, int userId, int channelId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var users = from user in context.Users where user.Id == userId select user; if (users.Any() == false) throw new ArgumentException("No user with user id [" + userId + "]"); User theUser = users.First(); var channels = from channel in context.Channels where channel.Id == channelId select channel; if (channels.Any() == false) throw new ArgumentException("No channel with channel id [" + channelId + "]"); Channel theChannel = channels.First(); Comment theComment = new Comment() { ChannelId = channelId, Content = comment, UserId = userId, Date = DateTime.UtcNow, User = theUser, Channel = theChannel }; context.Comments.Add(theComment); context.SaveChanges(); // Update the channel with the comment theChannel.Comments.Add(theComment); context.SaveChanges(); } }
/// <summary> /// Makes a Rent Entry in the Database /// </summary> /// <param name="userId"></param> /// <param name="bookId"></param> /// <param name="time"></param> /// <param name="mediaType">Type of media the User wants to Rent</param> /// <returns>The Rent Id</returns> public int RentBook(int userId, int bookId, DateTime time, int mediaType) { using (RENTIT21Entities proxy = new RENTIT21Entities()) { var books = from book in proxy.SMUbooks where book.id == bookId select book; if (books.Any() == false) throw new ArgumentException("no book with bookId = " + bookId); var us = from user in proxy.SMUusers where user.id == userId select user; if (us.Any() == false) throw new ArgumentException("No user with that userid"); SMUrental theRental = new SMUrental() { userId = userId, startDate = time, bookId = bookId, mediaType = mediaType }; SMUbook theBook = books.First(); SMUuser theUser = us.First(); if (mediaType == 0 || mediaType == 2) { // Only rent the pdf, throw exception if there's no pdf path if (theBook.PDFFilePath == null) throw new ArgumentException("mediaType parameter specified pdf. The book [" + theBook.title + "] with id [" + theBook.id + "] is not associated with a pdf. "); } if (mediaType == 1) { // Only rent the audio for the book, throw exception if there's no audio path if (theBook.audioFilePath == null) throw new ArgumentException("mediaType parameter specified audio. The book [" + theBook.title + "] with id [" + theBook.id + "] is not associated with audio. "); } theRental.SMUbook = theBook; theRental.SMUuser = theUser; theBook.hit += 1; proxy.SMUrentals.Add(theRental); proxy.SaveChanges(); return theRental.id; } }
/// <summary> /// Deletes all the users votes on a given channel /// </summary> /// <param name="userId">The id of the user</param> /// <param name="channelId">The id of the channel</param> public void DeleteVotesForUser(int userId, int channelId) { using (RENTIT21Entities context = new RENTIT21Entities()) { var votes = from v in context.Votes where v.UserId == userId && v.Track.ChannelId == channelId select v; foreach (Vote v in votes) { context.Votes.Remove(v); } context.SaveChanges(); } }
/// <summary> /// Creates a genre with the name. /// </summary> /// <param name="genreName">The name of the genre.</param> /// <returns>The id of the genre</returns> public int CreateGenre(string genreName) { using (RENTIT21Entities context = new RENTIT21Entities()) { var genres = from g in context.Genres where g.Name == genreName select g; if (genres.Any()) throw new ArgumentException("A genre with the name already exists"); Genre genre = new Genre(); genre.Name = genreName; context.Genres.Add(genre); context.SaveChanges(); return genre.Id; } }