示例#1
0
        public LeilaoPaginacaoResponse(LeilaoPaginacao leilaoPaginacao, IUrlHelper urlHelper, UsuarioAutenticado usuarioAutenticado)
        {
            _urlHelper          = urlHelper;
            _usuarioAutenticado = usuarioAutenticado;

            Search      = leilaoPaginacao.Search;
            Pagina      = leilaoPaginacao.Pagina;
            Total       = leilaoPaginacao.Total;
            PorPagina   = leilaoPaginacao.PorPagina;
            Order       = leilaoPaginacao.Order;
            MeusLeiloes = !leilaoPaginacao.IsPublico;
            Resultados  = leilaoPaginacao.Resultados.Select(x =>
            {
                var leilao = new LeilaoResponse(x, urlHelper, usuarioAutenticado);
                leilao.AddAllLinks();
                return(leilao);
            }).ToList();
        }
        public async Task <IActionResult> GetPaginacaoAsync(int?pagina, int?porPagina, string order, string search, Boolean?meusLeiloes = false)
        {
            try
            {
                _logger.LogInformation("Inicio {0}", nameof(GetPaginacaoAsync));
                var usuarioAutenticado = new UsuarioAutenticado(User);

                var leilaoPaginacao = new LeilaoPaginacao(
                    porPagina: porPagina,
                    pagina: pagina,
                    order: order,
                    search: search
                    );

                if (meusLeiloes == true)
                {
                    if (usuarioAutenticado.IsAuthenticated == false)
                    {
                        return(Unauthorized());
                    }

                    leilaoPaginacao.LeiloadoPorId = usuarioAutenticado.Id;
                }


                var listas = await _leilaoService.GetAllAsync(leilaoPaginacao);

                var leilaoPaginacaoResponse = new LeilaoPaginacaoResponse(listas, _urlHelper, usuarioAutenticado);
                leilaoPaginacaoResponse.AddLinkPaginaAnterior();
                leilaoPaginacaoResponse.AddLinkProximaPagina();

                return(Ok(leilaoPaginacaoResponse));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Erro" + nameof(GetPaginacaoAsync));
                return(NotFound(ErrorResponse.From(e)));
            }
        }
        public async Task GetAllComSucesso()
        {
            //Arranje
            var faker             = new Faker("pt_BR");
            var leilaoRepository  = new LeilaoRepository(this.Context);
            var usuarioRepository = new UsuarioRepository(this.Context);
            var usuarioId         = await usuarioRepository.InsertAsync(new Usuario(faker.Name.FullName(), faker.Internet.Email()));

            var totalLeiloes = 20;

            for (int i = 0; i < totalLeiloes; i++)
            {
                var leilaoId = await leilaoRepository.InsertAsync(
                    new Leilao(usuarioId, faker.Internet.UserNameUnicode(), null, DateTime.UtcNow, DateTime.UtcNow.AddDays(50), 250.50)
                    );
            }

            //Act
            LeilaoPaginacao leiloes = await leilaoRepository.GetAllAsync(new LeilaoPaginacao(porPagina : 20, pagina : 1, leiloadoPorId : usuarioId));

            //Assert
            Assert.Equal(leiloes.Resultados.Count, totalLeiloes);
        }
        public async Task <LeilaoPaginacao> GetAllAsync(LeilaoPaginacao data)
        {
            string sql = String.Format(@"
                SELECT
                    COUNT(id)
                FROM leiloes
                WHERE 
                    (status = @Status OR @Status IS NULL) AND
                    (titulo ILIKE @SearchDb OR @SearchDb IS NULL) AND
                    ((datainicio >= now() OR datafim >= now()) OR @LeiloadoPorId IS NOT NULL) AND
                    (ispublico = @IsPublico OR @LeiloadoPorId IS NOT NULL) AND
                    (leiloadoporid = @LeiloadoPorId OR @LeiloadoPorId IS NULL);
                SELECT 
                    LE.*,
                    (SELECT COUNT(id) FROM lances WHERE leilaoid = LE.id) AS totallances,
	                (SELECT url FROM leilaoimagens WHERE leilaoid = LE.id LIMIT 1) AS caminhoimagem
                FROM leiloes AS LE 
                WHERE 
                    (LE.status = @Status OR @Status IS NULL) AND
                    (LE.titulo ILIKE @SearchDb OR @SearchDb IS NULL) AND
                    ((datainicio >= now() OR datafim >= now()) OR @LeiloadoPorId IS NOT NULL) AND
                    (ispublico = @IsPublico OR @LeiloadoPorId IS NOT NULL) AND
                    (leiloadoporid = @LeiloadoPorId OR @LeiloadoPorId IS NULL)
                ORDER BY {0}
                LIMIT @PorPagina 
                OFFSET(@Pagina - 1) * @PorPagina;
            ", data.Order);

            using (var result = await _dbConnection.QueryMultipleAsync(sql, data))
            {
                data.Total      = result.Read <int>().FirstOrDefault();
                data.Resultados = result.Read <Leilao>().ToList();
            }

            return(data);
        }
示例#5
0
 public async Task <LeilaoPaginacao> GetAllAsync(LeilaoPaginacao data)
 {
     _logger.LogInformation("Dados: " + JsonSerializer.Serialize(data));
     return(await _leilaoRepository.GetAllAsync(data));
 }