Players with teams many-to-many relation is now maintained by the FootbalContract class
This commit is contained in:
parent
0ef7fd22bf
commit
99f21ec181
5 changed files with 38 additions and 36 deletions
|
@ -8,10 +8,12 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-preview.7.22376.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="7.0.0-preview.7.22376.6" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.6" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0-preview.7.22375.6" />
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.0-preview.7" />
|
||||||
<PackageReference Include="Sharprompt" Version="2.4.3" />
|
<PackageReference Include="Sharprompt" Version="2.4.3" />
|
||||||
|
<PackageReference Include="StackExchange.Redis" Version="2.6.48" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
using Microsoft.EntityFrameworkCore;
|
using EFTask.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using StackExchange.Redis;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -13,39 +15,31 @@ namespace EFTask
|
||||||
public DbSet<FootballPlayer> Players { get; set; } = null!;
|
public DbSet<FootballPlayer> Players { get; set; } = null!;
|
||||||
public DbSet<FootballTeam> Teams { get; set; } = null!;
|
public DbSet<FootballTeam> Teams { get; set; } = null!;
|
||||||
public DbSet<FootbalContract> Contracts { get; set; } = null!;
|
public DbSet<FootbalContract> Contracts { get; set; } = null!;
|
||||||
|
public IDatabase RedisCache { get; protected set; } = null!;
|
||||||
|
|
||||||
public FootballContext()
|
public FootballContext()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Connecting to database...");
|
Console.WriteLine("Ensuring that the database exists...");
|
||||||
Database.EnsureCreated();
|
Database.EnsureCreated();
|
||||||
Console.WriteLine("Connection established");
|
Console.WriteLine("Success");
|
||||||
Console.WriteLine();
|
Console.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
{
|
{
|
||||||
string connectionString = GetConnectionString();
|
Console.WriteLine("Connecting to database...");
|
||||||
optionsBuilder.UseNpgsql(connectionString);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string GetConnectionString()
|
|
||||||
{
|
|
||||||
var builder = new ConfigurationBuilder();
|
var builder = new ConfigurationBuilder();
|
||||||
var config = builder.SetBasePath(Directory.GetCurrentDirectory())
|
var config = builder.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
.AddJsonFile("appsettings.json")
|
.AddJsonFile("appsettings.json")
|
||||||
.Build();
|
.Build();
|
||||||
var dbConnectionSettings = config.GetSection("databaseConnectionSettings");
|
var psqlConnection = config.GetConnectionString("Postgres");
|
||||||
string host = dbConnectionSettings["host"],
|
optionsBuilder.UseNpgsql(psqlConnection);
|
||||||
port = dbConnectionSettings["port"],
|
Console.WriteLine("Connection established");
|
||||||
db = dbConnectionSettings["database"],
|
|
||||||
user = dbConnectionSettings["username"],
|
Console.WriteLine("Connecting to redis...");
|
||||||
password = dbConnectionSettings["password"];
|
var redisConnection = config.GetConnectionString("Redis");
|
||||||
return $"Host={host};Port={port};Database={db};Username={user};Password={password};";
|
RedisCache = ConnectionMultiplexer.Connect(redisConnection).GetDatabase();
|
||||||
|
Console.WriteLine("Connection established");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace EFTask
|
namespace EFTask.Data
|
||||||
{
|
{
|
||||||
[Table("players")]
|
[Table("players")]
|
||||||
public class FootballPlayer
|
public class FootballPlayer
|
||||||
|
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using EFTask.Data;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Sharprompt;
|
using Sharprompt;
|
||||||
|
|
||||||
|
@ -166,7 +167,7 @@ namespace EFTask
|
||||||
|
|
||||||
protected void ShowContracts(FootballContext fc)
|
protected void ShowContracts(FootballContext fc)
|
||||||
{
|
{
|
||||||
var contracts = fc.Contracts.ToHashSet<FootbalContract?>();
|
var contracts = fc.Contracts.Include(c => c.Player).Include(c => c.Team).ToHashSet<FootbalContract?>();
|
||||||
contracts.Add(null);
|
contracts.Add(null);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
@ -181,7 +182,7 @@ namespace EFTask
|
||||||
+ $"\tAge: {contractToDescribe.Player.Age}\n"
|
+ $"\tAge: {contractToDescribe.Player.Age}\n"
|
||||||
+ $"Team:\n"
|
+ $"Team:\n"
|
||||||
+ $"\tId: {contractToDescribe.TeamId}\n"
|
+ $"\tId: {contractToDescribe.TeamId}\n"
|
||||||
+ $"\tNAme: {contractToDescribe.Team.Name}\n"
|
+ $"\tName: {contractToDescribe.Team.Name}\n"
|
||||||
+ $"Salary: {contractToDescribe.Salary} $/year\n\n"
|
+ $"Salary: {contractToDescribe.Salary} $/year\n\n"
|
||||||
+ "Press any key to go back...");
|
+ "Press any key to go back...");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
@ -200,7 +201,7 @@ namespace EFTask
|
||||||
}
|
}
|
||||||
var player = Prompt.Select("Select the contract's player", fc.Players, 10,
|
var player = Prompt.Select("Select the contract's player", fc.Players, 10,
|
||||||
textSelector: p => $"{p.Id}. {p.Name}, {p.Age}");
|
textSelector: p => $"{p.Id}. {p.Name}, {p.Age}");
|
||||||
var availableTeams = fc.Teams.Except(player.Contracts.Select(p => p.Team));
|
var availableTeams = fc.Teams.ToHashSet().Except(player.Contracts.Select(p => p.Team));
|
||||||
if (availableTeams.Count() == 0)
|
if (availableTeams.Count() == 0)
|
||||||
{
|
{
|
||||||
Console.WriteLine("No available teams were found, insert a new team to make a contract with this player");
|
Console.WriteLine("No available teams were found, insert a new team to make a contract with this player");
|
||||||
|
@ -258,7 +259,15 @@ namespace EFTask
|
||||||
|
|
||||||
protected void PlayersTeamAmount(FootballContext fc)
|
protected void PlayersTeamAmount(FootballContext fc)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
Console.WriteLine("Calculating amount of players in a team...");
|
||||||
|
var team = Prompt.Select("Select a team", fc.Teams, 10,
|
||||||
|
textSelector: t => $"{t.Id}. {t.Name}");
|
||||||
|
var cachedValue = fc.RedisCache.StringGet(team.Id.ToString());
|
||||||
|
if (cachedValue != StackExchange.Redis.RedisValue.Null)
|
||||||
|
{
|
||||||
|
var result = cachedValue.ToString();
|
||||||
|
Console.WriteLine($"There are {result} players in team {team.Name}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PlayersTotalAmount(FootballContext fc)
|
protected void PlayersTotalAmount(FootballContext fc)
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
{
|
{
|
||||||
"databaseConnectionSettings": {
|
"ConnectionStrings": {
|
||||||
"host": "localhost",
|
"Postgres": "host=localhost;port=5432;database=football;username=cbgr;password=cbgr",
|
||||||
"port": "5432",
|
"Redis": "localhost:6379"
|
||||||
"database": "football",
|
|
||||||
"username": "cbgr",
|
|
||||||
"password": "cbgr"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue