37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|
|
|