From d98443127088751728a58d97219f125683087f23 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sat, 6 Apr 2024 16:22:40 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B5=D0=BA=D1=82=20RestApi,=20=D0=B4=D0=B2=D0=B0?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B?= =?UTF-8?q?=20-=20Main=20=D0=B8=20Client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBarDatabaseImplement/Models/Order.cs | 2 +- .../Controllers/ClientController.cs | 65 +++++++++++++++ SushiBarRestApi/Controllers/MainController.cs | 82 +++++++++++++++++++ SushiBarRestApi/Program.cs | 41 ++++++++++ .../Properties/launchSettings.json | 41 ++++++++++ SushiBarRestApi/SushiBarRestApi.csproj | 20 +++++ SushiBarRestApi/SushiBarRestApi.http | 6 ++ SushiBarRestApi/WeatherForecast.cs | 13 +++ SushiBarRestApi/appsettings.Development.json | 8 ++ SushiBarRestApi/appsettings.json | 9 ++ SushiBarRestApi/log4net.config | 16 ++++ 11 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 SushiBarRestApi/Controllers/ClientController.cs create mode 100644 SushiBarRestApi/Controllers/MainController.cs create mode 100644 SushiBarRestApi/Program.cs create mode 100644 SushiBarRestApi/Properties/launchSettings.json create mode 100644 SushiBarRestApi/SushiBarRestApi.csproj create mode 100644 SushiBarRestApi/SushiBarRestApi.http create mode 100644 SushiBarRestApi/WeatherForecast.cs create mode 100644 SushiBarRestApi/appsettings.Development.json create mode 100644 SushiBarRestApi/appsettings.json create mode 100644 SushiBarRestApi/log4net.config diff --git a/SushiBarDatabaseImplement/Models/Order.cs b/SushiBarDatabaseImplement/Models/Order.cs index 967457b..b32f22c 100644 --- a/SushiBarDatabaseImplement/Models/Order.cs +++ b/SushiBarDatabaseImplement/Models/Order.cs @@ -60,7 +60,7 @@ namespace SushiBarDatabaseImplement.Models Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, - ClientId = ClientId + ClientId = ClientId, }; } } diff --git a/SushiBarRestApi/Controllers/ClientController.cs b/SushiBarRestApi/Controllers/ClientController.cs new file mode 100644 index 0000000..70791d7 --- /dev/null +++ b/SushiBarRestApi/Controllers/ClientController.cs @@ -0,0 +1,65 @@ +using Microsoft.AspNetCore.Mvc; +using SushiBarContracts.BindingModel; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModel; +using SushiBarContracts.ViewModels; + +namespace SushiBarRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ClientController : Controller + { + private readonly ILogger _logger; + private readonly IClientLogic _logic; + public ClientController(IClientLogic logic, ILogger + logger) + { + _logger = logger; + _logic = logic; + } + [HttpGet] + public ClientViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new ClientSearchModel + { + Email = login, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка входа в систему"); + throw; + } + } + [HttpPost] + public void Register(ClientBindingModel model) + { + try + { + _logic.Create(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка регистрации"); + throw; + } + } + [HttpPost] + public void UpdateData(ClientBindingModel model) + { + try + { + _logic.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных"); + throw; + } + } + } +} diff --git a/SushiBarRestApi/Controllers/MainController.cs b/SushiBarRestApi/Controllers/MainController.cs new file mode 100644 index 0000000..33b9171 --- /dev/null +++ b/SushiBarRestApi/Controllers/MainController.cs @@ -0,0 +1,82 @@ +using DocumentFormat.OpenXml.Office2010.Excel; +using Microsoft.AspNetCore.Mvc; +using SushiBarContracts.BindingModel; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModel; +using SushiBarContracts.ViewModels; + +namespace SushiBarRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly ISushiLogic _sushi; + public MainController(ILogger logger, IOrderLogic order, ISushiLogic sushi) + { + _logger = logger; + _order = order; + _sushi = sushi; + } + [HttpGet] + public List? GetProductList() + { + try + { + return _sushi.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка продуктов"); + throw; + } + } + [HttpGet] + public SushiViewModel? GetProduct(int sushiId) + { + try + { + return _sushi.ReadElement(new SushiSearchModel + { + Id = sushiId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения продукта по id={Id}", sushiId); + throw; + } + } + [HttpGet] + public List? GetOrders(int clientId) + { + try + { + return _order.ReadList(new OrderSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка заказов клиента id = {Id} ", clientId); + throw; + } + } + [HttpPost] + public void CreateOrder(OrderBindingModel model) + { + try + { + _order.CreateOrder(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания заказа"); + throw; + } + } + } +} \ No newline at end of file diff --git a/SushiBarRestApi/Program.cs b/SushiBarRestApi/Program.cs new file mode 100644 index 0000000..27863ad --- /dev/null +++ b/SushiBarRestApi/Program.cs @@ -0,0 +1,41 @@ +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.StoragesContracts; +using SushiBarDatabaseImplement.Implements; +using Microsoft.OpenApi.Models; +using SushiBarBusinessLogic.BusinessLogic; +using SushiBarBusinessLogic; +var builder = WebApplication.CreateBuilder(args); +builder.Logging.SetMinimumLevel(LogLevel.Trace); +builder.Logging.AddLog4Net("log4net.config"); +// Add services to the container. +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at +https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "SushiBarRestApi", + Version + = "v1" + }); +}); +var app = builder.Build(); +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", + "SushiBarRestApi v1")); +} +app.UseHttpsRedirection(); +app.UseAuthorization(); +app.MapControllers(); +app.Run(); \ No newline at end of file diff --git a/SushiBarRestApi/Properties/launchSettings.json b/SushiBarRestApi/Properties/launchSettings.json new file mode 100644 index 0000000..c491962 --- /dev/null +++ b/SushiBarRestApi/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:2876", + "sslPort": 44332 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5262", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7106;http://localhost:5262", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/SushiBarRestApi/SushiBarRestApi.csproj b/SushiBarRestApi/SushiBarRestApi.csproj new file mode 100644 index 0000000..830945e --- /dev/null +++ b/SushiBarRestApi/SushiBarRestApi.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + diff --git a/SushiBarRestApi/SushiBarRestApi.http b/SushiBarRestApi/SushiBarRestApi.http new file mode 100644 index 0000000..9a80072 --- /dev/null +++ b/SushiBarRestApi/SushiBarRestApi.http @@ -0,0 +1,6 @@ +@SushiBarRestApi_HostAddress = http://localhost:5262 + +GET {{SushiBarRestApi_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/SushiBarRestApi/WeatherForecast.cs b/SushiBarRestApi/WeatherForecast.cs new file mode 100644 index 0000000..a7f2dfa --- /dev/null +++ b/SushiBarRestApi/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace SushiBarRestApi +{ + public class WeatherForecast + { + public DateOnly Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} diff --git a/SushiBarRestApi/appsettings.Development.json b/SushiBarRestApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/SushiBarRestApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/SushiBarRestApi/appsettings.json b/SushiBarRestApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/SushiBarRestApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/SushiBarRestApi/log4net.config b/SushiBarRestApi/log4net.config new file mode 100644 index 0000000..fe2e7b9 --- /dev/null +++ b/SushiBarRestApi/log4net.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file