public void IsGetAllAudios(int count, int skip, int take, int actualCount) { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist playlist = context.CreatePlaylist(true, userId, null, 0); CreateAndAddAudio(context, playlist.Id, count); context.SaveChanges(); UserPlaylistsResult result = playListService.GetUserPlaylists(userId, playlist.Id, skip, take); CheckIfSuccess(result); Assert.Equal(playlist.Id, result.Playlists.FirstOrDefault().Id); Assert.Equal(playlist.Title, result.Playlists.FirstOrDefault().Title); Assert.Equal(actualCount, result.Playlists.FirstOrDefault().Audios.Count()); } }); }
public void ShouldDeletePlaylist() { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); //create general DataModel.Playlist generalPlaylist = context.CreatePlaylist(true, userId, null, 0); DataModel.Playlist playlist = context.CreatePlaylist(false, userId, "My playlist", 0); CreateAndAddAudio(context, playlist.Id, 10); context.SaveChanges(); Assert.Equal(context.AudioPlaylists.Count(), 10); var result = playListService.DeletePlaylist(userId, playlist.Id); CheckIfSuccess(result); Assert.Equal(context.AudioPlaylists.Count(), 0); } }); }
public void ShouldAddToPlaylist() { SqlLiteDatabaseTest(options => { var audioService = _server.Host.Services.GetService(typeof(IAudioService)) as IAudioService; using (var context = new PlayCatDbContext(options)) { audioService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist generalPlaylist = context.CreatePlaylist(true, userId, null, 0); CreateAndAddAudio(context, generalPlaylist.Id, 10); DataModel.Playlist playlist = context.CreatePlaylist(false, userId, "Rock", 0); context.SaveChanges(); var audio = context.Audios.FirstOrDefault(); var result = audioService.AddToPlaylist(userId, new AddRemovePlaylistRequest() { AudioId = audio.Id, PlaylistId = playlist.Id, }); var audioPlaylists = context.AudioPlaylists.ToList(); var addedAP = audioPlaylists.FirstOrDefault(x => x.PlaylistId == playlist.Id); CheckIfSuccess(result); Assert.Equal(audioPlaylists.Count, 11); Assert.NotNull(addedAP); Assert.Equal(addedAP.AudioId, audio.Id); } }); }
public void ShouldRemoveIfAudioNotExists() { SqlLiteDatabaseTest(options => { var audioService = _server.Host.Services.GetService(typeof(IAudioService)) as IAudioService; using (var context = new PlayCatDbContext(options)) { audioService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist generalPlaylist = context.CreatePlaylist(true, userId, null, 0); DataModel.Playlist playlist = context.CreatePlaylist(false, userId, "Rock", 0); context.SaveChanges(); var result = audioService.RemoveFromPlaylist(userId, new AddRemovePlaylistRequest() { AudioId = Guid.Empty, PlaylistId = playlist.Id, }); CheckIfSuccess(result); } }); }
public void IsDescendingOrder() { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist playlist = context.CreatePlaylist(true, userId, null, 0); CreateAndAddAudio(context, playlist.Id, 5); context.SaveChanges(); UserPlaylistsResult result = playListService.GetUserPlaylists(userId, playlist.Id, 0, 5); CheckIfSuccess(result); Assert.Equal(playlist.Id, result.Playlists.FirstOrDefault().Id); Assert.Equal(playlist.Title, result.Playlists.FirstOrDefault().Title); Assert.Equal(1, result.Playlists.Count()); Assert.Equal(5, result.Playlists.FirstOrDefault().Audios.Count()); for (int i = 0; i < 4; i++) { Assert.True(result.Playlists.FirstOrDefault().Audios.ElementAt(i).DateAdded > result.Playlists.FirstOrDefault().Audios.ElementAt(i + 1).DateAdded); } } }); }
public void ShouldReturnNotGeneralPlaylist() { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist playlist = context.CreatePlaylist(false, userId, "My playlist", 0); CreateAndAddAudio(context, playlist.Id, 10); context.SaveChanges(); UserPlaylistsResult result = playListService.GetUserPlaylists(userId, playlist.Id, 0, 10); CheckIfSuccess(result); Assert.Equal(playlist.Id, result.Playlists.FirstOrDefault().Id); Assert.Equal(playlist.Title, result.Playlists.FirstOrDefault().Title); Assert.Equal(10, result.Playlists.FirstOrDefault().Audios.Count()); } }); }
public void ShouldCreatePlaylist() { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); //create general DataModel.Playlist playlist = context.CreatePlaylist(true, userId, null, 0); context.SaveChanges(); string playlistTitle = "Test"; var result = playListService.CreatePlaylist(userId, new CreatePlaylistRequest() { Title = playlistTitle, }); CheckIfSuccess(result); var playlists = context.Playlists.ToList(); Assert.True(playlists.Any(x => x.Title == playlistTitle && !x.IsGeneral)); } }); }
public void ShouldReturnPlaylists() { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); //create general DataModel.Playlist playlist = context.CreatePlaylist(true, userId, null, 0); DataModel.Playlist playlist1 = context.CreatePlaylist(false, userId, "playlist1", 0); DataModel.Playlist playlist2 = context.CreatePlaylist(false, userId, "playlist2", 0); DataModel.Playlist playlist3 = context.CreatePlaylist(false, userId, "playlist3", 0); context.SaveChanges(); UserPlaylistsResult result = playListService.GetUserPlaylists(userId, null, 0, 10); CheckIfSuccess(result); Assert.Equal(4, result.Playlists.Count()); Assert.True(result.Playlists.All(x => !x.Audios.Any())); } }); }
public void ShouldFailCreatePlaylist() { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); //create general DataModel.Playlist playlist = context.CreatePlaylist(true, userId, null, 0); context.SaveChanges(); var result = playListService.CreatePlaylist(userId, new CreatePlaylistRequest() { Title = "13", }); CheckIfFail(result); } }); }
public void ShouldFailUpdateOnWrongPlaylistId() { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); //create general DataModel.Playlist playlist = context.CreatePlaylist(true, userId, null, 0); string newTitle = "New title"; var updatePlaylist = context.CreatePlaylist(false, userId, "Top", 0); context.SaveChanges(); var result = playListService.UpdatePlaylist(userId, new UpdatePlaylistRequest() { PlaylistId = Guid.Empty, Title = newTitle, }); CheckIfFail(result); Assert.Equal("Playlist not found", result.Info); } }); }
public void ShouldFailAddToPlaylistNotOwner() { SqlLiteDatabaseTest(options => { var audioService = _server.Host.Services.GetService(typeof(IAudioService)) as IAudioService; using (var context = new PlayCatDbContext(options)) { audioService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist generalPlaylist = context.CreatePlaylist(true, userId, null, 0); DataModel.Playlist playlist = context.CreatePlaylist(false, userId, "Rock", 0); CreateAndAddAudio(context, playlist.Id, 1); context.SaveChanges(); var audio = context.Audios.FirstOrDefault(); var result = audioService.AddToPlaylist(Guid.Empty, new AddRemovePlaylistRequest() { AudioId = audio.Id, PlaylistId = playlist.Id, }); CheckIfFail(result); Assert.True(result.Info.Any()); } }); }
public void ShouldTrueRemoveFromGeneral() { SqlLiteDatabaseTest(options => { var audioService = _server.Host.Services.GetService(typeof(IAudioService)) as IAudioService; using (var context = new PlayCatDbContext(options)) { audioService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist generalPlaylist = context.CreatePlaylist(true, userId, null, 0); CreateAndAddAudio(context, generalPlaylist.Id, 1); context.SaveChanges(); var audio = context.Audios.FirstOrDefault(); var result = audioService.RemoveFromPlaylist(userId, new AddRemovePlaylistRequest() { AudioId = audio.Id, PlaylistId = generalPlaylist.Id, }); CheckIfSuccess(result); Assert.Equal(context.AudioPlaylists.Count(), 0); } }); }
public void ShouldReturnGeneralPlaylist() { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist playlist = context.CreatePlaylist(true, userId, null, 0); DataModel.Playlist playlist2 = context.CreatePlaylist(false, userId, "Rock", 0); DataModel.Playlist playlist3 = context.CreatePlaylist(false, userId, "RnB", 0); CreateAndAddAudio(context, playlist3.Id, 10); context.SaveChanges(); UserPlaylistsResult result = playListService.GetUserPlaylists(userId, playlist3.Id, 0, 10); CheckIfSuccess(result); Assert.True(result.Playlists.Where(x => x.Id != playlist3.Id).All(x => !x.Audios.Any())); Assert.Equal(10, result.Playlists.FirstOrDefault(x => x.Id == playlist3.Id).Audios.Count()); } }); }
public static ApiModel.Playlist FromData(DataModel.Playlist playlist) { return(playlist == null ? null : new ApiModel.Playlist() { Id = playlist.Id, IsGeneral = playlist.IsGeneral, Title = playlist.Title, Owner = UserMapper.ToApi.FromData(playlist.Owner), }); }
private new Guid GetUserId(PlayCatDbContext context) { var inviteService = _server.Host.Services.GetService(typeof(IInviteService)) as IInviteService; string password = "******"; string email = "*****@*****.**"; DataModel.User user = context.CreateUser(email, "test", "test", "m", password, inviteService.GenerateInvite()); DataModel.Playlist playlist = context.CreatePlaylist(true, user.Id, "General", 0); DataModel.AuthToken authToken = context.CreateToken(DateTime.Now.AddDays(-1), false, user.Id); context.SaveChanges(); return(user.Id); }
public PlaylistResult UpdatePlaylist(Guid userId, UpdatePlaylistRequest request) { return(BaseInvokeCheckModel(request, () => { DataModel.Playlist playlist = _dbContext.Playlists.FirstOrDefault(x => x.Id == request.PlaylistId && x.OwnerId == userId); if (playlist == null) { return ResponseBuilder <PlaylistResult> .Fail().SetInfoAndBuild(PlaylistNotFound); } playlist.Title = request.Title; _dbContext.SaveChanges(); return ResponseBuilder <PlaylistResult> .SuccessBuild(new PlaylistResult() { Playlist = PlaylistMapper.ToApi.FromData(playlist), }); })); }
public PlaylistResult CreatePlaylist(Guid userId, CreatePlaylistRequest request) { return(BaseInvokeCheckModel(request, () => { var playlist = new DataModel.Playlist() { Id = Guid.NewGuid(), IsGeneral = false, OrderValue = 0, OwnerId = userId, Title = request.Title, }; _dbContext.Playlists.Add(playlist); _dbContext.SaveChanges(); return ResponseBuilder <PlaylistResult> .SuccessBuild(new PlaylistResult() { Playlist = PlaylistMapper.ToApi.FromData(playlist), }); })); }
public void ShouldEmptyOnWrongId() { SqlLiteDatabaseTest(options => { var audioService = _server.Host.Services.GetService(typeof(IAudioService)) as IAudioService; using (var context = new PlayCatDbContext(options)) { audioService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist playlist = context.CreatePlaylist(true, userId, null, 0); CreateAndAddAudio(context, playlist.Id, 10); context.SaveChanges(); AudioResult result = audioService.GetAudios(Guid.Empty, 0, 10); CheckIfSuccess(result); Assert.Equal(0, result.Audios.Count()); } }); }
public void ShouldDeleteFailOnRemoveGeneral() { SqlLiteDatabaseTest(options => { var playListService = _server.Host.Services.GetService(typeof(IPlaylistService)) as IPlaylistService; using (var context = new PlayCatDbContext(options)) { playListService.SetDbContext(context); Guid userId = GetUserId(context); //create general DataModel.Playlist generalPlaylist = context.CreatePlaylist(true, userId, null, 0); context.SaveChanges(); var result = playListService.DeletePlaylist(userId, generalPlaylist.Id); CheckIfFail(result); Assert.True(result.Info.Any()); } }); }
public void ShouldReturnAudios(int count, int skip, int take, int actual) { SqlLiteDatabaseTest(options => { var audioService = _server.Host.Services.GetService(typeof(IAudioService)) as IAudioService; using (var context = new PlayCatDbContext(options)) { audioService.SetDbContext(context); Guid userId = GetUserId(context); DataModel.Playlist playlist = context.CreatePlaylist(true, userId, null, 0); CreateAndAddAudio(context, playlist.Id, count); context.SaveChanges(); AudioResult result = audioService.GetAudios(playlist.Id, skip, take); CheckIfSuccess(result); Assert.Equal(actual, result.Audios.Count()); } }); }
public SignUpInResult SignUp(SignUpRequest request) { return(BaseInvokeCheckModel(request, () => { var responseBuilder = ResponseBuilder <SignUpInResult> .Fail(); if (!_inviteService.IsInviteValid(request.VerificationCode)) { return responseBuilder.SetInfoAndBuild("Verification code is wrong"); } if (_dbContext.Users.Any(x => x.Email == request.Email)) { return responseBuilder.SetInfoAndBuild("User with this email already registered"); } if (_dbContext.Users.Any(x => x.VerificationCode == request.VerificationCode)) { return responseBuilder.SetInfoAndBuild("This invite already used"); } string salt = Crypto.GenerateSalt(); string passwordHah = Crypto.HashPassword(request.Password + salt); var dataUser = UserMapper.ToData.FromRequest(request, (user) => { user.Id = Guid.NewGuid(); user.IsUploadingAudio = false; user.PasswordHash = passwordHah; user.PasswordSalt = salt; user.RegisterDate = DateTime.Now; }); var dataAuthToken = new DataModel.AuthToken() { Id = Guid.NewGuid(), DateExpired = DateTime.Now.AddDays(AuthTokenDaysExpired), UserId = dataUser.Id, IsActive = true, }; var playlist = new DataModel.Playlist() { Id = Guid.NewGuid(), IsGeneral = true, OwnerId = dataUser.Id, OrderValue = 0, }; _dbContext.AuthTokens.Add(dataAuthToken); _dbContext.Users.Add(dataUser); _dbContext.Playlists.Add(playlist); _dbContext.SaveChanges(); return ResponseBuilder <SignUpInResult> .SuccessBuild(new SignUpInResult() { User = UserMapper.ToApi.FromData(dataUser), AuthToken = AuthTokenMapper.ToApi.FromData(dataAuthToken), }); })); }