public static Song SongModelToEntity(SongModel model) { Song entity = new Song(); entity.Length = !string.IsNullOrEmpty(model.Length) ? model.Length : "(Unknown Length)"; entity.FilePath = !string.IsNullOrEmpty(model.FilePath) ? model.FilePath : "(Unknown Filepath)"; entity.Title = !string.IsNullOrEmpty(model.SongTitle) ? model.SongTitle : System.IO.Path.GetFileNameWithoutExtension(model.FilePath); entity.Genre = !string.IsNullOrEmpty(model.Genre) ? model.Genre : "(Unknown Genre)"; entity.Artist = !string.IsNullOrEmpty(model.Artist) ? model.Artist : "(Unknown Artist)"; entity.Album = !string.IsNullOrEmpty(model.Album) ? model.Album : "(Unknown Album)"; entity.Likes = model.Likes; return entity; }
/// <summary> /// Adds songs to the database /// </summary> /// <param name="model">SongModel of song being added</param> public void Add(SongModel model) { try { Account account; //Convert the SongModel into an entity to be pushed to the database. Song entity = ModelConversions.SongModelToEntity(model); //If there is an account that matches the given username, set account equal to it. if (_context.Accounts.Any(a => a.Username == model.Username)) { account = _context.Accounts.First(a => a.Username == model.Username); } //Else, create an Account with the username else { account = new Account() { Username = model.Username }; } //Add the account association to the song, and push the song to the database entity.Accounts.Add(account); _context.Songs.Add(entity); _context.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } }
/// <summary> /// Push edited song metadata to the database. /// </summary> /// <param name="song">SongModel with updated information.</param> public void EditSong(SongModel song) { //Get the song based on its songId Song entity = GetSong(song.SongID); //Edit the song information. entity.Title = song.SongTitle; entity.Artist = song.Artist; entity.Album = song.Album; //Push to the database. _context.SaveChanges(); }
/// <summary> /// Deletes a song asssociated with a user /// </summary> /// <param name="model">SongModel of Song being deleted</param> /// <param name="loginID">LoginId of user deleting song</param> public void Delete(SongModel model, int loginID) { //Get the account based on the loginId Account account = GetAccount(loginID); //Remove the song from the database. account.Songs.Remove(GetSong(model.SongID)); _context.SaveChanges(); }
/// <summary> /// Add song method for associating a current database song with a username /// </summary> /// <param name="model">SongModel of song being added</param> /// <param name="username">username of user adding song</param> public void AddSong(SongModel model, string username) { //Get the account for the given username Account account = _context.Accounts.Where(s => s.Username == username).Single(); //Find the song that matches the given songtitle and length Song song = _context.Songs.Where(s => s.Title == model.SongTitle && s.Length == model.Length).Single(); //Add the song association to the account and push to the database. account.Songs.Add(song); song.Accounts.Add(account); _context.SaveChanges(); }
/// <summary> /// Adds a new song to the database /// </summary> /// <param name="model">Song that is being added</param> /// <param name="username">User that is adding the song</param> public void AddSong(SongModel model, string username) { SongRepository.AddSong(model, username); }
/// <summary> /// Adds a song to the database /// </summary> /// <param name="model">SongModel of song you are adding</param> public void UploadSong(SongModel model) { SongRepository.Add(model); }
/// <summary> /// Increment Like count when user like's a song /// </summary> /// <param name="song">Song that is being liked</param> public void IncrementLike(SongModel song) { int songId = song.SongID; //song.IncrementLikes(); SongRepository.IncrementLike(songId); }
/// <summary> /// Edit song metadata and push to database /// </summary> /// <param name="song">SongModel with updated information.</param> public void EditSong(SongModel song) { SongRepository.EditSong(song); }
/// <summary> /// Deletes a song from database /// </summary> /// <param name="songmodel">Song to be deleted</param> /// <param name="username">Username who is deleting the song</param> public void Delete(SongModel songmodel, string username) { AccountModel accountmodel = GetAccountModel(username); int userId = accountmodel.LoginId; SongRepository.Delete(songmodel, userId); }