Elimnating repository pattern
This commit is contained in:
@@ -24,7 +24,7 @@ namespace Teams.DbContexts
|
||||
if (!optionsBuilder.IsConfigured)
|
||||
{
|
||||
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
|
||||
optionsBuilder.UseNpgsql("Host=localhost;Database=mlb-game-day;Username=postgres;Password=postgres");
|
||||
optionsBuilder.UseNpgsql("Host=localhost;Port=5450;Database=teams;Username=postgres;Password=postgres");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
using System;
|
||||
namespace Teams.Filter
|
||||
{
|
||||
public class PaginationFilter
|
||||
{
|
||||
public int PageNumber { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
|
||||
public PaginationFilter()
|
||||
{
|
||||
this.PageNumber = 1;
|
||||
this.PageSize = 10;
|
||||
}
|
||||
|
||||
public PaginationFilter(int pageNumber, int pageSize)
|
||||
{
|
||||
this.PageNumber = pageNumber < 1 ? 1 : pageNumber;
|
||||
this.PageSize = pageSize > 10 ? 10 : pageSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
using System;
|
||||
namespace Teams.Models
|
||||
{
|
||||
public class TeamDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Abbreviation { get; set; }
|
||||
public string? Active { get; set; }
|
||||
public string? AllStarStatus { get; set; }
|
||||
public string? ClubName { get; set; }
|
||||
public int? DivisionId { get; set; }
|
||||
public string? FileCode { get; set; }
|
||||
public int? FirstYearOfPlay { get; set; }
|
||||
public string? FranchiseName { get; set; }
|
||||
public int? LeagueId { get; set; }
|
||||
public string? Link { get; set; }
|
||||
public string? LocationName { get; set; }
|
||||
public string? Season { get; set; }
|
||||
public string? ShortName { get; set; }
|
||||
public int? SpringLeagueId { get; set; }
|
||||
public int? SpringVenueId { get; set; }
|
||||
public int? SportId { get; set; }
|
||||
public string? TeamCode { get; set; }
|
||||
public string? TeamName { get; set; }
|
||||
public int? VenueId { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
using AutoMapper;
|
||||
|
||||
namespace Teams.Profiles
|
||||
{
|
||||
public class TeamProfile : Profile
|
||||
{
|
||||
public TeamProfile()
|
||||
{
|
||||
CreateMap<Entities.Team, Models.TeamDto>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using Teams.Services;
|
||||
|
||||
namespace Teams.Wrappers
|
||||
{
|
||||
public class PagedResponse<T>: Response<T>
|
||||
{
|
||||
public int TotalItemCount { get; set; }
|
||||
public int TotalPageCount { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
public int CurrentPage { get; set; }
|
||||
|
||||
public PagedResponse(T results, PaginationMetadata paginationMetadata)
|
||||
{
|
||||
this.TotalItemCount = paginationMetadata.TotalItemCount;
|
||||
this.TotalPageCount = paginationMetadata.TotalPageCount;
|
||||
this.CurrentPage = paginationMetadata.CurrentPage;
|
||||
this.PageSize = paginationMetadata.PageSize;
|
||||
this.CurrentPage = paginationMetadata.CurrentPage;
|
||||
this.Results = results;
|
||||
this.Message = null;
|
||||
this.Succeeded = true;
|
||||
this.Errors = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
using System;
|
||||
namespace Teams.Wrappers
|
||||
{
|
||||
public class Response<T>
|
||||
{
|
||||
public T Results { get; set; }
|
||||
public bool Succeeded { get; set; }
|
||||
public string[] Errors { get; set; }
|
||||
public string Message { get; set; }
|
||||
|
||||
public Response()
|
||||
{
|
||||
}
|
||||
|
||||
public Response(T results)
|
||||
{
|
||||
Succeeded = true;
|
||||
Message = string.Empty;
|
||||
Errors = null;
|
||||
Results = results;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,26 +6,26 @@ using Venues.Entities;
|
||||
|
||||
namespace Venues.DbContexts
|
||||
{
|
||||
public partial class MlbGameDayContext : DbContext
|
||||
public partial class VenuesContext : DbContext
|
||||
{
|
||||
public MlbGameDayContext()
|
||||
public VenuesContext()
|
||||
{
|
||||
}
|
||||
|
||||
public MlbGameDayContext(DbContextOptions<MlbGameDayContext> options)
|
||||
public VenuesContext(DbContextOptions<VenuesContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual DbSet<Venue> Venues { get; set; } = null!;
|
||||
|
||||
//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
//{
|
||||
// if (!optionsBuilder.IsConfigured)
|
||||
// {
|
||||
// optionsBuilder.UseNpgsql("Host=localhost;Database=mlb-game-day;Username=postgres;Password=postgres");
|
||||
// }
|
||||
//}
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (!optionsBuilder.IsConfigured)
|
||||
{
|
||||
optionsBuilder.UseNpgsql("Host=localhost;Port=5460;Database=venues;Username=postgres;Password=postgres");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
@@ -1,22 +0,0 @@
|
||||
using System;
|
||||
namespace Venues.Filter
|
||||
{
|
||||
public class PaginationFilter
|
||||
{
|
||||
public int PageNumber { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
|
||||
public PaginationFilter()
|
||||
{
|
||||
this.PageNumber = 1;
|
||||
this.PageSize = 10;
|
||||
}
|
||||
|
||||
public PaginationFilter(int pageNumber, int pageSize)
|
||||
{
|
||||
this.PageNumber = pageNumber < 1 ? 1 : pageNumber;
|
||||
this.PageSize = pageSize > 10 ? 10 : pageSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
using System;
|
||||
namespace Venues.Models
|
||||
{
|
||||
public class VenueDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Link { get; set; }
|
||||
public string Active { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
using AutoMapper;
|
||||
|
||||
namespace Venues.Profiles
|
||||
{
|
||||
public class VenueProfile : Profile
|
||||
{
|
||||
public VenueProfile()
|
||||
{
|
||||
CreateMap<Entities.Venue, Models.VenueDto>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
using Venues.Entities;
|
||||
|
||||
namespace Venues.Services
|
||||
{
|
||||
public interface IVenuesRepository
|
||||
{
|
||||
Task<(IEnumerable<Venue>, PaginationMetadata)> GetVenuesAsync(int pageNumber, int pageSize);
|
||||
Task<Venue?> GetVenueAsync(int venueId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
using System;
|
||||
namespace Venues.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Venues.DbContexts;
|
||||
using Venues.Entities;
|
||||
|
||||
namespace Venues.Services
|
||||
{
|
||||
public class VenuesRepository : IVenuesRepository
|
||||
{
|
||||
private readonly MlbGameDayContext _context;
|
||||
|
||||
public VenuesRepository(MlbGameDayContext context)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
public async Task<Venue?> GetVenueAsync(int venueId)
|
||||
{
|
||||
return await _context.Venues.Where(v => v.Id == venueId).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<(IEnumerable<Venue>, PaginationMetadata)> GetVenuesAsync(int pageNumber, int pageSize)
|
||||
{
|
||||
var collection = _context.Venues as IQueryable<Venue>;
|
||||
var totalItemCount = await collection.CountAsync();
|
||||
var paginationMetadata = new PaginationMetadata(totalItemCount, pageSize, pageNumber);
|
||||
var collectionToReturn = await collection.OrderBy(v => v.Name)
|
||||
.Skip(pageSize * (pageNumber -1))
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
|
||||
return (collectionToReturn, paginationMetadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using Venues.Services;
|
||||
|
||||
namespace Venues.Wrappers
|
||||
{
|
||||
public class PagedResponse<T>: Response<T>
|
||||
{
|
||||
public int TotalItemCount { get; set; }
|
||||
public int TotalPageCount { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
public int CurrentPage { get; set; }
|
||||
|
||||
public PagedResponse(T results, PaginationMetadata paginationMetadata)
|
||||
{
|
||||
this.TotalItemCount = paginationMetadata.TotalItemCount;
|
||||
this.TotalPageCount = paginationMetadata.TotalPageCount;
|
||||
this.CurrentPage = paginationMetadata.CurrentPage;
|
||||
this.PageSize = paginationMetadata.PageSize;
|
||||
this.CurrentPage = paginationMetadata.CurrentPage;
|
||||
this.Results = results;
|
||||
this.Message = null;
|
||||
this.Succeeded = true;
|
||||
this.Errors = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
namespace Venues.Wrappers
|
||||
{
|
||||
public class Response<T>
|
||||
{
|
||||
public T Results { get; set; }
|
||||
public bool Succeeded { get; set; }
|
||||
public string[] Errors { get; set; }
|
||||
public string Message { get; set; }
|
||||
|
||||
public Response()
|
||||
{
|
||||
}
|
||||
|
||||
public Response(T results)
|
||||
{
|
||||
Succeeded = true;
|
||||
Message = string.Empty;
|
||||
Errors = null;
|
||||
Results = results;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user