CourseWork_Hotel/Hotel/HotelRestApi/Controllers/MainController.cs

228 lines
6.5 KiB
C#

using HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
using HotelContracts.ViewModels;
using HotelDataBaseImplement.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO.Packaging;
namespace HotelRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IConferenceLogic _conference;
private readonly IMemberLogic _member;
private readonly IMealPlanLogic _mealPlan;
public MainController(ILogger<MainController> logger, IConferenceLogic conference, IMemberLogic member, IMealPlanLogic mealPlan)
{
_logger = logger;
_conference = conference;
_member = member;
_mealPlan = mealPlan;
}
[HttpGet]
public List<ConferenceViewModel>? GetConferenceList(int organiserId)
{
try
{
return _conference.ReadList(new ConferenceSearchModel
{
OrganiserId = organiserId,
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка конференций");
throw;
}
}
[HttpGet]
public List<MemberViewModel>? GetMemberList(int organiserId)
{
try
{
return _member.ReadList(new MemberSearchModel
{
OrganiserId= organiserId,
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка участников");
throw;
}
}
[HttpGet]
public List<MealPlanViewModel>? GetMealPlanList(int organiserId)
{
try
{
return _mealPlan.ReadList(new MealPlanSearchModel
{
OrganiserId=organiserId,
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка планов питания");
throw;
}
}
[HttpPost]
public void UpdateMember(MemberBindingModel model)
{
try
{
_member.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpGet]
public MemberViewModel? GetMember(int memberId)
{
try
{
return _member.ReadElement(new MemberSearchModel
{
Id = memberId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения участника по id={Id}", memberId);
throw;
}
}
[HttpPost]
public void CreateMember(MemberBindingModel model)
{
try
{
_member.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания участника");
throw;
}
}
[HttpPost]
public void DeleteMember(MemberBindingModel model)
{
try
{
_member.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления участника");
throw;
}
}
[HttpPost]
public void CreateConference(ConferenceBindingModel model)
{
try
{
_conference.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания конференции");
throw;
}
}
[HttpPost]
public void UpdateConference(ConferenceBindingModel model)
{
try
{
model.ConferenceMembers = null!;
_conference.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных");
throw;
}
}
[HttpGet]
public Tuple<ConferenceViewModel, List<Tuple<string, string>>>? GetConference(int conferenceId)
{
try
{
var elem = _conference.ReadElement(new ConferenceSearchModel { Id = conferenceId });
if (elem == null)
return null;
return Tuple.Create(elem, elem.ConferenceMembers.Select(x => Tuple.Create(x.Value.MemberFIO, x.Value.Citizenship)).ToList());
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения конференции по id={Id}", conferenceId);
throw;
}
}
[HttpPost]
public void DeleteConference(ConferenceBindingModel model)
{
try
{
_conference.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления конференции");
throw;
}
}
[HttpPost]
public void AddMemberToConference(Tuple<ConferenceSearchModel, MemberViewModel> model)
{
try
{
_conference.AddMemberToConference(model.Item1, model.Item2);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка добавления участника в конференцию.");
throw;
}
}
[HttpPost]
public void CreateMealPlan(MealPlanBindingModel model)
{
try
{
_mealPlan.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания плана питания");
throw;
}
}
}
}