add changes
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace server.Persistence
|
||||
{
|
||||
public class DatabaseSettings : IDatabaseSettings
|
||||
{
|
||||
public string? ConnectionString { get; set; }
|
||||
public string? DatabaseName { get; set; }
|
||||
public string? SeasonsCollection { get; set; }
|
||||
public string? EpisodesCollection { get; set; }
|
||||
public string? CharactersCollection { get; set; }
|
||||
public string? QuotesCollection { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.OpenApi.Writers;
|
||||
using MongoDB.Driver;
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence
|
||||
{
|
||||
public class DbContext : IDbContext
|
||||
{
|
||||
public IMongoCollection<Season> Seasons { get; set; }
|
||||
|
||||
public IMongoCollection<Episode> Episodes { get; set; }
|
||||
|
||||
public IMongoCollection<Character> Characters { get; set; }
|
||||
|
||||
public IMongoCollection<Quote> Quotes { get; set; }
|
||||
|
||||
|
||||
public DbContext(IDatabaseSettings settings)
|
||||
{
|
||||
IMongoClient client = new MongoClient(settings.ConnectionString);
|
||||
IMongoDatabase database = client.GetDatabase(settings.DatabaseName);
|
||||
|
||||
Seasons = database.GetCollection<Season>(settings.SeasonsCollection);
|
||||
Episodes = database.GetCollection<Episode>(settings.EpisodesCollection);
|
||||
Characters = database.GetCollection<Character>(settings.CharactersCollection);
|
||||
Quotes = database.GetCollection<Quote>(settings.QuotesCollection);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace server.Persistence
|
||||
{
|
||||
public interface IDatabaseSettings
|
||||
{
|
||||
string? ConnectionString { get; set; }
|
||||
string? DatabaseName { get; set; }
|
||||
string? SeasonsCollection { get; set; }
|
||||
string? EpisodesCollection { get; set; }
|
||||
string? CharactersCollection { get; set; }
|
||||
string? QuotesCollection { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Driver;
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence
|
||||
{
|
||||
public interface IDbContext
|
||||
{
|
||||
public IMongoCollection<Season> Seasons { get; set; }
|
||||
|
||||
public IMongoCollection<Episode> Episodes { get; set; }
|
||||
|
||||
public IMongoCollection<Character> Characters { get; set; }
|
||||
|
||||
public IMongoCollection<Quote> Quotes { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence.Repositories
|
||||
{
|
||||
public interface ISeasonRepository
|
||||
{
|
||||
Task<List<Season>> GetSeasonsAsync();
|
||||
Task<Season> GetSeasonByNumberAsync(int number);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using MongoDB.Driver;
|
||||
using server.Models;
|
||||
|
||||
namespace server.Persistence.Repositories
|
||||
{
|
||||
public class SeasonRepository : ISeasonRepository
|
||||
{
|
||||
private readonly IDbContext _context;
|
||||
|
||||
public SeasonRepository(IDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<List<Season>> GetSeasonsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.Seasons.Find(season => true).ToListAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Season> GetSeasonByNumberAsync(int number)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _context.Seasons.Find(season => season.SeasonNumber == number).SingleOrDefaultAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user