Bogdanov D.S. LabWork05 #5
@ -11,5 +11,7 @@ namespace ComputerShopContracts.SearchModels
|
||||
public int? Id { get; set; }
|
||||
public DateTime? DateFrom { get; set; }
|
||||
public DateTime? DateTo { get; set; }
|
||||
public int? ClientId { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
65
ComputerShopRestApi/Controllers/ClientController.cs
Normal file
65
ComputerShopRestApi/Controllers/ClientController.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.BusinessLogicsContracts;
|
||||
using ComputerShopContracts.SearchModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ComputerShopRestApi.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class ClientController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IClientLogic _logic;
|
||||
public ClientController(IClientLogic logic, ILogger<ClientController>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
83
ComputerShopRestApi/Controllers/MainController.cs
Normal file
83
ComputerShopRestApi/Controllers/MainController.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.BusinessLogicsContracts;
|
||||
using ComputerShopContracts.SearchModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using DocumentFormat.OpenXml.Office2010.Excel;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ComputerShopRestApi.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class MainController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IOrderLogic _order;
|
||||
private readonly IComputerLogic _computer;
|
||||
public MainController(ILogger<MainController> logger, IOrderLogic order, IComputerLogic computer)
|
||||
{
|
||||
_logger = logger;
|
||||
_order = order;
|
||||
_computer = computer;
|
||||
}
|
||||
[HttpGet]
|
||||
public List<ComputerViewModel>? GetProductList()
|
||||
{
|
||||
try
|
||||
{
|
||||
return _computer.ReadList(null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка получения списка продуктов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
public ComputerViewModel? GetProduct(int productId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _computer.ReadElement(new ComputerSearchModel
|
||||
{
|
||||
Id =
|
||||
productId
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка получения продукта по id={Id}", productId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
public List<OrderViewModel>? 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -21,7 +21,15 @@ builder.Services.AddTransient<IComputerLogic, ComputerLogic>();
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo
|
||||
{
|
||||
Title = "ComputerShopRestApi",
|
||||
Version
|
||||
= "v1"
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@ -29,7 +37,8 @@ var app = builder.Build();
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json",
|
||||
"ComputerShopRestApi v1"));
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
Loading…
Reference in New Issue
Block a user