I am using ASP.NET Core Web API. I have these models:
public abstract class EntityBase
{
[Key]
public int Id { get; set; }
}
public class Mandate : EntityBase
{
public int? MerchantId { get; set; }
public DateTime DueDate { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
[ForeignKey("MerchantId")]
public virtual Merchant Merchant { get; set; }
}
Model: Mandate
ViewModel (Dto):
public class MandateGetDto
{
public int? MerchantId { get; set; }
public DateTime DueDate { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
IBaseRepository:
public interface IBaseRepository<T> where T : BaseEntity
{
Task<IEnumerable<T>> GetAll();
bool EntityExists(long id);
}
BaseRepository:
public class BaseRepository<T> : IBaseRepository<T> where T : AuditableBaseEntity
{
private readonly DDMDbContext _context;
private DbSet<T> _entities;
public BaseRepository(DDMDbContext context)
{
_context = context;
_entities = context.Set<T>();
}
public async Task<IEnumerable<T>> GetAll()
{
var list = await _entities.ToListAsync();
return list;
}
public bool EntityExists(long id)
{
return _entities.Any(x => x.Id == id);
}
}
EntityMapper:
public MandateGetDto FromMandateToMandateGetDto(Mandate mandate)
{
MandateGetDto mandateDto = new MandateGetDto();
mandateDto.MerchantId = mandate.MerchantId;
mandateDto.DueDate = mandate.DueDate;
mandateDto.StartDate = mandate.StartDate;
mandateDto.EndDate = mandate.EndDate;
return mandateDto;
}
UnitOfWork:
public class UnitOfWork : IUnitOfWork
{
private readonly DContext _context;
public UnitOfWork(DContext context)
{
_context = context;
}
#region Mandate
private readonly IBaseRepository<Mandate> _mandateRepository;
public IBaseRepository<Mandate> MandateRepository => _mandateRepository ?? new BaseRepository<Mandate>(_context);
# endregion
public void Dispose()
{
if (_context != null)
{
_context.Dispose();
}
}
}
Below is the code I have written for the Mandate service which retrieves all the records for the mandates.
MandateService:
public async Task<ResponsePagination<GenericPagination<MandateGetDto>>> GetAll(int page, int sizeByPage)
{
string nextRoute = null, previousRoute = null;
IEnumerable<Mandate> data = await _unitOfWork.MandateRepository.GetAll();
var mapper = new EntityMapper();
var mandatesDto = data.Select(m => mapper.FromMandateToMandateGetDto(m)).ToList();
GenericPagination<MandateGetDto> objGenericPagination = GenericPagination<MandateGetDto>.Create(mandatesDto, page, sizeByPage);
ResponsePagination<GenericPagination<MandateGetDto>> response = new ResponsePagination<GenericPagination<MandateGetDto>>(objGenericPagination);
response.CurrentPage = objGenericPagination.CurrentPage;
response.HasNextPage = objGenericPagination.HasNextPage;
response.HasPreviousPage = objGenericPagination.HasPreviousPage;
response.PageSize = objGenericPagination.PageSize;
response.TotalPages = objGenericPagination.TotalPages;
response.TotalRecords = objGenericPagination.TotalRecords;
response.Data = objGenericPagination;
if (response.HasNextPage)
{
nextRoute = $"/mandates?page={(page 1)}";
response.NextPageUrl = _uriPaginationService.GetPaginationUri(page, nextRoute).ToString();
}
else
{
response.NextPageUrl = null;
}
if (response.HasPreviousPage)
{
previousRoute = $"/mandates?page={(page - 1)}";
response.PreviousPageUrl = _uriPaginationService.GetPaginationUri(page, previousRoute).ToString();
}
else
{
response.PreviousPageUrl = null;
}
return response;
}
public async Task<IEnumerable<Mandate>> GetMandates()
{
return await _unitOfWork.MandateRepository.GetAll();
}
startup.cs:
services.AddTransient<IMandateService, MandateService>();
Controller:
[Produces("application/json")]
[ApiController]
[ApiVersion("1.0")]
public class AdminController : ControllerBase
{
private readonly IMerchantService _merchantService;
private readonly DDMDbContext _context;
public AdminController(DDMDbContext context, IMerchantService merchantService)
{
_merchantService = merchantService;
_context = context;
}
[HttpGet("mandates")]
[Authorize]
public async Task<ResponsePagination<GenericPagination<MandateGetDto>>> GetAllMyMandates(int page, int sizeByPage)
{
return await _mandateService.GetAll(page, sizeByPage);
}
}
When I used POSTMAN for the Get Request, I got this response:
{
"current_page": 0,
"page_size": 0,
"total_pages": -2147483648,
"total_records": 1,
"has_next_page": false,
"has_previous_page": false,
"data": []
}
Data is blank while total record is 1.
How do I resolve this?
Thanks
CodePudding user response:
You have not shown us the principal part of your code, which is the controller
Controller is the reason why your web API even works. So, if you have misconfigured something, it should be present in the controller first or then somewhere else.
Show us the code for the controller.
Edit
I am not sure if DbSet is appropriate for the usage, but using the context object works for me.
You should use
_context.MandateOrWhateverElseItIs.ToListAsync();
instead of using
_entities.ToListAsync();
Using the DbSet can be an issue, I recommend using the context. My application works fruitfully with context.
Edit
This is your code in BaseRepository
public class BaseRepository<T> : IBaseRepository<T> where T : AuditableBaseEntity
{
private readonly DDMDbContext _context;
private DbSet<T> _entities;
public BaseRepository(DDMDbContext context)
{
_context = context;
_entities = context.Set<T>();
}
public async Task<IEnumerable<T>> GetAll()
{
var list = await _entities.ToListAsync();
return list;
}
public bool EntityExists(long id)
{
return _entities.Any(x => x.Id == id);
}
}
What you should change it to is
public class BaseRepository<T> : IBaseRepository<T> where T : AuditableBaseEntity
{
private readonly DDMDbContext _context;
private DbSet<T> _entities;
public BaseRepository(DDMDbContext context)
{
_context = context;
_entities = context.Set<T>();
}
public async Task<IEnumerable<T>> GetAll()
{
//Changes are here
var list = await _context.EntitiesOrWhateverElseItIs.ToListAsync(); //I don't know what the option is, you can look it inside the autocomplete. Be sure to change EntitiesOrWhateverElseItIs with the option you see in autocomplete menu
return list;
}
public bool EntityExists(long id)
{
return _entities.Any(x => x.Id == id);
}
}