From 99f21ec181eba9805702661caefca1c90fbfdaf8 Mon Sep 17 00:00:00 2001 From: erius Date: Thu, 11 Aug 2022 02:33:23 +0300 Subject: [PATCH] Players with teams many-to-many relation is now maintained by the FootbalContract class --- EFTask/EFTask/EFTask.csproj | 8 ++++--- EFTask/EFTask/FootballContext.cs | 38 ++++++++++++++------------------ EFTask/EFTask/FootballData.cs | 2 +- EFTask/EFTask/FootballManager.cs | 17 ++++++++++---- EFTask/EFTask/appsettings.json | 9 +++----- 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/EFTask/EFTask/EFTask.csproj b/EFTask/EFTask/EFTask.csproj index 8faa7ed..3a683a4 100644 --- a/EFTask/EFTask/EFTask.csproj +++ b/EFTask/EFTask/EFTask.csproj @@ -8,10 +8,12 @@ - - - + + + + + diff --git a/EFTask/EFTask/FootballContext.cs b/EFTask/EFTask/FootballContext.cs index 92f3cf0..6114f22 100644 --- a/EFTask/EFTask/FootballContext.cs +++ b/EFTask/EFTask/FootballContext.cs @@ -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 Players { get; set; } = null!; public DbSet Teams { get; set; } = null!; public DbSet 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"); } } } diff --git a/EFTask/EFTask/FootballData.cs b/EFTask/EFTask/FootballData.cs index ad99193..b5937a2 100644 --- a/EFTask/EFTask/FootballData.cs +++ b/EFTask/EFTask/FootballData.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace EFTask +namespace EFTask.Data { [Table("players")] public class FootballPlayer diff --git a/EFTask/EFTask/FootballManager.cs b/EFTask/EFTask/FootballManager.cs index 78883a4..38568e0 100644 --- a/EFTask/EFTask/FootballManager.cs +++ b/EFTask/EFTask/FootballManager.cs @@ -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(); + var contracts = fc.Contracts.Include(c => c.Player).Include(c => c.Team).ToHashSet(); 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) diff --git a/EFTask/EFTask/appsettings.json b/EFTask/EFTask/appsettings.json index 85de09c..acd7702 100644 --- a/EFTask/EFTask/appsettings.json +++ b/EFTask/EFTask/appsettings.json @@ -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" } } \ No newline at end of file