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

164 lines
5.2 KiB
C#

using DocumentFormat.OpenXml.Presentation;
using Microsoft.AspNetCore.Mvc;
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyDatabaseImplement.Models;
using TravelAgencyDataModels.Models;
namespace TravelAgencyWebApp.Controllers
{
public class ExcursionController : Controller
{
private readonly ILogger<ExcursionController> _logger;
private readonly IExcursionLogic _excursionLogic;
private readonly ITourLogic _tourLogic;
private readonly IPlaceLogic _placeLogic;
public ExcursionController(ILogger<ExcursionController> logger, IExcursionLogic excursionLogic, ITourLogic tourLogic, IPlaceLogic placeLogic)
{
_logger = logger;
_excursionLogic = excursionLogic;
_tourLogic = tourLogic;
_placeLogic = placeLogic;
}
[HttpGet]
public IActionResult Excursions()
{
if (LoggedinUser.User == null)
{
return Redirect("~/Home/Enter");
}
return View(_excursionLogic.ReadList(new ExcursionSearchModel
{
UserId = LoggedinUser.User.Id,
}));
}
[HttpGet]
public IActionResult CreateExcursion()
{
if (LoggedinUser.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Tours = _tourLogic.ReadList(new TourSearchModel
{
UserId = LoggedinUser.User.Id,
});
ViewBag.Places = _placeLogic.ReadList(null);
return View();
}
[HttpPost]
public void CreateExcursion(string excursionName, string excursionDescription, int place, double price, List<int> tours)
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(excursionName) || string.IsNullOrEmpty(excursionDescription) || price <= 0 || place <= 0 || tours == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, ITourModel> excursionTours = new Dictionary<int, ITourModel>();
foreach (var tourId in tours)
{
excursionTours.Add(tourId, _tourLogic.ReadElement(new TourSearchModel { Id = tourId })!);
}
_excursionLogic.Create(new ExcursionBindingModel
{
ExcursionName = excursionName,
ExcursionDescription = excursionDescription,
Price = price,
UserId = LoggedinUser.User.Id,
PlaceId = place,
ExcursionTours = excursionTours
});
Response.Redirect("/Excursion/Excursions");
}
[HttpGet]
public IActionResult UpdateExcursion(int id)
{
if (LoggedinUser.User == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Tours = _tourLogic.ReadList(new TourSearchModel
{
UserId = LoggedinUser.User.Id,
});
ViewBag.Places = _placeLogic.ReadList(null);
return View(_excursionLogic.ReadElement(new ExcursionSearchModel
{
Id = id
}));
}
[HttpPost]
public void UpdateExcursion(int id, string excursionName, string excursionDescription, int place, double price, List<int> tours)
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(excursionName) || string.IsNullOrEmpty(excursionDescription) || price <= 0 || place <= 0 || tours == null)
{
throw new Exception("Введены не все данные!");
}
Dictionary<int, ITourModel> excursionTours = new Dictionary<int, ITourModel>();
foreach (var tourId in tours)
{
excursionTours.Add(tourId, _tourLogic.ReadElement(new TourSearchModel { Id = tourId })!);
}
_excursionLogic.Update(new ExcursionBindingModel
{
Id = id,
ExcursionName = excursionName,
ExcursionDescription = excursionDescription,
Price = price,
UserId = LoggedinUser.User.Id,
PlaceId = place,
ExcursionTours = excursionTours
});
Response.Redirect("/Excursion/Excursions");
}
[HttpPost]
public void DeleteExcursion(int id)
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
_excursionLogic.Delete(new ExcursionBindingModel
{
Id = id
});
Response.Redirect("/Excursion/Excursions");
}
}
}