From 12e0de336b56abdbf84eaa77b474e4d47e6ac537 Mon Sep 17 00:00:00 2001 From: revengel66 Date: Thu, 9 May 2024 15:47:34 +0300 Subject: [PATCH] rest api edit --- Pizzeria/Pizzeria.sln | 20 ++--- .../Controllers/ClientController.cs | 67 +++++++++++++++ .../Controllers/MainController.cs | 84 +++++++++++++++++++ .../PizzeriaRestApi2/PizzeriaRestApi.csproj | 20 +++++ Pizzeria/PizzeriaRestApi2/Program.cs | 36 ++++++++ .../Properties/launchSettings.json | 31 +++++++ .../appsettings.Development.json | 8 ++ Pizzeria/PizzeriaRestApi2/appsettings.json | 9 ++ Pizzeria/PizzeriaRestApi2/log4net.config | 16 ++++ 9 files changed, 280 insertions(+), 11 deletions(-) create mode 100644 Pizzeria/PizzeriaRestApi2/Controllers/ClientController.cs create mode 100644 Pizzeria/PizzeriaRestApi2/Controllers/MainController.cs create mode 100644 Pizzeria/PizzeriaRestApi2/PizzeriaRestApi.csproj create mode 100644 Pizzeria/PizzeriaRestApi2/Program.cs create mode 100644 Pizzeria/PizzeriaRestApi2/Properties/launchSettings.json create mode 100644 Pizzeria/PizzeriaRestApi2/appsettings.Development.json create mode 100644 Pizzeria/PizzeriaRestApi2/appsettings.json create mode 100644 Pizzeria/PizzeriaRestApi2/log4net.config diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 93d4764..5538f3c 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -17,15 +17,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaFileImplement", "Pi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaRestApi", "PizzeriaRestApi\PizzeriaRestApi.csproj", "{47B41018-72A8-4D32-AD04-694CBCD62B57}" - ProjectSection(ProjectDependencies) = postProject - {16EC5652-8348-4520-9736-1889B2749416} = {16EC5652-8348-4520-9736-1889B2749416} - {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C} = {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C} - {F039417A-21F0-4947-9623-C89A755AB8DD} = {F039417A-21F0-4947-9623-C89A755AB8DD} - EndProjectSection -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaClientApp", "PizzeriaClientApp\PizzeriaClientApp.csproj", "{431FCB4B-6315-4FC3-A88A-9ECFBF1D3763}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaRestApi", "PizzeriaRestApi2\PizzeriaRestApi.csproj", "{DB5EBCD0-2BBF-4EDB-A59F-CA2F0496C56F}" + ProjectSection(ProjectDependencies) = postProject + {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C} = {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -60,14 +58,14 @@ Global {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Debug|Any CPU.Build.0 = Debug|Any CPU {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Release|Any CPU.ActiveCfg = Release|Any CPU {31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Release|Any CPU.Build.0 = Release|Any CPU - {47B41018-72A8-4D32-AD04-694CBCD62B57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {47B41018-72A8-4D32-AD04-694CBCD62B57}.Debug|Any CPU.Build.0 = Debug|Any CPU - {47B41018-72A8-4D32-AD04-694CBCD62B57}.Release|Any CPU.ActiveCfg = Release|Any CPU - {47B41018-72A8-4D32-AD04-694CBCD62B57}.Release|Any CPU.Build.0 = Release|Any CPU {431FCB4B-6315-4FC3-A88A-9ECFBF1D3763}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {431FCB4B-6315-4FC3-A88A-9ECFBF1D3763}.Debug|Any CPU.Build.0 = Debug|Any CPU {431FCB4B-6315-4FC3-A88A-9ECFBF1D3763}.Release|Any CPU.ActiveCfg = Release|Any CPU {431FCB4B-6315-4FC3-A88A-9ECFBF1D3763}.Release|Any CPU.Build.0 = Release|Any CPU + {DB5EBCD0-2BBF-4EDB-A59F-CA2F0496C56F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DB5EBCD0-2BBF-4EDB-A59F-CA2F0496C56F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DB5EBCD0-2BBF-4EDB-A59F-CA2F0496C56F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DB5EBCD0-2BBF-4EDB-A59F-CA2F0496C56F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/PizzeriaRestApi2/Controllers/ClientController.cs b/Pizzeria/PizzeriaRestApi2/Controllers/ClientController.cs new file mode 100644 index 0000000..d8a5f21 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi2/Controllers/ClientController.cs @@ -0,0 +1,67 @@ +using Microsoft.AspNetCore.Mvc; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaRestApi.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/Pizzeria/PizzeriaRestApi2/Controllers/MainController.cs b/Pizzeria/PizzeriaRestApi2/Controllers/MainController.cs new file mode 100644 index 0000000..218c582 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi2/Controllers/MainController.cs @@ -0,0 +1,84 @@ +using DocumentFormat.OpenXml.Office2010.Excel; +using Microsoft.AspNetCore.Mvc; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly IPizzaLogic _pizza; + public MainController(ILogger logger, IOrderLogic order, IPizzaLogic pizza) + { + _logger = logger; + _order = order; + _pizza = pizza; + } + [HttpGet] + public List? GetPizzaList() + { + try + { + return _pizza.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка продуктов"); + throw; + } + } + [HttpGet] + public PizzaViewModel? GetPizza(int pizzaId) + { + try + { + return _pizza.ReadElement(new PizzaSearchModel + { + Id = + pizzaId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения продукта по id={Id}", + pizzaId); + 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; + } + } + } +} diff --git a/Pizzeria/PizzeriaRestApi2/PizzeriaRestApi.csproj b/Pizzeria/PizzeriaRestApi2/PizzeriaRestApi.csproj new file mode 100644 index 0000000..bb046f5 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi2/PizzeriaRestApi.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + diff --git a/Pizzeria/PizzeriaRestApi2/Program.cs b/Pizzeria/PizzeriaRestApi2/Program.cs new file mode 100644 index 0000000..b7636b6 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi2/Program.cs @@ -0,0 +1,36 @@ +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaDatabaseImplement.Implements; +using PizzeriaContracts.StorageContracts; +using PizzeriaBusinessLogic.BusinessLogic; +using Microsoft.OpenApi.Models; + +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 = "PizzeriaRestApi", 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", "PizzeriaRestApi v1")); +} +app.UseHttpsRedirection(); +app.UseAuthorization(); +app.MapControllers(); +app.Run(); + + diff --git a/Pizzeria/PizzeriaRestApi2/Properties/launchSettings.json b/Pizzeria/PizzeriaRestApi2/Properties/launchSettings.json new file mode 100644 index 0000000..b07365d --- /dev/null +++ b/Pizzeria/PizzeriaRestApi2/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:37303", + "sslPort": 44315 + } + }, + "profiles": { + "PizzeriaRestApi2": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7291;http://localhost:5166", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Pizzeria/PizzeriaRestApi2/appsettings.Development.json b/Pizzeria/PizzeriaRestApi2/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Pizzeria/PizzeriaRestApi2/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Pizzeria/PizzeriaRestApi2/appsettings.json b/Pizzeria/PizzeriaRestApi2/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Pizzeria/PizzeriaRestApi2/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Pizzeria/PizzeriaRestApi2/log4net.config b/Pizzeria/PizzeriaRestApi2/log4net.config new file mode 100644 index 0000000..8f1290c --- /dev/null +++ b/Pizzeria/PizzeriaRestApi2/log4net.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file