From 9a0cbb236f2fc917fa809b5fc0b9ffc3e09b7c2e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 15 Apr 2023 12:28:26 +0700 Subject: [PATCH] initial --- .../Controllers/CasinoControllerV1.cs | 65 ++++++++++++++++ 20230415.2/w230415_casinoapi/Program.cs | 39 ++++++++++ .../Properties/launchSettings.json | 31 ++++++++ .../appsettings.Development.json | 8 ++ 20230415.2/w230415_casinoapi/appsettings.json | 9 +++ .../w230415_casinoapi.csproj | 17 +++++ 20230415.2/w230415_classes/Casino.cs | 76 +++++++++++++++++++ 20230415.2/w230415_classes/Game.cs | 8 ++ 20230415.2/w230415_classes/PiCasino.cs | 18 +++++ .../w230415_classes/w230415_business.csproj | 13 ++++ .../w230415_classes/w230415_classes.sln | 49 ++++++++++++ 20230415.2/w230415_console/Program.cs | 27 +++++++ .../w230415_console/w230415_console.csproj | 14 ++++ 20230415.2/w230415_consoleapi/Program.cs | 35 +++++++++ .../w230415_consoleapi.csproj | 18 +++++ 20230415.2/w230415_contract/Game.cs | 12 +++ 20230415.2/w230415_contract/Intf/ICasino.cs | 11 +++ 20230415.2/w230415_contract/Intf/IGame.cs | 10 +++ .../w230415_contract/w230415_contract.csproj | 9 +++ 19 files changed, 469 insertions(+) create mode 100644 20230415.2/w230415_casinoapi/Controllers/CasinoControllerV1.cs create mode 100644 20230415.2/w230415_casinoapi/Program.cs create mode 100644 20230415.2/w230415_casinoapi/Properties/launchSettings.json create mode 100644 20230415.2/w230415_casinoapi/appsettings.Development.json create mode 100644 20230415.2/w230415_casinoapi/appsettings.json create mode 100644 20230415.2/w230415_casinoapi/w230415_casinoapi.csproj create mode 100644 20230415.2/w230415_classes/Casino.cs create mode 100644 20230415.2/w230415_classes/Game.cs create mode 100644 20230415.2/w230415_classes/PiCasino.cs create mode 100644 20230415.2/w230415_classes/w230415_business.csproj create mode 100644 20230415.2/w230415_classes/w230415_classes.sln create mode 100644 20230415.2/w230415_console/Program.cs create mode 100644 20230415.2/w230415_console/w230415_console.csproj create mode 100644 20230415.2/w230415_consoleapi/Program.cs create mode 100644 20230415.2/w230415_consoleapi/w230415_consoleapi.csproj create mode 100644 20230415.2/w230415_contract/Game.cs create mode 100644 20230415.2/w230415_contract/Intf/ICasino.cs create mode 100644 20230415.2/w230415_contract/Intf/IGame.cs create mode 100644 20230415.2/w230415_contract/w230415_contract.csproj diff --git a/20230415.2/w230415_casinoapi/Controllers/CasinoControllerV1.cs b/20230415.2/w230415_casinoapi/Controllers/CasinoControllerV1.cs new file mode 100644 index 0000000..5fce2e7 --- /dev/null +++ b/20230415.2/w230415_casinoapi/Controllers/CasinoControllerV1.cs @@ -0,0 +1,65 @@ +using Microsoft.AspNetCore.Mvc; +using w230415_classes.Intf; + +namespace w230415_casinoapi.Controllers +{ + [ApiController] + [Route("v1/casino")] + public class CasinoControllerV1 : ControllerBase + { + private readonly ICasino _casino; + + public CasinoControllerV1(ICasino casino) + { + _casino = casino; + } + + [HttpGet("game", Name = "Get list")] + public IEnumerable GetList() + { + return _casino.GetActiveList(); + } + + [HttpPost("game", Name = "Add game")] + public void AddGame( + // [FromQuery] + string name, + // [FromQuery] + double cash) + { + bool bb = _casino.AddGame(name, cash); + Response.StatusCode = (bb ? 200 : 500); + } + + [HttpPost("gameForm", Name = "Add game form")] + public void AddGameForm( + [FromForm] + string name, + [FromForm] + double cash) + { + bool bb = _casino.AddGame(name, cash); + Response.StatusCode = (bb ? 200 : 500); + } + + [HttpDelete("game/{uid}", Name = "Remove game")] + public void RemoveGame( + string uid + ) + { + bool bb = _casino.RemoveGame(uid); + Response.StatusCode = (bb ? 200 : 404); + } + + + [HttpPatch("game/{uid}", Name = "State game")] + public void StateGame( + string uid, + [FromHeader] + string flag) + { + bool bb = _casino.ChangeGameState(uid, flag == "1"); + Response.StatusCode = (bb ? 200 : 404); + } + } +} \ No newline at end of file diff --git a/20230415.2/w230415_casinoapi/Program.cs b/20230415.2/w230415_casinoapi/Program.cs new file mode 100644 index 0000000..f049499 --- /dev/null +++ b/20230415.2/w230415_casinoapi/Program.cs @@ -0,0 +1,39 @@ +using w230415_classes; +using w230415_classes.Intf; + +namespace w230415_casinoapi +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + + + PiCasino? c = new PiCasino(); + builder.Services.AddSingleton(c); + + + + builder.Services.AddControllers(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.MapControllers(); + + app.Run(); + } + } +} \ No newline at end of file diff --git a/20230415.2/w230415_casinoapi/Properties/launchSettings.json b/20230415.2/w230415_casinoapi/Properties/launchSettings.json new file mode 100644 index 0000000..417df6d --- /dev/null +++ b/20230415.2/w230415_casinoapi/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:20598", + "sslPort": 0 + } + }, + "profiles": { + "w230415_casinoapi": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5091", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/20230415.2/w230415_casinoapi/appsettings.Development.json b/20230415.2/w230415_casinoapi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/20230415.2/w230415_casinoapi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/20230415.2/w230415_casinoapi/appsettings.json b/20230415.2/w230415_casinoapi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/20230415.2/w230415_casinoapi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/20230415.2/w230415_casinoapi/w230415_casinoapi.csproj b/20230415.2/w230415_casinoapi/w230415_casinoapi.csproj new file mode 100644 index 0000000..b59ae8a --- /dev/null +++ b/20230415.2/w230415_casinoapi/w230415_casinoapi.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/20230415.2/w230415_classes/Casino.cs b/20230415.2/w230415_classes/Casino.cs new file mode 100644 index 0000000..58f5817 --- /dev/null +++ b/20230415.2/w230415_classes/Casino.cs @@ -0,0 +1,76 @@ +using w230415_classes.Intf; + +namespace w230415_classes +{ + public class Casino : ICasino + { + protected List GameList = new List(); + + + /// + /// Create [C] + /// + /// + /// + /// + public bool AddGame(string name, double cash) + { + GameList.Add(new CGame() { Name = name, Cash = cash, Serial = Guid.NewGuid().ToString("N") }); + return true; + } + + + /// + /// Read [R] list + /// + /// + public IEnumerable GetActiveList() + { + List list = new List(); + foreach (Game? g in GameList) + { + if (g.IsActive) + { + list.Add(g); + } + } + return list; + } + + /// + /// Read [R] single + /// + /// + public IGame? GetGame(string uid) + { + return GameList.FirstOrDefault(p => p.Serial.Equals(uid)); + } + + /// + /// Update [U] + /// + /// + /// + /// + public bool ChangeGameState(string uid, bool isActive) + { + var pp = GameList.FirstOrDefault(p => p.Serial.Equals(uid)); + if (pp == null) return false; + pp.IsActive = isActive; + return true; + } + + + /// + /// Delete [D] single + /// + /// + public bool RemoveGame(string uid) + { + var pp = GameList.FirstOrDefault(p => p.Serial.Equals(uid)); + if (pp == null) return false; + return GameList.Remove(pp); + } + + } +} \ No newline at end of file diff --git a/20230415.2/w230415_classes/Game.cs b/20230415.2/w230415_classes/Game.cs new file mode 100644 index 0000000..ca6cb10 --- /dev/null +++ b/20230415.2/w230415_classes/Game.cs @@ -0,0 +1,8 @@ +using w230415_classes.Intf; + +namespace w230415_classes +{ + public class CGame : Game + { + } +} \ No newline at end of file diff --git a/20230415.2/w230415_classes/PiCasino.cs b/20230415.2/w230415_classes/PiCasino.cs new file mode 100644 index 0000000..f057afe --- /dev/null +++ b/20230415.2/w230415_classes/PiCasino.cs @@ -0,0 +1,18 @@ +namespace w230415_classes +{ + public class PiCasino : Casino + { + + /// + /// .ctor + /// + public PiCasino() + { + GameList.Add(new CGame() { Name = "Однорукий бандит", Cash = 999000, Serial = Guid.NewGuid().ToString("N") }); + GameList.Add(new CGame() { Name = "Однорукий бандит", Cash = 929000, Serial = Guid.NewGuid().ToString("N"), IsActive = false }); + GameList.Add(new CGame() { Name = "Однорукий бандит", Cash = 929120, Serial = Guid.NewGuid().ToString("N") }); + GameList.Add(new CGame() { Name = "Однорукий бандит", Cash = 929220, Serial = Guid.NewGuid().ToString("N"), IsActive = false }); + GameList.Add(new CGame() { Name = "Однорукий бандит", Cash = 999100, Serial = Guid.NewGuid().ToString("N") }); + } + } +} \ No newline at end of file diff --git a/20230415.2/w230415_classes/w230415_business.csproj b/20230415.2/w230415_classes/w230415_business.csproj new file mode 100644 index 0000000..61ff77f --- /dev/null +++ b/20230415.2/w230415_classes/w230415_business.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/20230415.2/w230415_classes/w230415_classes.sln b/20230415.2/w230415_classes/w230415_classes.sln new file mode 100644 index 0000000..e4eedfc --- /dev/null +++ b/20230415.2/w230415_classes/w230415_classes.sln @@ -0,0 +1,49 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32616.157 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "w230415_business", "w230415_business.csproj", "{951C881B-DCFB-47AC-BF76-591A26BDB74C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "w230415_console", "..\w230415_console\w230415_console.csproj", "{9C4140B8-E40D-4FDE-B79A-A4E82AFF9BEB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "w230415_casinoapi", "..\w230415_casinoapi\w230415_casinoapi.csproj", "{31C2758A-1B74-4FAE-98DE-DBD4ECD81D68}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "w230415_consoleapi", "..\w230415_consoleapi\w230415_consoleapi.csproj", "{B317F117-1329-4F46-B2A6-47810E27F755}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "w230415_contract", "..\w230415_contract\w230415_contract.csproj", "{06CCB832-DBDA-4AAF-A1E7-3E5C4528673A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {951C881B-DCFB-47AC-BF76-591A26BDB74C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {951C881B-DCFB-47AC-BF76-591A26BDB74C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {951C881B-DCFB-47AC-BF76-591A26BDB74C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {951C881B-DCFB-47AC-BF76-591A26BDB74C}.Release|Any CPU.Build.0 = Release|Any CPU + {9C4140B8-E40D-4FDE-B79A-A4E82AFF9BEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9C4140B8-E40D-4FDE-B79A-A4E82AFF9BEB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9C4140B8-E40D-4FDE-B79A-A4E82AFF9BEB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9C4140B8-E40D-4FDE-B79A-A4E82AFF9BEB}.Release|Any CPU.Build.0 = Release|Any CPU + {31C2758A-1B74-4FAE-98DE-DBD4ECD81D68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31C2758A-1B74-4FAE-98DE-DBD4ECD81D68}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31C2758A-1B74-4FAE-98DE-DBD4ECD81D68}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31C2758A-1B74-4FAE-98DE-DBD4ECD81D68}.Release|Any CPU.Build.0 = Release|Any CPU + {B317F117-1329-4F46-B2A6-47810E27F755}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B317F117-1329-4F46-B2A6-47810E27F755}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B317F117-1329-4F46-B2A6-47810E27F755}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B317F117-1329-4F46-B2A6-47810E27F755}.Release|Any CPU.Build.0 = Release|Any CPU + {06CCB832-DBDA-4AAF-A1E7-3E5C4528673A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {06CCB832-DBDA-4AAF-A1E7-3E5C4528673A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {06CCB832-DBDA-4AAF-A1E7-3E5C4528673A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {06CCB832-DBDA-4AAF-A1E7-3E5C4528673A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9AE37ABA-A437-4CB2-B4DD-E6B0B721CA17} + EndGlobalSection +EndGlobal diff --git a/20230415.2/w230415_console/Program.cs b/20230415.2/w230415_console/Program.cs new file mode 100644 index 0000000..2c6dd65 --- /dev/null +++ b/20230415.2/w230415_console/Program.cs @@ -0,0 +1,27 @@ +using w230415_classes; +using w230415_classes.Intf; + +namespace w230415_console +{ + internal class Program + { + static void Main(string[] args) + { + ICasino c = new PiCasino(); + h_OldConsoleWork(c); + } + + private static void h_OldConsoleWork(ICasino c) + { + string s = String.Empty; + foreach ( + Game? c2 in c.GetActiveList()) + { + s = c2.Serial; + Console.WriteLine($"{c2.Serial} // {c2.IsActive}"); + } + c.RemoveGame(s); + c.AddGame("asdasd", 100); + } + } +} \ No newline at end of file diff --git a/20230415.2/w230415_console/w230415_console.csproj b/20230415.2/w230415_console/w230415_console.csproj new file mode 100644 index 0000000..03cb048 --- /dev/null +++ b/20230415.2/w230415_console/w230415_console.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/20230415.2/w230415_consoleapi/Program.cs b/20230415.2/w230415_consoleapi/Program.cs new file mode 100644 index 0000000..c74c52d --- /dev/null +++ b/20230415.2/w230415_consoleapi/Program.cs @@ -0,0 +1,35 @@ +using Newtonsoft.Json; +using w230415_classes; +using w230415_classes.Intf; + +namespace w230415_consoleapi +{ + internal class Program + { + static void Main(string[] args) + { + string url = "http://localhost:5091/v1/casino/"; + HttpClient client = new HttpClient(); + + var rr = client.GetAsync(url + "game").Result; + if (rr.StatusCode == System.Net.HttpStatusCode.OK) + { + string json = rr.Content.ReadAsStringAsync().Result; + ////1. dynamic + // dynamic dynamicObject = JsonConvert.DeserializeObject(json); + // foreach(var c2 in dynamicObject) + // { + // Console.WriteLine( + // $"{c2.serial} // {c2.isActive}"); + // } + ////2. with self types + var result = JsonConvert.DeserializeObject>(json); + foreach (var c2 in result) + { + Console.WriteLine( + $"{c2.Serial} // {c2.IsActive}"); + } + } + } + } +} \ No newline at end of file diff --git a/20230415.2/w230415_consoleapi/w230415_consoleapi.csproj b/20230415.2/w230415_consoleapi/w230415_consoleapi.csproj new file mode 100644 index 0000000..31ecdc5 --- /dev/null +++ b/20230415.2/w230415_consoleapi/w230415_consoleapi.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/20230415.2/w230415_contract/Game.cs b/20230415.2/w230415_contract/Game.cs new file mode 100644 index 0000000..97767e4 --- /dev/null +++ b/20230415.2/w230415_contract/Game.cs @@ -0,0 +1,12 @@ +using w230415_classes.Intf; + +namespace w230415_classes +{ + public class Game : IGame + { + public bool IsActive { get; set; } = true; + public double Cash { get; set; } + public string Name { get; set; } + public string Serial { get; set; } + } +} \ No newline at end of file diff --git a/20230415.2/w230415_contract/Intf/ICasino.cs b/20230415.2/w230415_contract/Intf/ICasino.cs new file mode 100644 index 0000000..47fa9b8 --- /dev/null +++ b/20230415.2/w230415_contract/Intf/ICasino.cs @@ -0,0 +1,11 @@ +namespace w230415_classes.Intf +{ + public interface ICasino + { + bool AddGame(string name, double cash); + bool ChangeGameState(string uid, bool isActive); + IEnumerable GetActiveList(); + IGame? GetGame(string uid); + bool RemoveGame(string uid); + } +} \ No newline at end of file diff --git a/20230415.2/w230415_contract/Intf/IGame.cs b/20230415.2/w230415_contract/Intf/IGame.cs new file mode 100644 index 0000000..4238173 --- /dev/null +++ b/20230415.2/w230415_contract/Intf/IGame.cs @@ -0,0 +1,10 @@ +namespace w230415_classes.Intf +{ + public interface IGame + { + double Cash { get; set; } + bool IsActive { get; set; } + string Name { get; set; } + string Serial { get; set; } + } +} \ No newline at end of file diff --git a/20230415.2/w230415_contract/w230415_contract.csproj b/20230415.2/w230415_contract/w230415_contract.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/20230415.2/w230415_contract/w230415_contract.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + -- 2.30.2