PIbd-21_Anisin_R.S._CourseW.../TravelAgency/TravelAgencyWebApp/Controllers/ExcursionGroupController.cs

226 lines
7.6 KiB
C#
Raw Normal View History

using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Mvc;
2024-05-28 18:32:51 +04:00
using System;
using System.Linq;
2024-05-28 18:32:51 +04:00
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<ExcursionGroupController> _logger;
private readonly IExcursionGroupLogic _excursionGroupLogic;
private readonly ITourLogic _tourLogic;
private readonly IGuideLogic _guideLogic;
private readonly IPlaceLogic _placeLogic;
public ExcursionGroupController(ILogger<ExcursionGroupController> logger, IExcursionGroupLogic excursionGroupLogic, ITourLogic tourLogic, IGuideLogic guideLogic, IPlaceLogic placeLogic)
2024-05-28 18:32:51 +04:00
{
_logger = logger;
_excursionGroupLogic = excursionGroupLogic;
_tourLogic = tourLogic;
_guideLogic = guideLogic;
_placeLogic = placeLogic;
2024-05-28 18:32:51 +04:00
}
[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<int> tours)
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(excursionGroupName) || participantsAmount < 1 || guide <= 0 || tours == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, ITourModel> excursionGroupTours = new Dictionary<int, ITourModel>();
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<int> tours)
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(excursionGroupName) || participantsAmount < 1 || guide <= 0 || tours == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, ITourModel> excursionGroupTours = new Dictionary<int, ITourModel>();
foreach (var tourId in tours)
{
excursionGroupTours.Add(tourId, _tourLogic.ReadElement(new TourSearchModel { Id = tourId })!);
}
var excursionGroup = _excursionGroupLogic.ReadElement(new ExcursionGroupSearchModel() { Id = id });
2024-05-28 18:32:51 +04:00
_excursionGroupLogic.Update(new ExcursionGroupBindingModel
{
Id = id,
ExcursionGroupName = excursionGroupName,
ParticipantsAmount = participantsAmount,
UserId = LoggedinUser.User.Id,
GuideId = guide,
ExcursionGroupTours = excursionGroupTours,
ExcursionGroupPlaces = excursionGroup.ExcursionGroupPlaces
2024-05-28 18:32:51 +04:00
});
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");
}
[HttpGet]
public IActionResult ConnectGroupToPlace()
{
if (LoggedinUser.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.ExcursionGroups = _excursionGroupLogic.ReadList(new ExcursionGroupSearchModel
{
UserId = LoggedinUser.User.Id,
});
ViewBag.Places = _placeLogic.ReadList(null);
return View();
}
[HttpPost]
public IActionResult ConnectGroupToPlace(int excursionGroupId, int placeId)
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (excursionGroupId <= 0 || placeId <= 0)
{
throw new Exception("Введены не все данные!");
}
var excursionGroup = _excursionGroupLogic.ReadElement(new ExcursionGroupSearchModel() { Id = excursionGroupId });
Dictionary<int, IPlaceModel> excursionGroupPlaces = excursionGroup.ExcursionGroupPlaces;
if (!excursionGroupPlaces.ContainsKey(placeId))
excursionGroupPlaces.Add(placeId, _placeLogic.ReadElement(new PlaceSearchModel { Id = placeId })!);
_excursionGroupLogic.Update(new ExcursionGroupBindingModel
{
Id = excursionGroupId,
ExcursionGroupName = excursionGroup.ExcursionGroupName,
ParticipantsAmount = excursionGroup.ParticipantsAmount,
UserId = LoggedinUser.User.Id,
GuideId = excursionGroup.GuideId,
ExcursionGroupTours = excursionGroup.ExcursionGroupTours,
ExcursionGroupPlaces = excursionGroupPlaces
});
ViewBag.ExcursionGroups = _excursionGroupLogic.ReadList(new ExcursionGroupSearchModel
{
UserId = LoggedinUser.User.Id,
});
ViewBag.Places = _placeLogic.ReadList(null);
return View();
}
2024-05-28 18:32:51 +04:00
}
}