/// <summary> /// Adds playlist track to database /// </summary> /// <param name="playlistTrack"></param> public static void AddPlaylistTrack(PlaylistTrackToAdd playlistTrack) { using (var db = new TraxxPlayerContext()) { if (playlistTrack != null) { if(GetPlaylistTracks(playlistTrack.PlaylistID).Where(pt => pt.TrackID == playlistTrack.TrackID).FirstOrDefault() != null) { throw new Exception("Playlist track with the TrackID and PlaylistID provided already exists. Add playlist track failed."); } int trackOrder = 0; var trackWithHighestTrackOrder = GetPlaylistTracks(playlistTrack.PlaylistID).OrderByDescending(pt => pt.TrackOrder).FirstOrDefault(); if(trackWithHighestTrackOrder != null) { trackOrder = trackWithHighestTrackOrder.TrackOrder + 1; } db.PlaylistTracks.Add(new PlaylistTrack() { PlaylistID = playlistTrack.PlaylistID, TrackID = playlistTrack.TrackID, TrackOrder = trackOrder }); db.SaveChanges(); } else { throw new Exception("Playlist track to add cannot be null. Add playlist track failed."); } } }
/// <summary> /// Ads user to database /// </summary> /// <param name="user"></param> public static void AddUser(UserToAdd user) { if (user == null) { throw new Exception("User cannot be null. Add user failed"); } if (UserExist(user.username)) { throw new Exception("User with provided user name already exists. Add failed"); } using (var db = new TraxxPlayerContext()) { if (user.isDefault) { var currentDefaultUser = GetDefaultUser(); if (currentDefaultUser != null) { currentDefaultUser.isDefault = false; } } db.Users.Add(new User() { username = user.username, isDefault = user.isDefault }); db.SaveChanges(); } }
/// <summary> /// Modifies log in database /// </summary> /// <param name="log"></param> public static void ModifyLog(LogToDisplay log) { if (log != null) { using (var db = new TraxxPlayerContext()) { if (LogExist(log.id)) { var logToModify = db.Logs.First(l => l.id == log.id); logToModify.Message = log.Message; logToModify.MessageType = log.MessageType; logToModify.UserID = log.UserID; logToModify.Source = log.Source; db.SaveChanges(); } else { throw new Exception("Log with provided id doesn't exist. Modify Log failed"); } } } else { throw new Exception("Log cannot be null. Modify Log failed"); } }
/// <summary> /// Adds log to database /// </summary> /// <param name="log"></param> public static void AddLog(LogToAdd log) { using (var db = new TraxxPlayerContext()) { if (log != null) { db.Logs.Add(new Log(log.UserID, (int)log.MessageType, log.Source, log.Message)); db.SaveChanges(); } else { throw new Exception("Log to add cannot be null. Add Log failed."); } } }
/// <summary> /// Deletes track history from database /// </summary> /// <param name="trackHistoryID"></param> public static void DeleteTrackHistory(int trackHistoryID) { using (var db = new TraxxPlayerContext()) { var trackHistoryToDelete = db.TracksHistory.Where(th => th.id == trackHistoryID).FirstOrDefault(); if (trackHistoryToDelete != null) { db.TracksHistory.Remove(trackHistoryToDelete); db.SaveChanges(); } else { throw new Exception("Track history with the id specified does not exist. Delete track history failed."); } } }
/// <summary> /// Deletes log from database /// </summary> /// <param name="logID"></param> public static void DeleteLog(int logID) { using (var db = new TraxxPlayerContext()) { var logToDelete = db.Logs.Where(l => l.id == logID).FirstOrDefault(); if (logToDelete != null) { db.Logs.Remove(logToDelete); db.SaveChanges(); } else { throw new Exception("Log with the id specified does not exist. Delete Log failed."); } } }
/// <summary> /// Deletes user from database /// </summary> /// <param name="id"></param> public static void DeleteUser(int id) { using (var db = new TraxxPlayerContext()) { var userToDelete = db.Users.FirstOrDefault(u => u.id == id); if (userToDelete == null) { throw new Exception("There is no user with the id provided. Delete failed."); } else { db.Users.Remove(userToDelete); db.SaveChanges(); } } }
/// <summary> /// Deletes playlist track from database /// </summary> /// <param name="playlistTrackID"></param> public static void DeletePlaylistTrack(int playlistTrackID) { using (var db = new TraxxPlayerContext()) { var playlistTrackToDelete = db.PlaylistTracks.Where(pt => pt.id == playlistTrackID).FirstOrDefault(); if (playlistTrackToDelete != null) { db.PlaylistTracks.Remove(playlistTrackToDelete); db.SaveChanges(); } else { throw new Exception("Playlist track with the id specified does not exist. Delete playlist track failed."); } } }
/// <summary> /// Modifies playlist track in database /// </summary> /// <param name="playlistTrack"></param> public static void ModifyPlaylistTrack(PlaylistTrackToDisplay playlistTrack) { using (var db = new TraxxPlayerContext()) { if(!PlaylistTrackExist(playlistTrack.id)) { throw new Exception("Playlist track with the id specified does not exist. Modify playlist track failed."); } var playlistTrackToModify = db.PlaylistTracks.Where(pt => pt.id == playlistTrack.id).FirstOrDefault(); playlistTrackToModify.id = playlistTrack.id; playlistTrackToModify.PlaylistID = playlistTrack.PlaylistID; playlistTrackToModify.TrackID = playlistTrack.TrackID; playlistTrackToModify.TrackOrder = playlistTrack.TrackOrder; db.SaveChanges(); } }
/// <summary> /// Removes current default flag in database /// </summary> /// <param name="id"></param> public static void RemoveDefaultUser(int id) { using (var db = new TraxxPlayerContext()) { var defaultUser = db.Users.Where(u => u.id == id).FirstOrDefault(); if (defaultUser != null) { defaultUser.isDefault = false; db.SaveChanges(); } else { throw new Exception("There is no user with the id provided. Removing default user failed."); } } }
/// <summary> /// Adds playlist to database /// </summary> /// <param name="playlist"></param> public static void AddPlaylist(PlaylistToAdd playlist) { using (var db = new TraxxPlayerContext()) { if (playlist != null) { db.Playlists.Add(new Playlist() { Name = playlist.Name, UserID = playlist.UserID }); db.SaveChanges(); } else { throw new Exception("Playlist to add cannot be null. Add playlist failed."); } } }
/// <summary> /// Adds track history to database /// </summary> /// <param name="trackhistory"></param> public static void AddTrackHistory(TrackHistoryToAdd trackhistory) { using (var db = new TraxxPlayerContext()) { if (trackhistory != null) { db.TracksHistory.Add(new TrackHistory() { TrackID = trackhistory.TrackID, UserID = trackhistory.UserID }); db.SaveChanges(); } else { throw new Exception("Track history to add cannot be null. Add track history failed."); } } }
/// <summary> /// Deletes playlist track from database /// </summary> /// <param name="playlistID"></param> /// <param name="trackID"></param> public static void DeletePlaylistTrack(int playlistID, int trackID) { using (var db = new TraxxPlayerContext()) { if(!PlaylistService.PlaylistExist(playlistID)) { throw new Exception($"Could not find playlist with ID {playlistID}. Delete playlist track failed."); } var playlistTrack = db.PlaylistTracks.Where(pt => pt.PlaylistID == playlistID && pt.TrackID == trackID).FirstOrDefault(); if(playlistTrack != null) { db.PlaylistTracks.Remove(playlistTrack); db.SaveChanges(); } else { throw new Exception($"Playlist with ID {playlistID} does not contain track with ID {trackID}. Delete playlist track failed."); } } }
/// <summary> /// Sets user as default user. Default user is automatically logged in during launch. /// </summary> /// <param name="id"></param> public static void SetDefaultUser(int id) { using (var db = new TraxxPlayerContext()) { var nextDefaultUser = db.Users.Where(u => u.id == id).FirstOrDefault(); if (nextDefaultUser != null) { var previousDefaultUser = db.Users.Where(u => u.isDefault == true).FirstOrDefault(); if (previousDefaultUser != null) { previousDefaultUser.isDefault = false; } nextDefaultUser.isDefault = true; db.SaveChanges(); } else { throw new Exception("There is no user with the id provided. Setting default user failed."); } } }
/// <summary> /// Adds like to database /// </summary> /// <param name="like"></param> public static void AddLike(LikeToAdd like) { using (var db = new TraxxPlayerContext()) { if (like != null) { if (db.Likes.Where(l => l.TrackID == like.TrackID).FirstOrDefault() != null) { throw new Exception($"Track with id {like.TrackID} is already in Likes for user {like.UserID} . Add like failed."); } db.Likes.Add(new Like() { TrackID = like.TrackID, UserID = like.UserID }); db.SaveChanges(); } else { throw new Exception("Like to add cannot be null. Add like failed."); } } }
/// <summary> /// Modifies track history in database /// </summary> /// <param name="trackHistory"></param> public static void Modifytrackhistory(TrackHistoryToDisplay trackHistory) { if (trackHistory != null) { using (var db = new TraxxPlayerContext()) { if (TrackHistoryExist(trackHistory.id)) { var trackhistoryToModify = db.TracksHistory.First(th => th.id == trackHistory.id); trackhistoryToModify.TrackID = trackHistory.TrackID; db.SaveChanges(); } else { throw new Exception("Track history with provided id doesn't exist. Modify track history failed"); } } } else { throw new Exception("Track history cannot be null. Modify track history failed"); } }
/// <summary> /// Modifies playlist in database /// </summary> /// <param name="playlist"></param> public static void ModifyPlaylist(PlaylistToDisplay playlist) { if (playlist != null) { using (var db = new TraxxPlayerContext()) { if (PlaylistExist(playlist.id)) { var playlistToModify = db.Playlists.First(p => p.id == playlist.id); playlistToModify.Name = playlist.Name; db.SaveChanges(); } else { throw new Exception("Playlist with provided id doesn't exist. Modify failed"); } } } else { throw new Exception("Playlist cannot be null. Modify failed"); } }
/// <summary> /// Modifies like in database /// </summary> /// <param name="like"></param> public static void ModifyLike(LikeToDisplay like) { if (like != null) { using (var db = new TraxxPlayerContext()) { if (LikeExist(like.id)) { var likeToModify = db.Likes.First(l => l.id == like.id); likeToModify.TrackID = like.TrackID; db.SaveChanges(); } else { throw new Exception("Like with provided id doesn't exist. Modify like failed"); } } } else { throw new Exception("Like cannot be null. Modify like failed"); } }
/// <summary> /// Modifies user in database /// </summary> /// <param name="user"></param> public static void ModifyUser(UserToDisplay user) { if (user != null) { using (var db = new TraxxPlayerContext()) { if (UserExist(user.id)) { var userToModify = db.Users.First(u => u.id == user.id); userToModify.username = user.username; userToModify.isDefault = user.isDefault; db.SaveChanges(); } else { throw new Exception("User with provided id doesn't exist. Add failed"); } } } else { throw new Exception("User cannot be null. Add failed"); } }