commit
211b372219
20 changed files with 504 additions and 0 deletions
@ -0,0 +1,3 @@ |
|||
bin/ |
|||
obj/ |
|||
.vs/ |
|||
@ -0,0 +1,60 @@ |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace WebApplication1.Controllers |
|||
{ |
|||
[Route("api/v2/[controller]")]
|
|||
[ApiController] |
|||
public class Field2Controller : ControllerBase |
|||
{ |
|||
private IFieldManager _fm; |
|||
private ILogger<FieldController> _lg; |
|||
|
|||
/// <summary>
|
|||
/// .ctor
|
|||
/// </summary>
|
|||
/// <param name="fm"></param>
|
|||
public Field2Controller( |
|||
IFieldManager fm, |
|||
ILogger<FieldController> lg) |
|||
{ |
|||
_fm = fm; |
|||
_lg = lg; |
|||
} |
|||
|
|||
|
|||
|
|||
[HttpPost(Name = "new")] |
|||
[Route("new")] |
|||
public Field StartNewGame() |
|||
{ |
|||
return _fm.StartNewGame(); |
|||
} |
|||
[HttpPost(Name = "move")] |
|||
[Route("move")] |
|||
public Field Move(string identifier) |
|||
{ |
|||
return _fm.Move(identifier); |
|||
} |
|||
|
|||
[HttpPost(Name = "move2")] |
|||
[Route("move2")] |
|||
public Field Move2(string identifier) |
|||
{ |
|||
return _fm.Move(identifier); |
|||
} |
|||
|
|||
[HttpGet(Name = "list")] |
|||
[Route("list")] |
|||
public Field List(string identifier) |
|||
{ |
|||
return _fm.List(identifier); |
|||
} |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace WebApplication1.Controllers |
|||
{ |
|||
[Route("api/v1/[controller]")]
|
|||
[ApiController] |
|||
public class FieldController : ControllerBase |
|||
{ |
|||
private IFieldManager _fm; |
|||
private ILogger<FieldController> _lg; |
|||
|
|||
/// <summary>
|
|||
/// .ctor
|
|||
/// </summary>
|
|||
/// <param name="fm"></param>
|
|||
public FieldController( |
|||
IFieldManager fm, |
|||
ILogger<FieldController> lg) |
|||
{ |
|||
_fm = fm; |
|||
_lg = lg; |
|||
} |
|||
|
|||
|
|||
|
|||
[HttpPost(Name = "new")] |
|||
[Route("new")] |
|||
public Field StartNewGame() |
|||
{ |
|||
return _fm.StartNewGame(); |
|||
} |
|||
[HttpPost(Name = "move")] |
|||
[Route("move")] |
|||
public Field Move(string identifier) |
|||
{ |
|||
return _fm.Move(identifier); |
|||
} |
|||
|
|||
[HttpGet(Name = "list")] |
|||
[Route("list")] |
|||
public Field List(string identifier) |
|||
{ |
|||
return _fm.List(identifier); |
|||
} |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace WebApplication1.Controllers |
|||
{ |
|||
[ApiController] |
|||
[Route("[controller]")]
|
|||
public class WeatherForecastController : ControllerBase |
|||
{ |
|||
private static readonly string[] Summaries = new[] |
|||
{ |
|||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" |
|||
}; |
|||
|
|||
private readonly ILogger<WeatherForecastController> _logger; |
|||
|
|||
public WeatherForecastController(ILogger<WeatherForecastController> logger) |
|||
{ |
|||
_logger = logger; |
|||
} |
|||
|
|||
[HttpGet] |
|||
public IEnumerable<WeatherForecast> Get() |
|||
{ |
|||
var rng = new Random(); |
|||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast |
|||
{ |
|||
Date = DateTime.Now.AddDays(index), |
|||
TemperatureC = rng.Next(-20, 55), |
|||
SummaryWeather = Summaries[rng.Next(Summaries.Length)] |
|||
}) |
|||
.ToArray(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
|
|||
namespace WebApplication1 |
|||
{ |
|||
|
|||
public class Field |
|||
{ |
|||
public string identifier { get; } = Guid.NewGuid().ToString("N"); |
|||
public List<Figure> Figures { get; } = new List<Figure>(); |
|||
public bool MoveRandomFigure() |
|||
{ |
|||
Random rr = new Random(); |
|||
int iPos = rr.Next(Figures.Count()); |
|||
return FigureMover.Move(Figures[iPos]); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace WebApplication1 |
|||
{ |
|||
|
|||
public class FieldManager : IFieldManager |
|||
{ |
|||
private List<Field> _fields = new List<Field>(); |
|||
|
|||
public Field StartNewGame() |
|||
{ |
|||
var ff = new RandomField(2); |
|||
_fields.Add(ff); |
|||
return ff; |
|||
} |
|||
public Field Move(string identifier) |
|||
{ |
|||
var ff = _fields.FirstOrDefault(f => f.identifier.Equals(identifier)); |
|||
if (ff == null) return null; |
|||
ff.MoveRandomFigure(); |
|||
return ff; |
|||
} |
|||
|
|||
public Field List(string identifier) |
|||
{ |
|||
var ff = _fields.FirstOrDefault(f => f.identifier.Equals(identifier)); |
|||
if (ff == null) return null; |
|||
return ff; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
namespace WebApplication1 |
|||
{ |
|||
public class Figure |
|||
{ |
|||
public int FigureColor { get; set; } |
|||
public int FigureType { get; set; } |
|||
public int PosX { get; set; } |
|||
public int PosY { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
namespace WebApplication1 |
|||
{ |
|||
public static class FigureMover |
|||
{ |
|||
public static readonly int FieldYMin = 0; |
|||
public static readonly int FieldYMax = 7; |
|||
public static readonly int FieldXMin = 0; |
|||
public static readonly int FieldXMax = 7; |
|||
|
|||
public static bool Move(Figure figure) |
|||
{ |
|||
if (figure.FigureType == 1) |
|||
{ |
|||
return h_MoveSimple(figure); |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
private static bool h_MoveSimple(Figure figure) |
|||
{ |
|||
bool bDirectionUp = false; |
|||
if (figure.FigureColor == 1) |
|||
{ |
|||
bDirectionUp = true; |
|||
} |
|||
int iInc = bDirectionUp ? 1 : -1; |
|||
figure.PosY += iInc; |
|||
if (figure.PosY < FieldYMin) |
|||
{ |
|||
figure.PosY = FieldYMin; |
|||
return false; |
|||
} |
|||
if (figure.PosY > FieldYMax) |
|||
{ |
|||
figure.PosY = FieldYMax; |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
namespace WebApplication1 |
|||
{ |
|||
public interface IFieldManager |
|||
{ |
|||
public Field StartNewGame(); |
|||
Field Move(string identifier); |
|||
Field List(string identifier); |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using System; |
|||
|
|||
namespace WebApplication1 |
|||
{ |
|||
public class RandomField : Field |
|||
{ |
|||
public RandomField(int iCount) |
|||
{ |
|||
Random rr = new Random(); |
|||
for (int ii = 0; ii < iCount; ii++) |
|||
{ |
|||
this.Figures.Add(new Figure() |
|||
{ |
|||
FigureColor = rr.Next(2), |
|||
FigureType = rr.Next(1), |
|||
PosX = rr.Next(FigureMover.FieldXMin, FigureMover.FieldXMax), |
|||
PosY = rr.Next(FigureMover.FieldYMin, FigureMover.FieldYMax), |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace WebApplication1 |
|||
{ |
|||
public class Program |
|||
{ |
|||
public static void Main(string[] args) |
|||
{ |
|||
CreateHostBuilder(args) |
|||
.Build() |
|||
.Run(); |
|||
} |
|||
|
|||
public static IHostBuilder CreateHostBuilder(string[] args) => |
|||
Host.CreateDefaultBuilder(args) |
|||
.ConfigureWebHostDefaults(webBuilder => |
|||
{ |
|||
webBuilder.UseStartup<Startup>(); |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
{ |
|||
"$schema": "http://json.schemastore.org/launchsettings.json", |
|||
"iisSettings": { |
|||
"windowsAuthentication": false, |
|||
"anonymousAuthentication": true, |
|||
"iisExpress": { |
|||
"applicationUrl": "http://localhost:49746" |
|||
} |
|||
}, |
|||
"profiles": { |
|||
"IIS Express": { |
|||
"commandName": "IISExpress", |
|||
"launchBrowser": true, |
|||
"launchUrl": "swagger", |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
} |
|||
}, |
|||
"WebApplication1": { |
|||
"commandName": "Project", |
|||
"dotnetRunMessages": "true", |
|||
"launchBrowser": true, |
|||
"launchUrl": "swagger", |
|||
"applicationUrl": "http://localhost:5000", |
|||
"environmentVariables": { |
|||
"ASPNETCORE_ENVIRONMENT": "Development" |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.AspNetCore.HttpsPolicy; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.OpenApi.Models; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace WebApplication1 |
|||
{ |
|||
public class Startup |
|||
{ |
|||
public Startup(IConfiguration configuration) |
|||
{ |
|||
Configuration = configuration; |
|||
} |
|||
|
|||
public IConfiguration Configuration { get; } |
|||
|
|||
// This method gets called by the runtime. Use this method to add services to the container.
|
|||
public void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
|
|||
services.AddControllers(); |
|||
|
|||
services.AddSingleton<IFieldManager>(new FieldManager()); |
|||
|
|||
services.AddSwaggerGen(c => |
|||
{ |
|||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication1", Version = "v1" }); |
|||
}); |
|||
} |
|||
|
|||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
|||
{ |
|||
if (env.IsDevelopment()) |
|||
{ |
|||
app.UseDeveloperExceptionPage(); |
|||
app.UseSwagger(); |
|||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication1 v1")); |
|||
} |
|||
|
|||
app.UseHttpsRedirection(); |
|||
|
|||
app.UseRouting(); |
|||
|
|||
app.UseAuthorization(); |
|||
|
|||
app.UseEndpoints(endpoints => |
|||
{ |
|||
endpoints.MapControllers(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
# Задача |
|||
Продемонстрировать работающий сервис с реализацией логики вне контроллера |
|||
|
|||
# Алгоритм решения задачи |
|||
- Создать пустое веб-апи сервис на основе шаблона |
|||
- Создать класс, осуществляющий прогноз погоды на следующий день по предыдущему |
|||
- Встроить класс в сервисную коллекцию (Singleton) |
|||
- Получить класс через Dependency Injection (конструктор) |
|||
- Использовать класс для выдачи результата |
|||
|
|||
-- SET PATH=%PATH%;"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\mingw32\bin" |
|||
@ -0,0 +1,15 @@ |
|||
using System; |
|||
|
|||
namespace WebApplication1 |
|||
{ |
|||
public class WeatherForecast |
|||
{ |
|||
public DateTime Date { get; set; } |
|||
|
|||
public int TemperatureC { get; set; } |
|||
|
|||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); |
|||
|
|||
public string SummaryWeather { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk.Web"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net5.0</TargetFramework> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,7 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<PropertyGroup> |
|||
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID> |
|||
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath> |
|||
</PropertyGroup> |
|||
</Project> |
|||
@ -0,0 +1,31 @@ |
|||
|
|||
Microsoft Visual Studio Solution File, Format Version 12.00 |
|||
# Visual Studio Version 16 |
|||
VisualStudioVersion = 16.0.30709.132 |
|||
MinimumVisualStudioVersion = 10.0.40219.1 |
|||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication1", "WebApplication1.csproj", "{1E41DB1E-938D-4030-B8C7-B90C64A7B6CC}" |
|||
EndProject |
|||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp2", "..\ConsoleApp2\ConsoleApp2.csproj", "{95D5C921-BDC7-4A88-AAAD-EF09B81221EF}" |
|||
EndProject |
|||
Global |
|||
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
|||
Debug|Any CPU = Debug|Any CPU |
|||
Release|Any CPU = Release|Any CPU |
|||
EndGlobalSection |
|||
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
|||
{1E41DB1E-938D-4030-B8C7-B90C64A7B6CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|||
{1E41DB1E-938D-4030-B8C7-B90C64A7B6CC}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|||
{1E41DB1E-938D-4030-B8C7-B90C64A7B6CC}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|||
{1E41DB1E-938D-4030-B8C7-B90C64A7B6CC}.Release|Any CPU.Build.0 = Release|Any CPU |
|||
{95D5C921-BDC7-4A88-AAAD-EF09B81221EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|||
{95D5C921-BDC7-4A88-AAAD-EF09B81221EF}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|||
{95D5C921-BDC7-4A88-AAAD-EF09B81221EF}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|||
{95D5C921-BDC7-4A88-AAAD-EF09B81221EF}.Release|Any CPU.Build.0 = Release|Any CPU |
|||
EndGlobalSection |
|||
GlobalSection(SolutionProperties) = preSolution |
|||
HideSolutionNode = FALSE |
|||
EndGlobalSection |
|||
GlobalSection(ExtensibilityGlobals) = postSolution |
|||
SolutionGuid = {F485B05F-F662-4D8B-8E28-EB57AA152B44} |
|||
EndGlobalSection |
|||
EndGlobal |
|||
@ -0,0 +1,9 @@ |
|||
{ |
|||
"Logging": { |
|||
"LogLevel": { |
|||
"Default": "Information", |
|||
"Microsoft": "Warning", |
|||
"Microsoft.Hosting.Lifetime": "Information" |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
{ |
|||
"Logging": { |
|||
"LogLevel": { |
|||
"Default": "Information", |
|||
"Microsoft": "Warning", |
|||
"Microsoft.Hosting.Lifetime": "Information" |
|||
} |
|||
}, |
|||
"AllowedHosts": "*" |
|||
} |
|||
Loading…
Reference in new issue