public async Task <IActionResult> Edit(string privateSongIdString, [FromBody] PrivateSongBM privateSongModel) { //validate the new info if (!ModelState.IsValid) { return(BadRequest()); } //validate provided private song id as a valid GUID id if (privateSongIdString == null || !Guid.TryParse(privateSongIdString, out Guid privateSongId)) { return(NotFound()); } MyUser user = _context.Users.Include(x => x.PrivateSongs).ThenInclude(x => x.Song).ThenInclude(s => s.Video).FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { return(NotFound()); } //try to find private song in authenticated user private songs //(do not search outside current user, as any user is only allowed to change his own songs) PrivateSong privateSong = user.PrivateSongs.FirstOrDefault(x => x.PrivateSongId == privateSongId); if (privateSong == null) { return(NotFound()); } //Edit every field, every field must be provided even if unchanged privateSong.ArtistName = privateSongModel.ArtistName; privateSong.AlbumName = privateSongModel.AlbumName; privateSong.Name = privateSongModel.Name; privateSong.Song.Video.VideoUrl = privateSongModel.VideoUrl; privateSong.Song.Video.StartSec = Video.GetTimeInSeconds(privateSongModel.StartAt); privateSong.Song.Video.EndSec = Video.GetTimeInSeconds(privateSongModel.EndAt); privateSong.Song.Video.Duration = Video.GetDuration(privateSong.Song.Video.StartSec, privateSong.Song.Video.EndSec); if (privateSong.Song.Video.Duration == null) { return(BadRequest()); //invalid video duration } await _context.SaveChangesAsync(); PrivateSongBasicVM privateSongVM = new PrivateSongBasicVM(privateSong); return(Ok(privateSongVM)); }
public async void Edit_Valid() { //ARRANGE: authenticate user MyUser user = fixture.Authenticate_User("Anabela"); //ARRANGE: Get a valid and existing privateSongId string privateSongId = MySqlHelpers.GetRandomUserPrivateSongId(user.UserName); //ARRANGE: Set a privateSong model with the new info for the song PrivateSongBM model = ValidModelForEdit; //ACT: string responseContentString = await Edit_Act(privateSongId, model, HttpStatusCode.OK); //ASSERT: Correct response object PrivateSongBasicVM responseObject = JsonConvert.DeserializeObject <PrivateSongBasicVM>(responseContentString); Assert.Equal(model.Name, responseObject.Name); Assert.Equal(privateSongId, responseObject.PrivateSongId); Assert.True(Guid.TryParse(responseObject.SongId, out Guid auxSongId)); //ASSERT: Correct database state Song expected = new Song(); expected.SongId = auxSongId; expected.PrivateSong = new PrivateSong(); expected.PrivateSong.PrivateSongId = Guid.Parse(privateSongId); expected.PrivateSong.Name = model.Name; expected.PrivateSong.ArtistName = model.ArtistName; expected.PrivateSong.AlbumName = model.AlbumName; expected.PrivateSong.MyUser = user; expected.Video = new Video(); expected.Video.VideoUrl = model.VideoUrl; expected.Video.StartSec = Video.GetTimeInSeconds(model.StartAt); expected.Video.EndSec = Video.GetTimeInSeconds(model.EndAt); expected.Video.Duration = Video.GetDuration(expected.Video.StartSec, expected.Video.EndSec); Assert.True(MySqlHelpers.CheckIfPrivateSongWasCreated(expected, true)); }
public async void Delete_ValidId() { //ARRANGE: authenticate user MyUser user = fixture.Authenticate_User("Anabela"); //ARRANGE: Get a valid and existing privateSongId string privateSongId = MySqlHelpers.GetRandomUserPrivateSongId(user.UserName); //ACT: string responseContentString = await Delete_Act(privateSongId, HttpStatusCode.OK); //ASSERT: Correct Response Object PrivateSongBasicVM responseObject = JsonConvert.DeserializeObject <PrivateSongBasicVM>(responseContentString); Assert.Null(responseObject.Name); Assert.Equal(privateSongId, responseObject.PrivateSongId); Assert.True(Guid.TryParse(responseObject.SongId, out Guid auxSongId)); //ASSERT: Correct Database State Assert.True(MySqlHelpers.CheckIfPrivateSongWasDeleted(privateSongId)); }
public async Task <IActionResult> Delete(string privateSongIdString) { //validate provided private song id as a valid GUID id if (privateSongIdString == null || !Guid.TryParse(privateSongIdString, out Guid privateSongId)) { return(NotFound()); } MyUser user = _context.Users.Include(x => x.PrivateSongs).ThenInclude(x => x.Song).ThenInclude(s => s.Video).FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { return(NotFound()); } PrivateSong privateSong = user.PrivateSongs.FirstOrDefault(x => x.PrivateSongId == privateSongId); if (privateSong == null) { return(NotFound()); } //delete all playableSongs with such song first! //(song and video will cascade, but playablesong restricts, to avoid cycles when deleting an user // as user playlists and user privates songs cascade, and both would cause their playable songs to cascade) var toDelete = _context.PlayableSongs.Where(x => x.SongId == privateSong.Song.SongId); _context.PlayableSongs.RemoveRange(toDelete); //remove privateSong and save changes: user.PrivateSongs.Remove(privateSong); await _context.SaveChangesAsync(); PrivateSongBasicVM privateSongVM = new PrivateSongBasicVM(privateSong); //client needs ids, set name to null so client knows the private song was deleted: privateSongVM.Name = null; return(Ok(privateSongVM)); }
public async void Create_ValidModel(PrivateSongBM model) { //ARRANGE: authenticate user MyUser user = fixture.Authenticate_User("Miguel"); //ACT string responseContentString = await Create_Act(model, HttpStatusCode.OK); //ASSERT: Correct Response Object PrivateSongBasicVM responseObject = JsonConvert.DeserializeObject <PrivateSongBasicVM>(responseContentString); Assert.Equal(model.Name, responseObject.Name); Assert.True(Guid.TryParse(responseObject.PrivateSongId, out Guid auxPrivateSongId)); Assert.True(Guid.TryParse(responseObject.SongId, out Guid auxSongId)); //ASSERT: Correct Database State Song expected = new Song(); expected.SongId = auxSongId; expected.PrivateSong = new PrivateSong(); expected.PrivateSong.PrivateSongId = auxPrivateSongId; expected.PrivateSong.Name = model.Name; expected.PrivateSong.ArtistName = model.ArtistName; expected.PrivateSong.AlbumName = model.AlbumName; expected.PrivateSong.MyUser = user; expected.Video = new Video(); expected.Video.VideoUrl = model.VideoUrl; expected.Video.StartSec = Video.GetTimeInSeconds(model.StartAt); expected.Video.EndSec = Video.GetTimeInSeconds(model.EndAt); expected.Video.Duration = Video.GetDuration(expected.Video.StartSec, expected.Video.EndSec); Assert.True(MySqlHelpers.CheckIfPrivateSongWasCreated(expected)); }
public async Task <IActionResult> Create([FromBody] PrivateSongBM privateSongModel) { //automatically validate all input with model binding if (!ModelState.IsValid) { return(BadRequest()); } //get authenticated user MyUser user = _context.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { return(NotFound()); } _context.Entry(user).Reference(x => x.PrivateSongPlaylist).Load(); // Create private song object: PrivateSong pvs = new PrivateSong(); pvs.PrivateSongId = Guid.NewGuid(); pvs.MyUser = user; pvs.ArtistName = privateSongModel.ArtistName; pvs.AlbumName = privateSongModel.AlbumName; pvs.Name = privateSongModel.Name; Video v = new Video(); v.VideoId = Guid.NewGuid(); v.VideoUrl = privateSongModel.VideoUrl; v.IsLive = false; //not implemented yet, default to false v.StartSec = Video.GetTimeInSeconds(privateSongModel.StartAt); v.EndSec = Video.GetTimeInSeconds(privateSongModel.EndAt); v.Duration = Video.GetDuration(v.StartSec, v.EndSec); if (v.Duration == null) { return(BadRequest()); //invalid video duration } Song song = new Song(); song.SongId = Guid.NewGuid(); song.PrivateSong = pvs; song.PrivateSongId = pvs.PrivateSongId; v.Song = song; song.Video = v; //Add private song to private song's playlist: Playable p = _context.Playables.FirstOrDefault(y => y.PlaylistId == user.PrivateSongPlaylist.PlaylistId); PlayableSong ps = new PlayableSong(); ps.PlayableId = p.PlayableId; ps.Playable = p; ps.SongId = song.SongId; ps.Song = song; ps.Position = _context.Entry(p).Collection(x => x.PlayableSongs).Query().Count(); //Add to data context and save: _context.Songs.Add(song); _context.Videos.Add(v); _context.PrivateSongs.Add(pvs); _context.PlayableSongs.Add(ps); await _context.SaveChangesAsync(); PrivateSongBasicVM privateSongVM = new PrivateSongBasicVM(pvs); return(Ok(privateSongVM)); }