Players with teams many-to-many relation is now maintained by the FootbalContract class

This commit is contained in:
Egor 2022-08-11 02:33:23 +03:00
parent 0ef7fd22bf
commit 99f21ec181
5 changed files with 38 additions and 36 deletions

View file

@ -8,10 +8,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-preview.7.22376.2" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="7.0.0-preview.7.22376.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="StackExchange.Redis" Version="2.6.48" />
</ItemGroup>
<ItemGroup>

View file

@ -1,5 +1,7 @@
using Microsoft.EntityFrameworkCore;
using EFTask.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
@ -13,39 +15,31 @@ namespace EFTask
public DbSet<FootballPlayer> Players { get; set; } = null!;
public DbSet<FootballTeam> Teams { get; set; } = null!;
public DbSet<FootbalContract> Contracts { get; set; } = null!;
public IDatabase RedisCache { get; protected set; } = null!;
public FootballContext()
{
Console.WriteLine("Connecting to database...");
Console.WriteLine("Ensuring that the database exists...");
Database.EnsureCreated();
Console.WriteLine("Connection established");
Console.WriteLine();
Console.WriteLine("Success");
Console.Clear();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = GetConnectionString();
optionsBuilder.UseNpgsql(connectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
private static string GetConnectionString()
{
Console.WriteLine("Connecting to database...");
var builder = new ConfigurationBuilder();
var config = builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var dbConnectionSettings = config.GetSection("databaseConnectionSettings");
string host = dbConnectionSettings["host"],
port = dbConnectionSettings["port"],
db = dbConnectionSettings["database"],
user = dbConnectionSettings["username"],
password = dbConnectionSettings["password"];
return $"Host={host};Port={port};Database={db};Username={user};Password={password};";
var psqlConnection = config.GetConnectionString("Postgres");
optionsBuilder.UseNpgsql(psqlConnection);
Console.WriteLine("Connection established");
Console.WriteLine("Connecting to redis...");
var redisConnection = config.GetConnectionString("Redis");
RedisCache = ConnectionMultiplexer.Connect(redisConnection).GetDatabase();
Console.WriteLine("Connection established");
}
}
}

View file

@ -6,7 +6,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFTask
namespace EFTask.Data
{
[Table("players")]
public class FootballPlayer

View file

@ -4,6 +4,7 @@ using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using EFTask.Data;
using Microsoft.EntityFrameworkCore;
using Sharprompt;
@ -166,7 +167,7 @@ namespace EFTask
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);
while (true)
{
@ -181,7 +182,7 @@ namespace EFTask
+ $"\tAge: {contractToDescribe.Player.Age}\n"
+ $"Team:\n"
+ $"\tId: {contractToDescribe.TeamId}\n"
+ $"\tNAme: {contractToDescribe.Team.Name}\n"
+ $"\tName: {contractToDescribe.Team.Name}\n"
+ $"Salary: {contractToDescribe.Salary} $/year\n\n"
+ "Press any key to go back...");
Console.ReadKey();
@ -200,7 +201,7 @@ namespace EFTask
}
var player = Prompt.Select("Select the contract's player", fc.Players, 10,
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)
{
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)
{
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)

View file

@ -1,9 +1,6 @@
{
"databaseConnectionSettings": {
"host": "localhost",
"port": "5432",
"database": "football",
"username": "cbgr",
"password": "cbgr"
"ConnectionStrings": {
"Postgres": "host=localhost;port=5432;database=football;username=cbgr;password=cbgr",
"Redis": "localhost:6379"
}
}