rest api что ли

This commit is contained in:
Alenka 2024-04-29 23:15:01 +04:00
parent 30ea05901a
commit 0054d6550a
10 changed files with 318 additions and 1 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalBusinessLogic", "Ho
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalDatabaseImplement", "HospitalDatabaseImplement\HospitalDatabaseImplement.csproj", "{651AD7CE-7821-4BDF-9FA4-4F61DC5CF5C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PharmacistApp", "PharmacistApp\PharmacistApp.csproj", "{5CE4060B-0791-4F09-A52D-EF3EFF4EBF03}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PharmacistApp", "PharmacistApp\PharmacistApp.csproj", "{5CE4060B-0791-4F09-A52D-EF3EFF4EBF03}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalRestApi", "HospitalRestApi\HospitalRestApi.csproj", "{C524A697-427E-40BD-955C-334255D958B7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{5CE4060B-0791-4F09-A52D-EF3EFF4EBF03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5CE4060B-0791-4F09-A52D-EF3EFF4EBF03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5CE4060B-0791-4F09-A52D-EF3EFF4EBF03}.Release|Any CPU.Build.0 = Release|Any CPU
{C524A697-427E-40BD-955C-334255D958B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C524A697-427E-40BD-955C-334255D958B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C524A697-427E-40BD-955C-334255D958B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C524A697-427E-40BD-955C-334255D958B7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,107 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using HospitalDatabaseImplement.Models;
namespace HospitalRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MedicineController : Controller
{
private readonly ILogger _logger;
private readonly IMedicineLogic _medicine;
public MedicineController(ILogger<MedicineController> logger, IMedicineLogic medicine)
{
_logger = logger;
_medicine = medicine;
}
[HttpGet]
public Tuple<MedicineViewModel, List<string>>? GetMedicine(int medicineId)
{
try
{
var elem = _medicine.ReadElement(new MedicineSearchModel { Id = medicineId });
if (elem == null)
return null;
var res = Tuple.Create(elem, elem.ProcedureMedicines.Select(x => x.Value.AnimalName).ToList());
res.Item1.MedicineAnimals = null!;
return res;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения медикамента по id={Id}", medicineId);
throw;
}
}
[HttpGet]
public List<MedicineViewModel>? GetMedicines(int? pharmacistId = null)
{
try
{
List<MedicineViewModel> res;
if (!pharmacistId.HasValue)
res = _medicine.ReadList(null);
else
res = _medicine.ReadList(new MedicineSearchModel { PharmacistId = pharmacistId });
foreach (var medicine in res)
medicine.ProcedureMedicines = null!;
return res;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка медикаментов");
throw;
}
}
[HttpPost]
public bool CreateMedicine(MedicineBindingModel model)
{
try
{
return _medicine.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось создать медикамент");
throw;
}
}
[HttpPost]
public bool UpdateMedicine(bool isConnection, MedicineBindingModel model)
{
try
{
if (!isConnection)
model.ProcedureMedicines = null!;
return _medicine.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось обновить магазин");
throw;
}
}
[HttpPost]
public bool DeleteMedicine(MedicineBindingModel model)
{
try
{
return _medicine.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления магазина");
throw;
}
}
}
}

View File

@ -0,0 +1,67 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace HospitalRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class PharmacistController : Controller
{
private readonly ILogger _logger;
private readonly IPharmacistLogic _logic;
public PharmacistController(IPharmacistLogic logic, ILogger<PharmacistController>
logger)
{
_logger = logger;
_logic = logic;
}
[HttpGet]
public PharmacistViewModel? Login(string login, string password)
{
try
{
return _logic.ReadElement(new PharmacistSearchModel
{
Login = login,
Password = password
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка входа в систему");
throw;
}
}
[HttpPost]
public void Register(PharmacistBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка регистрации");
throw;
}
}
[HttpPost]
public void UpdateData(PharmacistBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
}
}

View File

@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;
namespace HospitalRestApi.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<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 = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HospitalBusinessLogic\HospitalBusinessLogic.csproj" />
<ProjectReference Include="..\HospitalDatabaseImplement\HospitalDatabaseImplement.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// 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.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:20855",
"sslPort": 44330
}
},
"profiles": {
"HospitalRestApi": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7092;http://localhost:5092",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,13 @@
namespace HospitalRestApi
{
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; }
}
}

View File

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

View File

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