Ejemplo n.º 1
0
        public async Task <ActionResult <Room> > AddSongToRoom(string userToken, int roomId, [FromBody] Song song)
        {
            try
            {
                var room = await _context.Rooms
                           .Include(e => e.RoomSongs)
                           .SingleOrDefaultAsync(s => s.Id == roomId);

                var playlist     = _context.SpotifyPlaylist.SingleOrDefaultAsync(s => s.RoomId == roomId);
                var matchingSong = _context.Songs.FirstOrDefaultAsync(s => $"{s.Artist}{s.Name}" == $"{song.Artist}{song.Name}");
                var decodedToken = JWT.Decode(userToken);
                var token        = JsonConvert.DeserializeAnonymousType(decodedToken, new { Name = "" });

                if (room == null)
                {
                    return(NotFound());
                }

                var roomSong = new RoomSong()
                {
                    RoomId      = roomId,
                    SongId      = (await matchingSong)?.Id ?? 0,
                    Song        = song,
                    SongAddedBy = token.Name,
                };
                if (await playlist != null)
                {
                    var playlistTable = await playlist;
                    var service       = new SpotifyService(playlistTable.AuthCode);
                    if (string.IsNullOrEmpty(song.SpotifyId))
                    {
                        var spotifySong = await service.GetSong(song.Name);

                        await service.AddSongToPlaylist(playlistTable, spotifySong);

                        song.SpotifyId = spotifySong.SpotifyId;
                    }
                    else
                    {
                        await service.AddSongToPlaylist(playlistTable, song);
                    }
                }
                if (!room.RoomSongs.Any(s => s.SongId == roomSong.SongId))
                {
                    room.RoomSongs.Add(roomSong);
                    await _context.SaveChangesAsync();
                }

                return(room);
            }
            catch
            {
                throw new SystemWeb.HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
        public async Task <IActionResult> AddSongToRoom(RoomVM roomVM)
        {
            var spotify = new SpotifyService(roomVM.CurrentRoom.SpotifyAuthCode);
            var song    = await spotify.GetSong(roomVM.SongToAdd);

            var  token    = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var  username = _tokenService.GetNameFromToken(token);
            Room room     = null;

            try
            {
                room = await _roomDataStore.AddSongToRoomAsync(username, roomVM.CurrentRoom.Id.ToString(), song);

                var playlist = await _spotifyPlaylistsStore.GetItemByRoomId(room.Id.ToString());

                await spotify.AddSongToPlaylist(playlist, song);

                await _roomDataStore.RemovePreviouslyPlayedSongsAsync(room.Id);
            }
            catch { }


            if (room != null)
            {
                await _roomHub.Clients.All.SendAsync("Update", room.Id.ToString());

                return(PartialView("Components/_roomSongListItem", room.RoomSongs));
            }

            return(PartialView("Components/_roomSongListItem", room.RoomSongs));
        }