diff --git a/EFTask/.dockerignore b/EFTask/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/EFTask/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/EFTask/EFTask.sln b/EFTask/EFTask.sln index 682ffe0..239ebbf 100644 --- a/EFTask/EFTask.sln +++ b/EFTask/EFTask.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.2.32630.192 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFTask", "EFTask\EFTask.csproj", "{38961979-A830-4012-9CD2-A7AFA0201540}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFTask", "EFTask\EFTask.csproj", "{38961979-A830-4012-9CD2-A7AFA0201540}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/EFTask/EFTask/EFTask.csproj b/EFTask/EFTask/EFTask.csproj index 3a683a4..3d25760 100644 --- a/EFTask/EFTask/EFTask.csproj +++ b/EFTask/EFTask/EFTask.csproj @@ -5,12 +5,14 @@ net6.0 enable enable + Linux + diff --git a/EFTask/EFTask/FootballManager.cs b/EFTask/EFTask/FootballManager.cs index 312eb15..8efd225 100644 --- a/EFTask/EFTask/FootballManager.cs +++ b/EFTask/EFTask/FootballManager.cs @@ -84,6 +84,7 @@ namespace EFTask var player = new FootballPlayer() { Name = name, Age = age }; fc.Players.Add(player); fc.SaveChanges(); + fc.RedisCache.StringSet("total_players", fc.Players.Count()); Console.WriteLine($"Successfully inserted a new player with an id {player.Id}"); } @@ -109,6 +110,7 @@ namespace EFTask foreach (var player in playersToDelete) fc.Players.Remove(player); fc.SaveChanges(); + fc.RedisCache.StringSet("total_players", fc.Players.Count()); Console.WriteLine($"Successfully deleted {playersToDelete.Count()} players"); } @@ -138,6 +140,7 @@ namespace EFTask var team = new FootballTeam() { Name = name }; fc.Teams.Add(team); fc.SaveChanges(); + fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString()); Console.WriteLine($"Successfully inserted a new team with an id {team.Id}"); } @@ -162,6 +165,7 @@ namespace EFTask foreach (var team in teamsToDelete) fc.Teams.Remove(team); fc.SaveChanges(); + fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString()); Console.WriteLine($"Successfully deleted {teamsToDelete.Count()} teams"); } @@ -214,6 +218,7 @@ namespace EFTask var contract = new FootbalContract() { Player = player, Team = team, Salary = salary }; fc.Contracts.Add(contract); fc.SaveChanges(); + fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString()); Console.WriteLine($"Successfully inserted a new contract with an id {contract.Id}"); } @@ -243,6 +248,7 @@ namespace EFTask var salary = Prompt.Input("Input the player's salary", contract.Salary, contract.Salary.ToString()); contract.Salary = salary; fc.SaveChanges(); + fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString()); Console.WriteLine($"Successfully updated a contract with an id {contract.Id}"); } @@ -254,6 +260,7 @@ namespace EFTask foreach (var contract in contractsToDelete) fc.Contracts.Remove(contract); fc.SaveChanges(); + fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString()); Console.WriteLine($"Successfully deleted {contractsToDelete.Count()} contracts"); } @@ -262,27 +269,43 @@ namespace EFTask 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}"); - } + var playersAmount = team.Contracts.Count(); + Console.WriteLine($"There are {playersAmount} players in team {team.Name}"); } protected void PlayersTotalAmount(FootballContext fc) { - throw new NotImplementedException(); + var cachedValue = fc.RedisCache.StringGet("total_players"); + string playersAmount; + if (cachedValue.IsNull) + { + playersAmount = fc.Players.Count().ToString(); + fc.RedisCache.StringSet("total_players", playersAmount); + } + else playersAmount = cachedValue.ToString(); + Console.WriteLine($"There are {playersAmount} players in total"); } protected void TeamExpenses(FootballContext fc) { - throw new NotImplementedException(); + Console.WriteLine("Calculating expenses of a team..."); + var team = Prompt.Select("Select a team", fc.Teams, 10, + textSelector: t => $"{t.Id}. {t.Name}"); + var expenses = team.Contracts.Sum(c => c.Salary); + Console.WriteLine($"Expenses of team {team.Name} are {expenses} $/year"); } protected void OverallExpenses(FootballContext fc) { - throw new NotImplementedException(); + var cachedValue = fc.RedisCache.StringGet("overall_expenses"); + string expenses; + if (cachedValue.IsNull) + { + expenses = fc.Contracts.Sum(c => c.Salary).ToString(); + fc.RedisCache.StringSet("overall_expenses", expenses); + } + else expenses = cachedValue.ToString(); + Console.WriteLine($"Overall expenses are {expenses} $/year"); } protected void Quit(FootballContext _) diff --git a/EFTask/EFTask/Properties/launchSettings.json b/EFTask/EFTask/Properties/launchSettings.json new file mode 100644 index 0000000..9888a95 --- /dev/null +++ b/EFTask/EFTask/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "EFTask": { + "commandName": "Project" + }, + "Docker": { + "commandName": "Docker" + } + } +} \ No newline at end of file diff --git a/EFTask/EFTask/appsettings.json b/EFTask/EFTask/appsettings.json index acd7702..7e7c345 100644 --- a/EFTask/EFTask/appsettings.json +++ b/EFTask/EFTask/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "Postgres": "host=localhost;port=5432;database=football;username=cbgr;password=cbgr", - "Redis": "localhost:6379" + "Postgres": "host=localhost;port=6543;database=football;username=cbgr;password=cbgr", + "Redis": "localhost:7623" } } \ No newline at end of file diff --git a/EFTask/docker-compose.dcproj b/EFTask/docker-compose.dcproj new file mode 100644 index 0000000..3fa62f0 --- /dev/null +++ b/EFTask/docker-compose.dcproj @@ -0,0 +1,15 @@ + + + + 2.1 + Linux + e5b07c27-2842-4f0d-ade4-ae8afc101721 + + + + docker-compose.yml + + + + + \ No newline at end of file diff --git a/EFTask/docker-compose.override.yml b/EFTask/docker-compose.override.yml new file mode 100644 index 0000000..8e89b07 --- /dev/null +++ b/EFTask/docker-compose.override.yml @@ -0,0 +1 @@ +version: '3.4' diff --git a/EFTask/docker-compose.yml b/EFTask/docker-compose.yml new file mode 100644 index 0000000..faa249f --- /dev/null +++ b/EFTask/docker-compose.yml @@ -0,0 +1,7 @@ +version: '3.4' + +name: EFTask +services: + psql: + build: . + image: postgres:latest