diff --git a/AndDietCokeProject/AndDietCokeBuisnessLogic/AndDietCokeBuisnessLogic.csproj b/AndDietCokeProject/AndDietCokeBuisnessLogic/AndDietCokeBuisnessLogic.csproj index 1c6db21..f308e75 100644 --- a/AndDietCokeProject/AndDietCokeBuisnessLogic/AndDietCokeBuisnessLogic.csproj +++ b/AndDietCokeProject/AndDietCokeBuisnessLogic/AndDietCokeBuisnessLogic.csproj @@ -22,6 +22,7 @@ + diff --git a/AndDietCokeProject/AndDietCokeDatabase/AndDietCokeDatabase.csproj b/AndDietCokeProject/AndDietCokeDatabase/AndDietCokeDatabase.csproj index 82027c7..2c65a57 100644 --- a/AndDietCokeProject/AndDietCokeDatabase/AndDietCokeDatabase.csproj +++ b/AndDietCokeProject/AndDietCokeDatabase/AndDietCokeDatabase.csproj @@ -21,6 +21,7 @@ + diff --git a/AndDietCokeProject/AndDietCokeDatabase/AndDietCokeDbContext.cs b/AndDietCokeProject/AndDietCokeDatabase/AndDietCokeDbContext.cs index b7d2063..dd8a117 100644 --- a/AndDietCokeProject/AndDietCokeDatabase/AndDietCokeDbContext.cs +++ b/AndDietCokeProject/AndDietCokeDatabase/AndDietCokeDbContext.cs @@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore; namespace AndDietCokeDatabase; -internal class AndDietCokeDbContext(IConfigurationDatabase configurationDatabase) : DbContext +public class AndDietCokeDbContext(IConfigurationDatabase configurationDatabase) : DbContext { private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase; diff --git a/AndDietCokeProject/AndDietCokeProject.sln b/AndDietCokeProject/AndDietCokeProject.sln index 97f4e62..5528170 100644 --- a/AndDietCokeProject/AndDietCokeProject.sln +++ b/AndDietCokeProject/AndDietCokeProject.sln @@ -9,11 +9,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AndDietCokeTests", "AndDiet EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AndDietCokeBuisnessLogic", "AndDietCokeBuisnessLogic\AndDietCokeBuisnessLogic.csproj", "{3C1C0E8F-5CB4-4E93-B20C-86FF4B248ECF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AndDietCokeDatabase", "AndDietCokeDatabase\AndDietCokeDatabase.csproj", "{4AFC4FDC-F8CA-4CC5-84E2-162ADA98EDD4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AndDietCokeDatabase", "AndDietCokeDatabase\AndDietCokeDatabase.csproj", "{4AFC4FDC-F8CA-4CC5-84E2-162ADA98EDD4}" ProjectSection(ProjectDependencies) = postProject {624595D1-D7AA-4448-AA30-4D5AA21AE6D6} = {624595D1-D7AA-4448-AA30-4D5AA21AE6D6} EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AndDietCokeWebApi", "AndDietCokeWebApi\AndDietCokeWebApi.csproj", "{59659F17-4A33-47B1-951B-A329A547B3BF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -36,6 +38,10 @@ Global {4AFC4FDC-F8CA-4CC5-84E2-162ADA98EDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU {4AFC4FDC-F8CA-4CC5-84E2-162ADA98EDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU {4AFC4FDC-F8CA-4CC5-84E2-162ADA98EDD4}.Release|Any CPU.Build.0 = Release|Any CPU + {59659F17-4A33-47B1-951B-A329A547B3BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {59659F17-4A33-47B1-951B-A329A547B3BF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {59659F17-4A33-47B1-951B-A329A547B3BF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {59659F17-4A33-47B1-951B-A329A547B3BF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AndDietCokeProject/AndDietCokeProject/BindingModels/BuyerBindingModel.cs b/AndDietCokeProject/AndDietCokeProject/BindingModels/BuyerBindingModel.cs new file mode 100644 index 0000000..2845b17 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeProject/BindingModels/BuyerBindingModel.cs @@ -0,0 +1,10 @@ +namespace AndDietCokeContracts.BindingModels; + +public class BuyerBindingModel +{ + public string? Id { get; set; } + + public string? FIO { get; set; } + + public string? PhoneNumber { get; set; } +} diff --git a/AndDietCokeProject/AndDietCokeProject/BindingModels/PostBindingModel.cs b/AndDietCokeProject/AndDietCokeProject/BindingModels/PostBindingModel.cs new file mode 100644 index 0000000..1265225 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeProject/BindingModels/PostBindingModel.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AndDietCokeContracts.BindingModels; + +public class PostBindingModel +{ + public string? Id { get; set; } + + public string? PostId => Id; + + public string? PostName { get; set; } + + public string? PostType { get; set; } + + public double Salary { get; set; } +} diff --git a/AndDietCokeProject/AndDietCokeProject/ViewModels/BuyerViewModel.cs b/AndDietCokeProject/AndDietCokeProject/ViewModels/BuyerViewModel.cs new file mode 100644 index 0000000..9f480fb --- /dev/null +++ b/AndDietCokeProject/AndDietCokeProject/ViewModels/BuyerViewModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AndDietCokeContracts.ViewModels; + +public class BuyerViewModel +{ + public required string Id { get; set; } + + public required string FIO { get; set; } + + public required string PhoneNumber { get; set; } + + public double DiscountSize { get; set; } +} diff --git a/AndDietCokeProject/AndDietCokeWebApi/AndDietCokeWebApi.csproj b/AndDietCokeProject/AndDietCokeWebApi/AndDietCokeWebApi.csproj new file mode 100644 index 0000000..4915ce9 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/AndDietCokeWebApi.csproj @@ -0,0 +1,27 @@ + + + + net7.0 + enable + enable + preview + + + + + + + + + + + + + + + + + + + + diff --git a/AndDietCokeProject/AndDietCokeWebApi/AuthOptions.cs b/AndDietCokeProject/AndDietCokeWebApi/AuthOptions.cs new file mode 100644 index 0000000..1ffb988 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/AuthOptions.cs @@ -0,0 +1,12 @@ +using Microsoft.IdentityModel.Tokens; +using System.Text; + +namespace AndDietCokeWebApi; + +public class AuthOptions +{ + public const string ISSUER = "AndDietCoke_AuthServer"; // издатель токена + public const string AUDIENCE = "AndDietCoke_AuthClient"; // потребитель токена + const string KEY = "cokesecret_secretsecretsecretkey!123"; // ключ для шифрации + public static SymmetricSecurityKey GetSymmetricSecurityKey() => new(Encoding.UTF8.GetBytes(KEY)); +} diff --git a/AndDietCokeProject/AndDietCokeWebApi/Controllers/WeatherForecastController.cs b/AndDietCokeProject/AndDietCokeWebApi/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..a1c3f7c --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/Controllers/WeatherForecastController.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using NUnit.Framework; + +namespace AndDietCokeWebApi.Controllers +{ + [Authorize] + [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 = DateOnly.FromDateTime(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/AndDietCokeProject/AndDietCokeWebApi/Infrastructure/ConfigurationDatabase.cs b/AndDietCokeProject/AndDietCokeWebApi/Infrastructure/ConfigurationDatabase.cs new file mode 100644 index 0000000..71c7935 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/Infrastructure/ConfigurationDatabase.cs @@ -0,0 +1,13 @@ +using AndDietCokeContracts.Infrastrusture; + +namespace AndDietCokeWebApi.Infrastructure; + +public class ConfigurationDatabase(IConfiguration configuration) : IConfigurationDatabase +{ + private readonly Lazy _dataBaseSettings = new(() => + { + return configuration.GetValue("DataBaseSettings") ?? throw new InvalidDataException(nameof(DataBaseSettings)); + }); + + public string ConnectionString => _dataBaseSettings.Value.ConnectionString; +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeWebApi/Infrastructure/DataBaseSettings.cs b/AndDietCokeProject/AndDietCokeWebApi/Infrastructure/DataBaseSettings.cs new file mode 100644 index 0000000..343cdbe --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/Infrastructure/DataBaseSettings.cs @@ -0,0 +1,6 @@ +namespace AndDietCokeWebApi.Infrastructure; + +public class DataBaseSettings +{ + public required string ConnectionString { get; set; } +} diff --git a/AndDietCokeProject/AndDietCokeWebApi/Program.cs b/AndDietCokeProject/AndDietCokeWebApi/Program.cs new file mode 100644 index 0000000..d033a54 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/Program.cs @@ -0,0 +1,109 @@ +using AndDietCokeWebApi; +using AndDietCokeDatabase; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.IdentityModel.Tokens; +using Microsoft.AspNetCore.OpenApi; +using Serilog; +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using Microsoft.EntityFrameworkCore; +using AndDietCokeBuisnessLogic.Implementations; +using AndDietCokeContracts.BisnessLogicsContracts; +using AndDietCokeContracts.Infrastrusture; +using AndDietCokeContracts.StoragesContracts; +using AndDietCokeDatabase.Implementations; +using AndDietCokeWebApi.Infrastructure; +using Microsoft.OpenApi.Models; +using static NUnit.Framework.TestContext; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); + +using var loggerFactory = new LoggerFactory(); +loggerFactory.AddSerilog(new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger()); +builder.Services.AddSingleton(loggerFactory.CreateLogger("Any")); + +builder.Services.AddAuthorization(); +builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + options.TokenValidationParameters = new TokenValidationParameters + { + // , + ValidateIssuer = true, + // , + ValidIssuer = AuthOptions.ISSUER, + // + ValidateAudience = true, + // + ValidAudience = AuthOptions.AUDIENCE, + // + ValidateLifetime = true, + // + IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(), + // + ValidateIssuerSigningKey = true, + }; + }); + +builder.Services.AddSingleton(); + +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + + + +// 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.MapOpenApi(); +} +if (app.Environment.IsProduction()) +{ + var dbContext = app.Services.GetRequiredService(); + if (dbContext.Database.CanConnect()) + { + dbContext.Database.EnsureCreated(); + dbContext.Database.Migrate(); + } +} + +app.UseHttpsRedirection(); + +app.UseAuthentication(); +app.UseAuthorization(); + +app.Map("/login/{username}", (string username) => +{ + return new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken( + issuer: AuthOptions.ISSUER, + audience: AuthOptions.AUDIENCE, + claims: [new(ClaimTypes.Name, username)], + expires: DateTime.UtcNow.Add(TimeSpan.FromMinutes(2)), + signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256))); +}); + +app.MapControllers(); + +app.Run(); diff --git a/AndDietCokeProject/AndDietCokeWebApi/Properties/launchSettings.json b/AndDietCokeProject/AndDietCokeWebApi/Properties/launchSettings.json new file mode 100644 index 0000000..5325099 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:48128", + "sslPort": 44326 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5006", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7215;http://localhost:5006", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/AndDietCokeProject/AndDietCokeWebApi/WeatherForecast.cs b/AndDietCokeProject/AndDietCokeWebApi/WeatherForecast.cs new file mode 100644 index 0000000..2eda47f --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace AndDietCokeWebApi +{ + 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; } + } +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeWebApi/appsettings.Development.json b/AndDietCokeProject/AndDietCokeWebApi/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/AndDietCokeProject/AndDietCokeWebApi/appsettings.json b/AndDietCokeProject/AndDietCokeWebApi/appsettings.json new file mode 100644 index 0000000..e2181a6 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeWebApi/appsettings.json @@ -0,0 +1,28 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": { + "Default": "Information" + }, + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "../logs/anddietcoke-.log", + "rollingInterval": "Day", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level:u3} {Username} {Message:lj}{Exception}{NewLine}" + } + } + ] + }, + "AllowedHosts": "*", + "DataBaseSettings": { + "ConnectionString": "Host=127.0.0.1;Port=5432;Database=AndDietCoke;Username=postgres;Password=postgres;" + } +}