PIbd-22_Filippov_D.S._Cours.../VeterinaryRestApi/Controllers/VisitController.cs
2024-05-30 07:33:38 +04:00

117 lines
2.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.AspNetCore.Mvc;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.BusinessLogicContracts;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;
namespace VeterinaryRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class VisitController : Controller
{
private readonly ILogger _logger;
private readonly IVisitLogic _visit;
public VisitController(ILogger<VisitController> logger, IVisitLogic visit)
{
_logger = logger;
_visit = visit;
}
[HttpGet]
public Tuple<VisitViewModel, List<Tuple<string, int>>>? GetVisit(int visitId)
{
try
{
var elem = _visit.ReadElement(new VisitSearchModel { Id = visitId });
if (elem == null)
return null;
var res = Tuple.Create(elem, elem.VisitPet.Select(x => Tuple.Create(x.Value.PetName, x.Value.Id)).ToList());
res.Item1.VisitPet = null!;
return res;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения визита по id={Id}", visitId);
throw;
}
}
[HttpGet]
public List<VisitViewModel>? GetVisits(int? ownerId = null)
{
try
{
List<VisitViewModel> res;
if (!ownerId.HasValue)
res = _visit.ReadList(null);
else
res = _visit.ReadList(new VisitSearchModel { OwnerId = ownerId.Value });
foreach (var service in res)
service.VisitPet = null;
return res;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка визитов");
throw;
}
}
[HttpGet]
public List<VisitViewModel> GetAllVisits()
{
try
{
return _visit.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка визитов");
throw;
}
}
[HttpPost]
public bool CreateVisit(VisitBindingModel model)
{
try
{
return _visit.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось создать визит");
throw;
}
}
[HttpPost]
public bool UpdateVisit(VisitBindingModel model)
{
try
{
return _visit.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Не удалось обновить рекомендации");
throw;
}
}
[HttpPost]
public bool DeleteVisit(VisitBindingModel model)
{
try
{
return _visit.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления рекомендации");
throw;
}
}
}
}