Пример #1
0
        public async Task <Response <PagedList <Dog> > > GetList(DogListQueryFilter model)
        {
            model.PageNumber = pagination.DefaultPageNumber;
            model.PageSize   = pagination.DefaultPageSize;

            var dogsInfo = await dogRepository.GetList(model);

            var serviceResponse = new Response <PagedList <Dog> >(
                true,
                "",
                PagedList <Dog> .Create(dogsInfo.Item2, (int)model.PageNumber, (int)model.PageSize, dogsInfo.Item1)
                );

            return(serviceResponse);
        }
        public async Task<IActionResult> GetAll([FromQuery] DogListQueryFilter model)
        {
            var response = await dogService.GetList(model);

            var apiResponse = new ApiResponse<List<DogDto>>(
                response.IsDone,
                response.Message,
                mapper.Map<List<Dog>, List<DogDto>>(response.Data),
                new PaginationMetadata()
                {
                    CurrentPage = response.Data.CurrentPage,
                    PageSize = response.Data.PageSize,
                    TotalCount = response.Data.TotalCount,
                    TotalPages = response.Data.TotalPages,
                    HasNextPage = response.Data.HasNextPage,
                    HasPreviousPage = response.Data.HasPreviousPage,
                    NextPageNumber = response.Data.NextPageNumber,
                    PreviousPageNumber = response.Data.PreviousPageNumber
                }
            );

            return Ok(apiResponse);
        }
Пример #3
0
        public async Task <Tuple <int, List <Dog> > > GetList(DogListQueryFilter model)
        {
            List <Dog> dogs        = null;
            string     order       = "";
            string     limit       = "";
            string     condition   = "";
            string     fromCommand = "";

            if (model.Random > 0)
            {
                order = "order by rand()";
                limit = $"limit {model.Random}";
            }
            else
            {
                condition += !string.IsNullOrEmpty(model.Name) ? $" and dogs.name like '%{model.Name.Replace(' ', '%')}%'" : "";
                condition += model.RaceId != 0 ? $" and raceId = {model.RaceId}" : "";
                condition += model.SizeId != 0 ? $" and sizeId = {model.SizeId}" : "";
                condition  = condition != "" ? $"where {condition.Substring(4)}" : "";

                order = "order by dogs.id";

                var skipUntilItem = (model.PageNumber - 1) * model.PageSize;
                limit = $"limit {skipUntilItem}, {model.PageSize}";
            }

            fromCommand = $@"
                FROM
                    dogs
                    inner join races
                        on races.id = raceId
                    inner join sizes
                        on sizes.id = sizeId
            ";

            var sqlCommand =
                $@"
                    SELECT 
                        *
                    {fromCommand}
                    {condition}
                    {order} 
                    {limit}
                ";

            var sqlCountCommnad =
                $@"
                    SELECT 
                        count(*)
                    {fromCommand}
                    {condition}
                ";

            using (var connection = new MySqlConnection(connectionString))
            {
                var sqlResponse = await connection.QueryAsync <Dog, Race, Size, Dog>(
                    sqlCommand,
                    (d, r, s) =>
                {
                    d.Race = r;
                    d.Size = s;

                    return(d);
                },
                    splitOn : "id, id" // first id = races.id second id = sizes.id
                    );

                dogs = sqlResponse.ToList();
            }

            var count = await baseRepository.Count(sqlCountCommnad);

            return(new Tuple <int, List <Dog> >(count, dogs));
        }