Updating
This commit is contained in:
12
teams/Services/ITeamsRepository.cs
Normal file
12
teams/Services/ITeamsRepository.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using Teams.Entities;
|
||||
|
||||
namespace Teams.Services
|
||||
{
|
||||
public interface ITeamsRepository
|
||||
{
|
||||
Task<Team?> GetTeamAsync(int teamId);
|
||||
Task<(IEnumerable<Team>, PaginationMetadata)> GetTeamsAsync(int pageNumber, int pageSize);
|
||||
}
|
||||
}
|
||||
|
||||
21
teams/Services/PaginationMetadata.cs
Normal file
21
teams/Services/PaginationMetadata.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace Teams.Services
|
||||
{
|
||||
public class PaginationMetadata
|
||||
{
|
||||
public int TotalItemCount { get; set; }
|
||||
public int TotalPageCount { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
public int CurrentPage { get; set; }
|
||||
|
||||
public PaginationMetadata(int totalItemCount, int pageSize, int currentPage)
|
||||
{
|
||||
TotalItemCount = totalItemCount;
|
||||
PageSize = pageSize;
|
||||
CurrentPage = currentPage;
|
||||
TotalPageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
36
teams/Services/TeamsRepository.cs
Normal file
36
teams/Services/TeamsRepository.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Teams.DbContexts;
|
||||
using Teams.Entities;
|
||||
|
||||
namespace Teams.Services
|
||||
{
|
||||
public class TeamsRepository : ITeamsRepository
|
||||
{
|
||||
private readonly MlbGameDayContext _context;
|
||||
|
||||
public TeamsRepository(MlbGameDayContext context)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
public async Task<Team?> GetTeamAsync(int teamId)
|
||||
{
|
||||
return await _context.Teams.Where(t => t.Id == teamId).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<(IEnumerable<Team>, PaginationMetadata)> GetTeamsAsync(int pageNumber, int pageSize)
|
||||
{
|
||||
var collection = _context.Teams as IQueryable<Team>;
|
||||
var totalItemCount = await collection.CountAsync();
|
||||
var paginationMetadata = new PaginationMetadata(totalItemCount, pageSize, pageNumber);
|
||||
var collectionToReturn = await collection.OrderBy(t => t.Name)
|
||||
.Skip(pageSize * (pageNumber - 1))
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
|
||||
return (collectionToReturn, paginationMetadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user