Ejemplo n.º 1
0
        public async Task GetTopUsersTest()
        {
            var url    = "/api/users/top-users";
            var client = ServerFactory.CreateHttpClient();

            var body = new TopUsersRequest
            {
                Count = 0,
            };
            var result = await client.PostAsync(url, body.GetContent());

            Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);

            body = new TopUsersRequest
            {
                Count = 100,
            };
            result = await client.PostAsync(url, body.GetContent());

            result.EnsureSuccessStatusCode();
            var json = await result.Content.ReadAsStringAsync();

            var deserialize = JsonConvert.DeserializeObject <IEnumerable <UserDTO> >(json);

            Assert.True(!deserialize.Any());
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <UserDTO> > GetTopUsers(TopUsersRequest request, CancellationToken token)
        {
            var result = await _context.Users.AsNoTracking()
                         .OrderByDescending(x => x.Raiting)
                         .Take(request.Count)
                         .ToArrayAsync(token);

            if (token.IsCancellationRequested)
            {
                return(Enumerable.Empty <UserDTO>());
            }

            return(result.Select(x => new UserDTO
            {
                Id = x.Id,
                Name = x.Name,
                Raiting = x.Raiting
            }));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> GetTopUsers(TopUsersRequest request, CancellationToken token)
        {
            var result = await _userService.GetTopUsers(request, token);

            return(Ok(result));
        }