CourseWork_Borschevskaya_A..../Hospital/HospitalRestApi/Controllers/MedicineController.cs

125 lines
3.7 KiB
C#

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 MedicineController : ControllerBase
{
private readonly ILogger _logger;
private readonly IMedicineLogic _logic;
private readonly IProcedureLogic _logicP;
public MedicineController(IMedicineLogic logic, ILogger<MedicineController> logger, IProcedureLogic logicP)
{
_logger = logger;
_logic = logic;
_logicP = logicP;
}
[HttpGet]
public List<MedicineViewModel>? GetMedicines(int? apothecaryid)
{
try
{
if (apothecaryid == null)
{
return _logic.ReadList(null);
}
return _logic.ReadList(new MedicineSearchModel
{
ApothecaryId = apothecaryid
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка лекарств");
throw;
}
}
[HttpGet]
public MedicineViewModel? GetMedicine(int id)
{
try
{
return _logic.ReadElement(new MedicineSearchModel
{
Id = id
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения поступления ");
throw;
}
}
[HttpPost]
public void Create(MedicineBindingModel model)
{
try
{
_logic.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания лекарства");
throw;
}
}
[HttpPost]
public void Update(MedicineBindingModel model)
{
try
{
_logic.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления лекарства");
throw;
}
}
[HttpPost]
public void Delete(MedicineBindingModel model)
{
try
{
_logic.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления лекарства");
throw;
}
}
[HttpPost]
public void AddProcedureMedicine(Tuple<MedicineBindingModel, ProcedureBindingModel> addInfo)
{
var medicineId = addInfo.Item1.Id;
var procedureId = addInfo.Item2.Id;
var procedure = _logicP.ReadElement(new ProcedureSearchModel { Id = procedureId });
var medicine = _logic.ReadElement(new MedicineSearchModel { Id = medicineId });
if (procedure != null && medicine != null)
{
var procedureMedicines = procedure.ProcedureMedicines;
procedureMedicines[medicineId] = medicine;
_logicP.Update(new ProcedureBindingModel {
Id = procedureId,
Name = procedure.Name,
ProcedureMedicines = procedureMedicines
});
}
}
}
}