PIbd-21_Anisin_R.S._CourseW.../TravelAgency/TravelAgencyWebApp/Controllers/ExcursionGroupController.cs
2024-05-28 18:32:51 +04:00

162 lines
5.2 KiB
C#

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<ExcursionGroupController> _logger;
private readonly IExcursionGroupLogic _excursionGroupLogic;
private readonly ITourLogic _tourLogic;
private readonly IGuideLogic _guideLogic;
public ExcursionGroupController(ILogger<ExcursionGroupController> 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<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 })!);
}
_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");
}
}
}