Ejemplo n.º 1
0
        public async Task <Room> AddSpotifyAuthCodeToRoomAsync(string roomId, string spotifyAuthCode)
        {
            try
            {
                var room = await _playlistsContext.Rooms
                           .SingleOrDefaultAsync(s => s.Id == Convert.ToInt32(roomId));

                if (room == null)
                {
                    throw new Exception("Room not found");
                }

                if (room.SpotifyPlaylist == null)
                {
                    var service = new SpotifyService(spotifyAuthCode);
                    var ownerId = await service.GetUserIdAsync();

                    var playlist = await service.CreatePlaylist(room.Name, ownerId, "");

                    room.SpotifyPlaylist  = (SpotifyPlaylist)playlist;
                    room.IsSpotifyEnabled = true;
                    await _playlistsContext.SaveChangesAsync();
                }

                return(room);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task RemovePreviouslyPlayedSongsAsync(int currentRoomId)
        {
            var room = await _playlistsContext.Rooms.SingleOrDefaultAsync(s => s.Id == currentRoomId);

            var spotify = new SpotifyService(room.SpotifyAuthCode);
            var currentlyPlayingSong = await spotify.GetCurrentlyPlayingSongAsync();

            void RemoveSongs()
            {
                if (currentlyPlayingSong != null)
                {
                    if ((bool)!currentlyPlayingSong?.IsPlaying && (bool)room.RoomSongs.Single(s => s.Song.ServiceId == currentlyPlayingSong?.Item?.Uri)?.PreviouslyPlayed)
                    {
                        room.RoomSongs.RemoveAll(s => s.PreviouslyPlayed);
                    }
                    room.RoomSongs.RemoveAll(s => s.PreviouslyPlayed && s.Song.ServiceId != currentlyPlayingSong?.Item?.Uri);
                }
            }

            await UpdatePreviouslyPlayedSongs(currentRoomId);

            RemoveSongs();

            await _playlistsContext.SaveChangesAsync();
        }
Ejemplo n.º 3
0
        public async Task <Room> AddSongToRoomAsync(string userToken, string roomId, Song song)
        {
            try
            {
                var roomIdAsInt = Convert.ToInt32(roomId);
                var room        = await GetItemAsync(roomId);

                var playlist     = _playlistsContext.SpotifyPlaylist.SingleOrDefaultAsync(s => s.RoomId == roomIdAsInt);
                var matchingSong = _playlistsContext.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)
                {
                    throw new Exception("Could not find room");
                }

                var roomSong = new RoomSong()
                {
                    RoomId      = roomIdAsInt,
                    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 _playlistsContext.SaveChangesAsync();
                }

                return(room);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 4
0
        public async Task UpdatePreviouslyPlayedSongs(int currentRoomId)
        {
            var room = await _playlistsContext
                       .Rooms
                       .Include(s => s.RoomSongs)
                       .ThenInclude(s => s.Song)
                       .SingleOrDefaultAsync(s => s.Id == currentRoomId);

            var spotify = new SpotifyService(room.SpotifyAuthCode);
            var currentlyPlayingSong = await spotify.GetCurrentlyPlayingSongAsync();

            var roomSong = room.RoomSongs.SingleOrDefault(s => s.Song.ServiceId == currentlyPlayingSong?.Item?.Uri);

            if (roomSong != null && !roomSong.PreviouslyPlayed)
            {
                roomSong.PreviouslyPlayed = true;
                _playlistsContext.RoomSongs.Update(roomSong);
            }
        }