using Microsoft.AspNetCore.Mvc; using System; using TravelAgencyContracts.BindingModels; using TravelAgencyContracts.BusinessLogicsContracts; using TravelAgencyContracts.SearchModels; using TravelAgencyDatabaseImplement.Models; using TravelAgencyDataModels.Models; namespace TravelAgencyWebApp.Controllers { public class ExcursionGroupController : Controller { private readonly ILogger _logger; private readonly IExcursionGroupLogic _excursionGroupLogic; private readonly ITourLogic _tourLogic; private readonly IGuideLogic _guideLogic; public ExcursionGroupController(ILogger logger, IExcursionGroupLogic excursionGroupLogic, ITourLogic tourLogic, IGuideLogic guideLogic) { _logger = logger; _excursionGroupLogic = excursionGroupLogic; _tourLogic = tourLogic; _guideLogic = guideLogic; } [HttpGet] public IActionResult ExcursionGroups() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } return View(_excursionGroupLogic.ReadList(new ExcursionGroupSearchModel { UserId = LoggedinUser.User.Id, })); } [HttpGet] public IActionResult CreateExcursionGroup() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Tours = _tourLogic.ReadList(new TourSearchModel { UserId = LoggedinUser.User.Id, }); ViewBag.Guides = _guideLogic.ReadList(null); return View(); } [HttpPost] public void CreateExcursionGroup(string excursionGroupName, int participantsAmount, int guide, List tours) { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(excursionGroupName) || participantsAmount < 1 || guide <= 0 || tours == null) { throw new Exception("Введены не все данные!"); } Dictionary excursionGroupTours = new Dictionary(); foreach (var tourId in tours) { excursionGroupTours.Add(tourId, _tourLogic.ReadElement(new TourSearchModel { Id = tourId })!); } _excursionGroupLogic.Create(new ExcursionGroupBindingModel { ExcursionGroupName = excursionGroupName, ParticipantsAmount = participantsAmount, UserId = LoggedinUser.User.Id, GuideId = guide, ExcursionGroupTours = excursionGroupTours }); Response.Redirect("/ExcursionGroup/ExcursionGroups"); } [HttpGet] public IActionResult UpdateExcursionGroup(int id) { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Tours = _tourLogic.ReadList(new TourSearchModel { UserId = LoggedinUser.User.Id, }); ViewBag.Guides = _guideLogic.ReadList(null); return View(_excursionGroupLogic.ReadElement(new ExcursionGroupSearchModel { Id = id })); } [HttpPost] public void UpdateExcursionGroup(int id, string excursionGroupName, int participantsAmount, int guide, List tours) { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(excursionGroupName) || participantsAmount < 1 || guide <= 0 || tours == null) { throw new Exception("Введены не все данные!"); } Dictionary excursionGroupTours = new Dictionary(); foreach (var tourId in tours) { excursionGroupTours.Add(tourId, _tourLogic.ReadElement(new TourSearchModel { Id = tourId })!); } _excursionGroupLogic.Update(new ExcursionGroupBindingModel { Id = id, ExcursionGroupName = excursionGroupName, ParticipantsAmount = participantsAmount, UserId = LoggedinUser.User.Id, GuideId = guide, ExcursionGroupTours = excursionGroupTours }); Response.Redirect("/ExcursionGroup/ExcursionGroups"); } [HttpPost] public void DeleteExcursionGroup(int id) { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } _excursionGroupLogic.Delete(new ExcursionGroupBindingModel { Id = id }); Response.Redirect("/ExcursionGroup/ExcursionGroups"); } } }