public void TestInit()
        {
            DbContextOptionsBuilder <PlaylistContext> dbContextOptionsBuilder = new DbContextOptionsBuilder <PlaylistContext>()
                                                                                .UseInMemoryDatabase("RoomSong");

            Room mockRoom = new Room()
            {
                Id    = 1,
                Owner = "Owner",
            };
            Song mockSong = new Song()
            {
                Id     = 1,
                Name   = "Song1",
                Artist = "Artist1",
            };

            _mockRoomSong = new RoomSong()
            {
                Id     = 1,
                RoomId = 1,
                Room   = mockRoom,
                Song   = mockSong,
                SongId = 1,
            };

            _playlistContext = new PlaylistContext(dbContextOptionsBuilder.Options);
            _playlistContext.Database.EnsureDeleted();
            _playlistContext.Rooms.Add(mockRoom);
            _playlistContext.RoomSongs.Add(_mockRoomSong);
            _playlistContext.Songs.Add(mockSong);
            _playlistContext.SaveChanges();
            _roomSongDataStore = new RoomSongDataStore(_playlistContext);
        }
        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;
            }
        }
Esempio n. 3
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 GetItemAsync_GivenRoomSongId_ReturnRoomSong()
        {
            int      checkedValue = 2;
            RoomSong mockRoomSong = new RoomSong()
            {
                Id = checkedValue
            };

            Assert.IsNotNull(await _roomSongDataStore.GetItemAsync(checkedValue.ToString()));
            Assert.AreEqual(mockRoomSong, await _roomSongDataStore.GetItemAsync(checkedValue.ToString()));
        }
Esempio n. 5
0
        public async Task <Room> AddSongToRoomAsync(string usernameAddingSong, string roomId, Song song)
        {
            var songDataStore = new SongDataStore(_playlistsContext);

            try
            {
                var roomIdAsInt = Convert.ToInt32(roomId);
                var room        = await GetItemAsync(roomId);

                var matchingSong = await songDataStore.GetItemByServiceId(song.ServiceId);

                if (room == null)
                {
                    throw new Exception("Could not find room");
                }

                var roomSong = new RoomSong()
                {
                    RoomId      = roomIdAsInt,
                    SongId      = (matchingSong)?.Id ?? 0,
                    Song        = matchingSong ?? song,
                    SongAddedBy = usernameAddingSong,
                };

                if (!room.RoomSongs.Any(s => s.SongId == roomSong.SongId))
                {
                    room.RoomSongs.Add(roomSong);
                    await _playlistsContext.SaveChangesAsync();
                }

                return(room);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Esempio n. 6
0
        public async Task ReorderPlaylist(IPlaylist playlist, Room room, RoomSong song)
        {
            CurrentPlaybackContext currentPlayback = null;

            try
            {
                var            http   = new HttpClient();
                IConfiguration config = new ConfigurationBuilder()
                                        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                        .AddEnvironmentVariables()
                                        .Build();

                var player = new PlayerApi(http, AuthToken);
                currentPlayback = await player.GetCurrentPlaybackInfo(AuthToken);
            }
            catch (Exception ex)
            {
            }

            try
            {
                var client = new RestClient(@"https://api.spotify.com/v1");
                client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator($"Bearer {AuthToken}");
                var request = new RestRequest($@"playlists/{playlist.PlaylistID}/tracks", Method.PUT);
                request.RequestFormat = DataFormat.Json;
                if (song != null)
                {
                    room.RoomSongs.SingleOrDefault(s => s.SongId == song.SongId).SongRating -= 1;
                }
                var spotifyUris = room.RoomSongs
                                  .OrderByDescending(s => s.SongRating)
                                  .Select(s => s.Song.SpotifyId)
                                  .ToList();
                if (currentPlayback != null && spotifyUris.Any(s => s == currentPlayback.Item.Uri))
                {
                    // Reorder songs, only after the current one we are playing.
                    // This ensures that if you're in the middle of a playlist,
                    // the next song you listen to is the highest rated song
                    for (int i = 0; i < spotifyUris.Count; ++i)
                    {
                        if (spotifyUris[0] == currentPlayback.Item.Uri)
                        {
                            break;
                        }
                        spotifyUris.Remove(spotifyUris[0]);
                    }
                }
                else
                {
                    spotifyUris = room.RoomSongs
                                  .OrderByDescending(s => s.SongRating)
                                  .Select(s => s.Song.SpotifyId)
                                  .ToList();
                }

                request.AddJsonBody(new { uris = spotifyUris });

                var response = await client.ExecuteTaskAsync(request);

                var content = response.Content;
            }
            catch (Exception ex)
            {
            }
        }