public async Task GetPlaylists_GivenName_ReturnsPlaylistsByName() { const string dbName = "GetPlaylistWithName"; using (var context = Utils.GetDbContext(dbName)) { // arrange const int userId = 1; var user = new User { Id = userId }; context.Users.Add(user); context.Playlists.Add(new Domain.Playlist { Id = 1, User = user, Name = "Test" }); context.SaveChanges(); var query = new GetPlaylistsQuery { Name = "Test" }; // act var handler = GetQueryHandler(context); var playlists = await handler.Handle(query, CancellationToken.None); // assert Assert.AreEqual(1, playlists.Count()); Assert.AreEqual("Test", playlists.First().Name); } }
public async Task GetPlaylists_GivenNonExistentUserId_ReturnsEmptyList() { const string dbName = "GetPlaylistWithMissingId"; using (var context = Utils.GetDbContext(dbName)) { // arrange const int userId = 5; var user = new User { Id = userId }; context.Users.Add(user); context.Playlists.Add(new Domain.Playlist { Id = 1, User = new User { Id = 1 }, Name = "Test" }); context.SaveChanges(); var query = new GetPlaylistsQuery { UserId = userId }; // act var handler = GetQueryHandler(context); var playlists = await handler.Handle(query, CancellationToken.None); // assert Assert.AreEqual(0, playlists.Count()); } }
public async Task <IActionResult> GetPlaylists([FromQuery] int?userId, [FromQuery] string name, CancellationToken token) { var query = new GetPlaylistsQuery { UserId = userId, Name = name }; var playlists = await _mediator.Send(query, token); return(new JsonResult(Result.Ok(playlists))); }
public void GetPlaylistsQueryValidator_FilterSupplied_PassesValidation(string name, int?userId) { var validator = GetValidator(); var query = new GetPlaylistsQuery { Name = name, UserId = userId }; var res = validator.Validate(query); Assert.True(res.IsValid); }
public void GetPlaylistsQueryValidator_NoUserIdOrName_FailsValidation(string name, int?userId) { var validator = GetValidator(); var query = new GetPlaylistsQuery { Name = name, UserId = userId }; var res = validator.Validate(query); Assert.False(res.IsValid); }
public async Task <OperationResult <GetPlaylistsQuery, GetPlaylistsResponse> > GetPlaylists(string ownerId, int index) { var accessToken = Request.Headers["Authorization"]; var query = new GetPlaylistsQuery() { AccessToken = accessToken, OwnerId = ownerId, Index = index }; return(await getPlaylistsQueryHandler.HandleAsync(query)); }
public async Task <ListResult <PlaylistDto> > Handle(GetPlaylistsQuery request, CancellationToken cancellationToken) { var query = _context.Playlists.Include(x => x.Subreddits); ListResult <Playlist> playlists; if (!request.IgnoreGlobal) { playlists = await query.Where(playlist => playlist.IsGlobal || playlist.GuildId == request.GuildId).ToListResultAsync(); } else { playlists = await query.Where(playlist => playlist.GuildId == request.GuildId).ToListResultAsync(); } if (!playlists.Data.Any()) { return((await Result.FailAsync("No playlists found")) as ListResult <PlaylistDto>); } var dtos = _mapper.Map <ListResult <PlaylistDto> >(playlists); return(dtos); }