Example #1
0
        public async Task <ServiceResponse <List <GetManufacturerDto> > > GetAllManufacturerAsync(PaginationParam pagination, QueryManufacturerDto query)
        {
            var response = new ServiceResponse <List <GetManufacturerDto> >();

            try
            {
                var dbManufacturers = await _context.Manufacturers
                                      .Where(x => query.Name == null?true : x.Name.Contains(query.Name))
                                      .Skip(pagination.Skip())
                                      .Take(pagination.PerPage)
                                      .ToListAsync();

                var totalItemsQuantity = await _context.Manufacturers
                                         .Where(x => query.Name == null?true : x.Name.Contains(query.Name))
                                         .CountAsync();

                response.Data       = _mapper.Map <List <GetManufacturerDto> >(dbManufacturers);
                response.Pagination = new Pagination
                {
                    CurrentPage = pagination.Page,
                    TotalPage   = pagination.TotalPage(totalItemsQuantity),
                    Count       = totalItemsQuantity,
                };

                return(response);
            }
            catch (BaseServiceException ex)
            {
                response.Success = false;
                response.Message = ex.ErrorMessage;
                response.Code    = ex.Code;

                _logger.LogError(ex.Message, ex.StackTrace);
                return(response);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                response.Code    = ErrorCode.MANUFACTURER_UNEXPECTED_ERROR;

                _logger.LogError(ex.Message, ex.StackTrace);
                return(response);
            }
        }
        public async Task <IActionResult> GetAll([FromQuery] PaginationParam pagination, [FromQuery] QueryManufacturerDto query)
        {
            var response = await _service.GetAllManufacturerAsync(pagination, query);

            if (!response.Success)
            {
                return(BadRequest(response));
            }

            return(Ok(response));
        }