какая-то часть

This commit is contained in:
2025-04-12 09:40:58 +04:00
parent 3c4b226736
commit 98af2eb8a1
17 changed files with 351 additions and 2 deletions

View File

@@ -22,6 +22,7 @@
<ItemGroup> <ItemGroup>
<InternalsVisibleTo Include="AndDietCokeTests" /> <InternalsVisibleTo Include="AndDietCokeTests" />
<InternalsVisibleTo Include="AndDietCokeWebApi" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" /> <InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup> </ItemGroup>

View File

@@ -21,6 +21,7 @@
<ItemGroup> <ItemGroup>
<InternalsVisibleTo Include="AndDietCokeTests" /> <InternalsVisibleTo Include="AndDietCokeTests" />
<InternalsVisibleTo Include="AndDietCokeWebApi" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" /> <InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup> </ItemGroup>

View File

@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
namespace AndDietCokeDatabase; namespace AndDietCokeDatabase;
internal class AndDietCokeDbContext(IConfigurationDatabase configurationDatabase) : DbContext public class AndDietCokeDbContext(IConfigurationDatabase configurationDatabase) : DbContext
{ {
private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase; private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase;

View File

@@ -9,11 +9,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AndDietCokeTests", "AndDiet
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AndDietCokeBuisnessLogic", "AndDietCokeBuisnessLogic\AndDietCokeBuisnessLogic.csproj", "{3C1C0E8F-5CB4-4E93-B20C-86FF4B248ECF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AndDietCokeBuisnessLogic", "AndDietCokeBuisnessLogic\AndDietCokeBuisnessLogic.csproj", "{3C1C0E8F-5CB4-4E93-B20C-86FF4B248ECF}"
EndProject 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 ProjectSection(ProjectDependencies) = postProject
{624595D1-D7AA-4448-AA30-4D5AA21AE6D6} = {624595D1-D7AA-4448-AA30-4D5AA21AE6D6} {624595D1-D7AA-4448-AA30-4D5AA21AE6D6} = {624595D1-D7AA-4448-AA30-4D5AA21AE6D6}
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AndDietCokeWebApi", "AndDietCokeWebApi\AndDietCokeWebApi.csproj", "{59659F17-4A33-47B1-951B-A329A547B3BF}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{4AFC4FDC-F8CA-4CC5-84E2-162ADA98EDD4}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -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; }
}

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.20" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.15" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.8.0" />
<PackageReference Include="NSwag.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AndDietCokeBuisnessLogic\AndDietCokeBuisnessLogic.csproj" />
<ProjectReference Include="..\AndDietCokeDatabase\AndDietCokeDatabase.csproj" />
<ProjectReference Include="..\AndDietCokeProject\AndDietCokeContracts.csproj" />
<ProjectReference Include="..\AndDietCokeTests\AndDietCokeTests.csproj" />
</ItemGroup>
</Project>

View File

@@ -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));
}

View File

@@ -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<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> 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();
}
}
}

View File

@@ -0,0 +1,13 @@
using AndDietCokeContracts.Infrastrusture;
namespace AndDietCokeWebApi.Infrastructure;
public class ConfigurationDatabase(IConfiguration configuration) : IConfigurationDatabase
{
private readonly Lazy<DataBaseSettings> _dataBaseSettings = new(() =>
{
return configuration.GetValue<DataBaseSettings>("DataBaseSettings") ?? throw new InvalidDataException(nameof(DataBaseSettings));
});
public string ConnectionString => _dataBaseSettings.Value.ConnectionString;
}

View File

@@ -0,0 +1,6 @@
namespace AndDietCokeWebApi.Infrastructure;
public class DataBaseSettings
{
public required string ConnectionString { get; set; }
}

View File

@@ -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
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidateIssuer = true,
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidIssuer = AuthOptions.ISSUER,
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidateAudience = true,
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidAudience = AuthOptions.AUDIENCE,
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidateLifetime = true,
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ValidateIssuerSigningKey = true,
};
});
builder.Services.AddSingleton<IConfigurationDatabase, ConfigurationDatabase>();
builder.Services.AddTransient<IBuyerBusinessLogicContract, BuyerBusinessLogicContract>();
builder.Services.AddTransient<IPostBusinessLogicContract, PostBusinessLogicContract>();
builder.Services.AddTransient<IDishBusinessLogicContract, DishBusinessLogicContract>();
builder.Services.AddTransient<ISalaryBusinessLogicContract, SalaryBusinessLogicContract>();
builder.Services.AddTransient<ISaleBusinessLogicContract, SaleBusinessLogicContract>();
builder.Services.AddTransient<IWorkerBusinessLogicContract, WorkerBusinessLogicContract>();
builder.Services.AddTransient<AndDietCokeDbContext>();
builder.Services.AddTransient<IBuyerStorageContract, BuyerStorageContract>();
builder.Services.AddTransient<IPostStorageContract, PostStorageContract>();
builder.Services.AddTransient<IDishStorageContract, DishStorageContract>();
builder.Services.AddTransient<ISalaryStorageContract, SalaryStorageContract>();
builder.Services.AddTransient<ISaleStorageContract, SaleStorageContract>();
builder.Services.AddTransient<IWorkerStorageContract, WorkerStorageContract>();
// 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<AndDietCokeDbContext>();
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();

View File

@@ -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"
}
}
}
}

View File

@@ -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; }
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -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;"
}
}