Настройка RestApi + контроллер сделки + фикс DealSearchModel (Поиск по id оператора)

This commit is contained in:
abazov73 2023-04-07 13:15:01 +04:00
parent d08b7351bd
commit dc7065bc50
8 changed files with 143 additions and 11 deletions

View File

@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BankDatabaseImplement", "Ba
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OperatorApp", "OperatorApp\OperatorApp.csproj", "{AF3FEDE5-EE18-4197-A01B-777B2E12F5EC}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OperatorApp", "OperatorApp\OperatorApp.csproj", "{AF3FEDE5-EE18-4197-A01B-777B2E12F5EC}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BankRestApi", "BankRestApi\BankRestApi.csproj", "{C8E959B1-3F3A-40D5-8186-D465C958A8BC}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -39,6 +41,10 @@ Global
{AF3FEDE5-EE18-4197-A01B-777B2E12F5EC}.Debug|Any CPU.Build.0 = Debug|Any CPU {AF3FEDE5-EE18-4197-A01B-777B2E12F5EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF3FEDE5-EE18-4197-A01B-777B2E12F5EC}.Release|Any CPU.ActiveCfg = Release|Any CPU {AF3FEDE5-EE18-4197-A01B-777B2E12F5EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF3FEDE5-EE18-4197-A01B-777B2E12F5EC}.Release|Any CPU.Build.0 = Release|Any CPU {AF3FEDE5-EE18-4197-A01B-777B2E12F5EC}.Release|Any CPU.Build.0 = Release|Any CPU
{C8E959B1-3F3A-40D5-8186-D465C958A8BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C8E959B1-3F3A-40D5-8186-D465C958A8BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C8E959B1-3F3A-40D5-8186-D465C958A8BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C8E959B1-3F3A-40D5-8186-D465C958A8BC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -9,5 +9,6 @@ namespace BankContracts.SearchModels
public class DealSearchModel public class DealSearchModel
{ {
public int? Id { get; set; } public int? Id { get; set; }
public int? OperatorId { get; set; }
} }
} }

View File

@ -23,15 +23,26 @@ namespace BankDatabaseImplement.Implements
} }
public List<DealViewModel> GetFilteredList(DealSearchModel model) public List<DealViewModel> GetFilteredList(DealSearchModel model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue && !model.OperatorId.HasValue)
{ {
return new(); return new();
} }
using var context = new BankDatabase(); if (model.OperatorId.HasValue)
return context.Deals.Include(x => x.Operator) {
.Where(x => x.Id == model.Id) using var context = new BankDatabase();
.Select(x => x.GetViewModel) return context.Deals.Include(x => x.Operator)
.ToList(); .Where(x => x.OperatorId == model.OperatorId)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
using var context = new BankDatabase();
return context.Deals.Include(x => x.Operator)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
} }
public DealViewModel? GetElement(DealSearchModel model) public DealViewModel? GetElement(DealSearchModel model)
{ {

View File

@ -1,5 +1,6 @@
using BankContracts.BindingModels; using BankContracts.BindingModels;
using BankContracts.SearchModels; using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels; using BankContracts.ViewModels;
using BankDatabaseImplement.Models; using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -11,7 +12,7 @@ using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements namespace BankDatabaseImplement.Implements
{ {
public class TransferStorage public class TransferStorage : ITransferStorage
{ {
public List<TransferViewModel> GetFullList() public List<TransferViewModel> GetFullList()
{ {

View File

@ -7,11 +7,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Controllers\" /> <ProjectReference Include="..\BankBusinessLogic\BankBusinessLogic.csproj" />
<ProjectReference Include="..\BankDatabaseImplement\BankDatabaseImplement.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,77 @@
using BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace BankRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class DealController : Controller
{
private readonly ILogger _logger;
private readonly IDealLogic _deal;
public DealController(ILogger<DealController> logger, IDealLogic deal)
{
_logger = logger;
_deal = deal;
}
[HttpGet]
public List<DealViewModel>? GetDeals(int operatorId)
{
try
{
return _deal.ReadList(new DealSearchModel { OperatorId = operatorId});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка сделок оператора id={Id}", operatorId);
throw;
}
}
[HttpPost]
public void CreateDeal(DealBindingModel model)
{
try
{
_deal.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания сделки");
throw;
}
}
[HttpPatch]
public void UpdateDeal(DealBindingModel model)
{
try
{
_deal.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления сделки");
throw;
}
}
[HttpDelete]
public void DeleteDeal(DealBindingModel model)
{
try
{
_deal.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления сделки");
throw;
}
}
}
}

View File

@ -1,11 +1,29 @@
using BankBusinessLogic.BusinessLogics;
using BankContracts.BusinessLogicsContracts;
using BankContracts.StoragesContracts;
using BankDatabaseImplement.Implements;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Logging.SetMinimumLevel(LogLevel.Trace);
builder.Logging.AddLog4Net("log4net.config");
// Add services to the container. // Add services to the container.
builder.Services.AddTransient<IDealStorage, DealStorage>();
builder.Services.AddTransient<IPaymentStorage, PaymentStorage>();
builder.Services.AddTransient<ITransferStorage, TransferStorage>();
builder.Services.AddTransient<IOperatorStorage, OperatorStorage>();
builder.Services.AddTransient<IDealLogic, DealLogic>();
builder.Services.AddTransient<IPaymentLogic, PaymentLogic>();
builder.Services.AddTransient<ITransferLogic, TransferLogic>();
builder.Services.AddTransient<IOperatorLogic, OperatorLogic>();
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "BankRestApi", Version = "v1" });
});
var app = builder.Build(); var app = builder.Build();
@ -13,7 +31,7 @@ var app = builder.Build();
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BankRestApi v1"));
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="c:/temp/BankRestApi.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
</layout>
</appender>
<root>
<level value="TRACE" />
<appender-ref ref="RollingFile" />
</root>
</log4net>