/// <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> /// Checks wheter user exist in database /// </summary> /// <param name="userName"></param> /// <returns></returns> public static bool UserExist(string userName) { using (var db = new TraxxPlayerContext()) { return(db.Users.Any(user => user.username == userName)); } }
/// <summary> /// Gets specific logs for user from database /// </summary> /// <param name="userID"></param> /// <param name="messageType"></param> /// <returns></returns> public static IEnumerable <LogToDisplay> GetLogs(int userID, LogMessageType messageType) { using (var db = new TraxxPlayerContext()) { return(db.Logs.Where(log => log.UserID == userID && log.MessageType == (int)messageType).Select(x => new LogToDisplay(x.UserID, (LogMessageType)x.MessageType, x.Source, x.Message)).ToList()); } }
/// <summary> /// Migrates database, used only during development /// </summary> public static void MigrateDatabase() { using (var db = new TraxxPlayerContext()) { db.Database.Migrate(); } }
/// <summary> /// Checks wheter user exist in database /// </summary> /// <param name="id"></param> /// <returns></returns> public static bool UserExist(int id) { using (var db = new TraxxPlayerContext()) { return(db.Users.Any(user => user.id == id)); } }
/// <summary> /// Gets specific logs for user from database with record limit /// </summary> /// <param name="userID"></param> /// <param name="messageType"></param> /// <param name="recordLimit"></param> /// <returns></returns> public static IEnumerable <LogToDisplay> GetLogs(int userID, LogMessageType messageType, int recordLimit) { using (var db = new TraxxPlayerContext()) { return(db.Logs.Where(log => log.UserID == userID && log.MessageType == (int)messageType).OrderByDescending(l => l.CreationDate).Take(recordLimit).Select(x => new LogToDisplay(x.UserID, (LogMessageType)x.MessageType, x.Source, x.Message)).ToList()); } }
/// <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> /// Checks wheter log exists in database /// </summary> /// <param name="logID"></param> /// <returns></returns> public static bool LogExist(int logID) { using (var db = new TraxxPlayerContext()) { return(db.Logs.Any(l => l.id == logID)); } }
/// <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> /// Checks wheter playlist exist in database /// </summary> /// <param name="playlistID"></param> /// <returns></returns> public static bool PlaylistExist(int playlistID) { using (var db = new TraxxPlayerContext()) { return(db.Playlists.Any(p => p.id == playlistID)); } }
/// <summary> /// Checks wheter track history exist in database /// </summary> /// <param name="trackHistoryID"></param> /// <returns></returns> public static bool TrackHistoryExist(int trackHistoryID) { using (var db = new TraxxPlayerContext()) { return(db.TracksHistory.Any(th => th.id == trackHistoryID)); } }
/// <summary> /// Checks whether like exists in database /// </summary> /// <param name="likeID"></param> /// <returns></returns> public static bool LikeExist(int likeID) { using (var db = new TraxxPlayerContext()) { return(db.Likes.Any(l => l.id == likeID)); } }
/// <summary> /// Checks wheter playlist track exist /// </summary> /// <param name="playlistTrackID"></param> /// <returns></returns> public static bool PlaylistTrackExist(int playlistTrackID) { using (var db = new TraxxPlayerContext()) { return db.PlaylistTracks.Any(pt => pt.id == playlistTrackID); } }
/// <summary> /// Gets users from database /// </summary> /// <returns></returns> public static IEnumerable <UserToDisplay> GetUsers() { using (var db = new TraxxPlayerContext()) { return(db.Users.Select(user => new UserToDisplay() { id = user.id, username = user.username, isDefault = user.isDefault }).ToList()); } }
/// <summary> /// Gets playlists for user from database /// </summary> /// <param name="userID"></param> /// <returns></returns> public static IEnumerable <PlaylistToDisplay> GetPlaylists(int userID) { using (var db = new TraxxPlayerContext()) { return(db.Playlists.Where(p => p.UserID == userID).Select(x => new PlaylistToDisplay() { id = x.id, CreationDate = x.CreationDate, Name = x.Name, UserID = x.UserID }).ToList()); } }
/// <summary> /// Gets single track history for user from database /// </summary> /// <param name="userID"></param> /// <param name="recordLimit"></param> /// <returns></returns> public static IEnumerable <TrackHistoryToDisplay> GetTracksHistory(int userID, int recordLimit) { using (var db = new TraxxPlayerContext()) { return(db.TracksHistory.Where(th => th.UserID == userID).OrderByDescending(t => t.CreationDate).Take(recordLimit).Select(x => new TrackHistoryToDisplay() { id = x.id, CreationDate = x.CreationDate, TrackID = x.TrackID, UserID = x.UserID }).ToList()); } }
/// <summary> /// Gets liked tracks for current user from database /// </summary> /// <param name="userID"></param> /// <returns></returns> public static IEnumerable <LikeToDisplay> GetLikes(int userID) { using (var db = new TraxxPlayerContext()) { return(db.Likes.Where(l => l.UserID == userID).Select(x => new LikeToDisplay() { id = x.id, CreationDate = x.CreationDate, TrackID = x.TrackID, UserID = x.UserID }).ToList()); } }
/// <summary> /// Gets playlist tracks from database /// </summary> /// <param name="playlistID"></param> /// <returns></returns> public static IEnumerable<PlaylistTrackToDisplay> GetPlaylistTracks(int playlistID) { using (var db = new TraxxPlayerContext()) { return db.PlaylistTracks.Where(pt => pt.PlaylistID == playlistID).Select(x => new PlaylistTrackToDisplay() { PlaylistID = x.PlaylistID, TrackID = x.TrackID, TrackOrder = x.TrackOrder, id = x.id }).ToList(); } }
/// <summary> /// Gets single log from database /// </summary> /// <param name="logID"></param> /// <returns></returns> public static LogToDisplay GetLog(int logID) { using (var db = new TraxxPlayerContext()) { var log = db.Logs.Where(l => l.id == logID).FirstOrDefault(); if (log != null) { return(new LogToDisplay(log.UserID, (LogMessageType)log.MessageType, log.Source, log.Message)); } else { throw new Exception("Log with the id provided does not exist. Get 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> /// 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> /// 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> /// 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> /// 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> /// 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 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> /// 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> /// 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> /// 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> /// Gets single user from database /// </summary> /// <param name="userName"></param> /// <returns></returns> public static UserToDisplay GetUser(string userName) { using (var db = new TraxxPlayerContext()) { var user = db.Users.Where(u => u.username == userName).FirstOrDefault(); if (user != null) { return(new UserToDisplay() { id = user.id, username = user.username, isDefault = user.isDefault }); } else { throw new Exception("User with the user name provided does not exist. Get user failed."); } } }