19 changed files with 469 additions and 0 deletions
@ -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<IGame> 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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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<ICasino>(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(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
{ |
||||
|
"Logging": { |
||||
|
"LogLevel": { |
||||
|
"Default": "Information", |
||||
|
"Microsoft.AspNetCore": "Warning" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
{ |
||||
|
"Logging": { |
||||
|
"LogLevel": { |
||||
|
"Default": "Information", |
||||
|
"Microsoft.AspNetCore": "Warning" |
||||
|
} |
||||
|
}, |
||||
|
"AllowedHosts": "*" |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk.Web"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>net6.0</TargetFramework> |
||||
|
<Nullable>enable</Nullable> |
||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\w230415_classes\w230415_business.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,76 @@ |
|||||
|
using w230415_classes.Intf; |
||||
|
|
||||
|
namespace w230415_classes |
||||
|
{ |
||||
|
public class Casino : ICasino |
||||
|
{ |
||||
|
protected List<CGame> GameList = new List<CGame>(); |
||||
|
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Create [C]
|
||||
|
/// </summary>
|
||||
|
/// <param name="name"></param>
|
||||
|
/// <param name="cash"></param>
|
||||
|
/// <returns></returns>
|
||||
|
public bool AddGame(string name, double cash) |
||||
|
{ |
||||
|
GameList.Add(new CGame() { Name = name, Cash = cash, Serial = Guid.NewGuid().ToString("N") }); |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Read [R] list
|
||||
|
/// </summary>
|
||||
|
/// <returns></returns>
|
||||
|
public IEnumerable<IGame> GetActiveList() |
||||
|
{ |
||||
|
List<Game> list = new List<Game>(); |
||||
|
foreach (Game? g in GameList) |
||||
|
{ |
||||
|
if (g.IsActive) |
||||
|
{ |
||||
|
list.Add(g); |
||||
|
} |
||||
|
} |
||||
|
return list; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Read [R] single
|
||||
|
/// </summary>
|
||||
|
/// <returns></returns>
|
||||
|
public IGame? GetGame(string uid) |
||||
|
{ |
||||
|
return GameList.FirstOrDefault(p => p.Serial.Equals(uid)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Update [U]
|
||||
|
/// </summary>
|
||||
|
/// <param name="uid"></param>
|
||||
|
/// <param name="isActive"></param>
|
||||
|
/// <returns></returns>
|
||||
|
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; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Delete [D] single
|
||||
|
/// </summary>
|
||||
|
/// <returns></returns>
|
||||
|
public bool RemoveGame(string uid) |
||||
|
{ |
||||
|
var pp = GameList.FirstOrDefault(p => p.Serial.Equals(uid)); |
||||
|
if (pp == null) return false; |
||||
|
return GameList.Remove(pp); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
using w230415_classes.Intf; |
||||
|
|
||||
|
namespace w230415_classes |
||||
|
{ |
||||
|
public class CGame : Game |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
namespace w230415_classes |
||||
|
{ |
||||
|
public class PiCasino : Casino |
||||
|
{ |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// .ctor
|
||||
|
/// </summary>
|
||||
|
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") }); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>net6.0</TargetFramework> |
||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||
|
<Nullable>enable</Nullable> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\w230415_contract\w230415_contract.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -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 |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<OutputType>Exe</OutputType> |
||||
|
<TargetFramework>net6.0</TargetFramework> |
||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||
|
<Nullable>enable</Nullable> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\w230415_classes\w230415_business.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -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<List<Game>>(json); |
||||
|
foreach (var c2 in result) |
||||
|
{ |
||||
|
Console.WriteLine( |
||||
|
$"{c2.Serial} // {c2.IsActive}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<OutputType>Exe</OutputType> |
||||
|
<TargetFramework>net6.0</TargetFramework> |
||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||
|
<Nullable>enable</Nullable> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\w230415_contract\w230415_contract.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -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; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
namespace w230415_classes.Intf |
||||
|
{ |
||||
|
public interface ICasino |
||||
|
{ |
||||
|
bool AddGame(string name, double cash); |
||||
|
bool ChangeGameState(string uid, bool isActive); |
||||
|
IEnumerable<IGame> GetActiveList(); |
||||
|
IGame? GetGame(string uid); |
||||
|
bool RemoveGame(string uid); |
||||
|
} |
||||
|
} |
||||
@ -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; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>net6.0</TargetFramework> |
||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||
|
<Nullable>enable</Nullable> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
</Project> |
||||
Loading…
Reference in new issue