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

154 lines
4.8 KiB
C#

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;
public ExcursionController(ILogger<ExcursionController> logger, IExcursionLogic excursionLogic, ITourLogic tourLogic)
{
_logger = logger;
_excursionLogic = excursionLogic;
_tourLogic = tourLogic;
}
[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,
});
return View();
}
[HttpPost]
public void CreateExcursion(string excursionName, string excursionDescription, double price, List<int> tours)
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(excursionName) || string.IsNullOrEmpty(excursionDescription) || price <= 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,
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,
});
return View(_excursionLogic.ReadElement(new ExcursionSearchModel
{
Id = id
}));
}
[HttpPost]
public void UpdateExcursion(int id, string excursionName, string excursionDescription, double price, List<int> tours)
{
if (LoggedinUser.User == null)
{
throw new Exception("Необходимо авторизоваться!");
}
if (string.IsNullOrEmpty(excursionName) || string.IsNullOrEmpty(excursionDescription) || price <= 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,
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");
}
}
}