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

164 lines
5.2 KiB
C#
Raw Normal View History

using DocumentFormat.OpenXml.Presentation;
using Microsoft.AspNetCore.Mvc;
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 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)
2024-05-28 18:32:51 +04:00
{
_logger = logger;
_excursionLogic = excursionLogic;
_tourLogic = tourLogic;
_placeLogic = placeLogic;
2024-05-28 18:32:51 +04:00
}
[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);
2024-05-28 18:32:51 +04:00
return View();
}
[HttpPost]
public void CreateExcursion(string excursionName, string excursionDescription, int place, double price, List<int> tours)
2024-05-28 18:32:51 +04:00
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(excursionName) || string.IsNullOrEmpty(excursionDescription) || price <= 0 || place <= 0 || tours == null)
2024-05-28 18:32:51 +04:00
{
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,
2024-05-28 18:32:51 +04:00
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);
2024-05-28 18:32:51 +04:00
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)
2024-05-28 18:32:51 +04:00
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(excursionName) || string.IsNullOrEmpty(excursionDescription) || price <= 0 || place <= 0 || tours == null)
2024-05-28 18:32:51 +04:00
{
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,
2024-05-28 18:32:51 +04:00
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");
}
}
}