diff --git a/TravelCompany/TravelCompany.sln b/TravelCompany/TravelCompany.sln index d754ce1..58f4dac 100644 --- a/TravelCompany/TravelCompany.sln +++ b/TravelCompany/TravelCompany.sln @@ -13,9 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyBusinessLogic" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyListImplement", "TravelCompanyListImplement\TravelCompanyListImplement.csproj", "{9D85E556-67A1-44D5-B46B-5C880935118D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelCompanyFileImplement", "TracelCompanyFileImplement\TravelCompanyFileImplement.csproj", "{9DB1A97D-B2AA-43B4-94DF-2A3B715B4235}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyFileImplement", "TracelCompanyFileImplement\TravelCompanyFileImplement.csproj", "{9DB1A97D-B2AA-43B4-94DF-2A3B715B4235}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelCompanyDatabaseImplement", "TravelCompanyDatabaseImplement\TravelCompanyDatabaseImplement.csproj", "{CAFAE45A-519C-41A6-89EE-B62682DFDECF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelCompanyDatabaseImplement", "TravelCompanyDatabaseImplement\TravelCompanyDatabaseImplement.csproj", "{CAFAE45A-519C-41A6-89EE-B62682DFDECF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelCompanyRestApi", "TravelCompanyRestApi\TravelCompanyRestApi.csproj", "{ED6168BB-64B4-4D12-B1F1-4867549FB0F1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -51,6 +53,10 @@ Global {CAFAE45A-519C-41A6-89EE-B62682DFDECF}.Debug|Any CPU.Build.0 = Debug|Any CPU {CAFAE45A-519C-41A6-89EE-B62682DFDECF}.Release|Any CPU.ActiveCfg = Release|Any CPU {CAFAE45A-519C-41A6-89EE-B62682DFDECF}.Release|Any CPU.Build.0 = Release|Any CPU + {ED6168BB-64B4-4D12-B1F1-4867549FB0F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ED6168BB-64B4-4D12-B1F1-4867549FB0F1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ED6168BB-64B4-4D12-B1F1-4867549FB0F1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ED6168BB-64B4-4D12-B1F1-4867549FB0F1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/ClientLogic.cs b/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/ClientLogic.cs new file mode 100644 index 0000000..d5c0349 --- /dev/null +++ b/TravelCompany/TravelCompanyBusinessLogic/BusinessLogic/ClientLogic.cs @@ -0,0 +1,125 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyContracts.BusinessLogicContracts; +using TravelCompanyContracts.SearchModels; + + +namespace TravelCompanyBusinessLogic.BusinessLogic +{ + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; + + public ClientLogic(ILogger logger, IClientStorage clientStorage) + { + _logger = logger; + _clientStorage = clientStorage; + } + + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. ClientId:{Id}", model?.Id); + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ClientFio:{ClientFio}.Id:{ Id}", model.ClientFIO, model.Id); + var element = _clientStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ClientBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_clientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ClientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ClientFIO)) + { + throw new ArgumentNullException("Нет ФИО пользователя", nameof(model.ClientFIO)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет почты пользователя", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); + } + _logger.LogInformation("Client. ClientFIO:{ClientFIO}.Email:{Email}.Password:{Password}.Id:{Id}", + model.ClientFIO, model.Email, model.Password, model.Id); + var element = _clientStorage.GetElement(new ClientSearchModel + { + ClientFIO = model.ClientFIO + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким именем уже есть"); + } + } + } +} diff --git a/TravelCompany/TravelCompanyContracts/BindingModels/ClientBindingModel.cs b/TravelCompany/TravelCompanyContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..e27fea7 --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyDataModels.Models; + +namespace TravelCompanyContracts.BindingModels +{ + public class ClientBindingModel : IClientModel + { + public int Id { get; set; } + public string ClientFIO { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + } +} diff --git a/TravelCompany/TravelCompanyContracts/BusinessLogicContracts/IClientLogic.cs b/TravelCompany/TravelCompanyContracts/BusinessLogicContracts/IClientLogic.cs new file mode 100644 index 0000000..3aaad25 --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/BusinessLogicContracts/IClientLogic.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.ViewModels; + + +namespace TravelCompanyContracts.BusinessLogicContracts +{ + public interface IClientLogic + { + List? ReadList(ClientSearchModel? model); + ClientViewModel? ReadElement(ClientSearchModel model); + bool Create(ClientBindingModel model); + bool Update(ClientBindingModel model); + bool Delete(ClientBindingModel model); + } +} diff --git a/TravelCompany/TravelCompanyContracts/SearchModels/ClientSearchModel.cs b/TravelCompany/TravelCompanyContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..9834dee --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyContracts.SearchModels +{ + public class ClientSearchModel + { + public int? Id { get; set; } + + public string? ClientFIO { get; set; } + + public string? Email { get; set; } + + public string? Password { get; set; } + } +} diff --git a/TravelCompany/TravelCompanyContracts/StoragesContracts/IClientStorage.cs b/TravelCompany/TravelCompanyContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..16d44e3 --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.ViewModels; +using TravelCompanyContracts.SearchModels; + + +namespace TravelCompanyContracts.StoragesContracts +{ + public interface IClientStorage + { + List GetFullList(); + List GetFilteredList(ClientSearchModel model); + ClientViewModel? GetElement(ClientSearchModel model); + ClientViewModel? Insert(ClientBindingModel model); + ClientViewModel? Update(ClientBindingModel model); + ClientViewModel? Delete(ClientBindingModel model); + } +} diff --git a/TravelCompany/TravelCompanyContracts/ViewModels/ClientViewModel.cs b/TravelCompany/TravelCompanyContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..b4293ad --- /dev/null +++ b/TravelCompany/TravelCompanyContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelCompanyDataModels.Models; + +namespace TravelCompanyContracts.ViewModels +{ + public class ClientViewModel : IClientModel + { + public int Id { get; set; } + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + [DisplayName("Логин (эл. почта)")] + public string Email { get; set; } = string.Empty; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + } +} diff --git a/TravelCompany/TravelCompanyDataModels/Models/IClientModel.cs b/TravelCompany/TravelCompanyDataModels/Models/IClientModel.cs new file mode 100644 index 0000000..f1d8725 --- /dev/null +++ b/TravelCompany/TravelCompanyDataModels/Models/IClientModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TravelCompanyDataModels.Models +{ + public interface IClientModel : IId + { + string ClientFIO { get; } + string Email { get; } + string Password { get; } + } +} diff --git a/TravelCompany/TravelCompanyRestApi/Controllers/ClientController.cs b/TravelCompany/TravelCompanyRestApi/Controllers/ClientController.cs new file mode 100644 index 0000000..cbbb675 --- /dev/null +++ b/TravelCompany/TravelCompanyRestApi/Controllers/ClientController.cs @@ -0,0 +1,67 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.BusinessLogicContracts; +using TravelCompanyContracts.BusinessLogicsContracts; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.ViewModels; + +namespace TravelCompanyRestApi.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/TravelCompany/TravelCompanyRestApi/Controllers/MainController.cs b/TravelCompany/TravelCompanyRestApi/Controllers/MainController.cs new file mode 100644 index 0000000..48d7f9a --- /dev/null +++ b/TravelCompany/TravelCompanyRestApi/Controllers/MainController.cs @@ -0,0 +1,89 @@ +using DocumentFormat.OpenXml.Office2010.Excel; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using TravelCompanyContracts.BindingModels; +using TravelCompanyContracts.BusinessLogicContracts; +using TravelCompanyContracts.BusinessLogicsContracts; +using TravelCompanyContracts.SearchModels; +using TravelCompanyContracts.ViewModels; + +namespace TravelCompanyRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class MainController : Controller + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly ITravelLogic _travel; + public MainController(ILogger logger, IOrderLogic order, +ITravelLogic product) + { + _logger = logger; + _order = order; + _travel = product; + } + [HttpGet] + public List? GetProductList() + { + try + { + return _travel.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка продуктов"); + throw; + } + } + [HttpGet] + public TravelViewModel? GetProduct(int productId) + { + try + { + return _travel.ReadElement(new TravelSearchModel + { + Id = + productId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения продукта по id={Id}", + productId); + 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/TravelCompany/TravelCompanyRestApi/Controllers/WeatherForecastController.cs b/TravelCompany/TravelCompanyRestApi/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..1a63b8e --- /dev/null +++ b/TravelCompany/TravelCompanyRestApi/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace TravelCompanyRestApi.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 _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} \ No newline at end of file diff --git a/TravelCompany/TravelCompanyRestApi/Program.cs b/TravelCompany/TravelCompanyRestApi/Program.cs new file mode 100644 index 0000000..e9d7556 --- /dev/null +++ b/TravelCompany/TravelCompanyRestApi/Program.cs @@ -0,0 +1,44 @@ +using TravelCompanyBusinessLogic.BusinessLogic; +using TravelCompanyContracts.BusinessLogicsContracts; +using TravelCompanyContracts.StoragesContracts; +using TravelCompanyDatabaseImplement.Implements; +using Microsoft.OpenApi.Models; +using TravelCompanyContracts.BusinessLogicContracts; + +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 = "AbstractShopRestApi", + 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", + "AbstractShopRestApi v1")); +} +app.UseHttpsRedirection(); +app.UseAuthorization(); +app.MapControllers(); +app.Run(); \ No newline at end of file diff --git a/TravelCompany/TravelCompanyRestApi/Properties/launchSettings.json b/TravelCompany/TravelCompanyRestApi/Properties/launchSettings.json new file mode 100644 index 0000000..db083d1 --- /dev/null +++ b/TravelCompany/TravelCompanyRestApi/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:49683", + "sslPort": 44302 + } + }, + "profiles": { + "TravelCompanyRestApi": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7165;http://localhost:5256", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/TravelCompany/TravelCompanyRestApi/TravelCompanyRestApi.csproj b/TravelCompany/TravelCompanyRestApi/TravelCompanyRestApi.csproj new file mode 100644 index 0000000..383b222 --- /dev/null +++ b/TravelCompany/TravelCompanyRestApi/TravelCompanyRestApi.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/TravelCompany/TravelCompanyRestApi/WeatherForecast.cs b/TravelCompany/TravelCompanyRestApi/WeatherForecast.cs new file mode 100644 index 0000000..3c45d54 --- /dev/null +++ b/TravelCompany/TravelCompanyRestApi/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace TravelCompanyRestApi +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} \ No newline at end of file diff --git a/TravelCompany/TravelCompanyRestApi/appsettings.Development.json b/TravelCompany/TravelCompanyRestApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/TravelCompany/TravelCompanyRestApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/TravelCompany/TravelCompanyRestApi/appsettings.json b/TravelCompany/TravelCompanyRestApi/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/TravelCompany/TravelCompanyRestApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}