CourseWork_FurnitureFactory/FurnitureFactory/ManagerWebClient/Controllers/HomeController.cs
2023-05-19 14:16:33 +04:00

411 lines
15 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using FurnitureContracts.BindingModels;
using FurnitureContracts.BusinessLogicsContracts;
using FurnitureContracts.SearchModels;
using FurnitureContracts.ViewModel;
using FurnitureFactoryDataBaseImplement.Models;
using FurnitureFactoryDataModels.Models;
using ManagerWebClient.Models;
using System.Diagnostics;
using System.IO;
namespace ManagerWebClient.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IHeadsetModuleLogic _headsetmodule;
private readonly IHeadsetLogic _headset;
private readonly IOrderLogic _order;
private readonly ISalesSalonsLogic _salessalon;
private readonly IManagerLogic _manager;
public HomeController(ILogger<HomeController> logger, IHeadsetLogic headset, IOrderLogic order , IHeadsetModuleLogic headsetmodule , ISalesSalonsLogic salessalon, IManagerLogic manager)
{
_logger = logger;
_headset = headset;
_order = order;
_headsetmodule = headsetmodule;
_salessalon = salessalon;
_manager = manager;
}
public IActionResult Index()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpGet]
public IActionResult Privacy()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.Manager);
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[HttpPost]
public void Privacy(string name, string email,string login, string password)
{
if (APIClient.Manager == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email))
{
throw new Exception("Введите данные");
}
_manager.Update( new ManagerBindingModel
{
Id = APIClient.Manager.Id,
UserName = name,
Email = login,
Password = password,
Login = login
});
APIClient.Manager.UserName = name;
APIClient.Manager.Email = email;
APIClient.Manager.Password = password;
APIClient.Manager.Login = login;
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Enter()
{
return View();
}
[HttpPost]
public void Enter(string login, string password)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
{
throw new Exception("Введите логин и пароль");
}
APIClient.Manager = _manager.ReadElement(new ManagerSearchModel { Login = login, Password = password });
if (APIClient.Manager == null)
{
throw new Exception("Неверный логин/пароль");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public void Register(string name, string email, string login, string password)
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email))
{
throw new Exception("Введите логин, пароль и ФИО, почту");
}
_manager.Create( new ManagerBindingModel
{
UserName = name,
Email = login,
Password = password,
Login = login
});
Response.Redirect("Enter");
return;
}
[HttpGet]
public IActionResult SalesSalons()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View(_salessalon.ReadList(new SalesSalonsSearchModel { ManagerId = APIClient.Manager.Id}));
}
[HttpGet]
public IActionResult CreateSalesSalon()
{
return View();
}
[HttpPost]
public void CreateSalesSalon(string name, string address)
{
if (APIClient.Manager == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(address))
{
throw new Exception("Введите название и адрес");
}
_salessalon.Create( new SalesSalonsBindingModel
{
ManagerId = APIClient.Manager.Id,
Name = name,
Address = address
});
Response.Redirect("SalesSalons");
}
[HttpGet]
public IActionResult SalesSalonSetting(int id)
{
return View(_salessalon.ReadElement(new SalesSalonsSearchModel { Id = id}));
}
[HttpPost]
public void UpdateSalesSalon(int id,string name, string address)
{
if (string.IsNullOrEmpty(name))
{
throw new Exception("Нет названия");
}
if (string.IsNullOrEmpty(address))
{
throw new Exception("Нет адреса");
}
_salessalon.Update( new SalesSalonsBindingModel
{
Id = id,
Name = name,
Address = address,
ManagerId = APIClient.Manager.Id,
});
Response.Redirect("/Home/SalesSalons");
}
public void DeleteSalesSalon(int id)
{
_salessalon.Delete(new SalesSalonsBindingModel
{
Id = id,
});
Response.Redirect("/Home/SalesSalons");
}
[HttpGet]
public IActionResult Headsetes()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View(_headset.ReadList(new HeadsetSearchModel { ManagerId = APIClient.Manager.Id}));
}
[HttpGet]
public IActionResult CreateHeadset()
{
ViewBag.HeadsetModules = _headsetmodule.ReadList(null);
var list = _salessalon.ReadList(new SalesSalonsSearchModel { ManagerId = APIClient.Manager.Id});
var simpSalesSalon = list.Select(x => new { SalesSalonId = x.Id, SalesSalonName = x.Name });
ViewBag.SalesSalons = new MultiSelectList(simpSalesSalon, "SalesSalonId", "SalesSalonName");
return View();
}
[HttpPost]
public void CreateHeadset(string title, string size, string dateCreate, int headsetmodule, int[] salessalons )
{
if (APIClient.Manager == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(size) || string.IsNullOrEmpty(dateCreate) || headsetmodule <=0 || salessalons.Length==0)
{
throw new Exception("Введите название и размер");
}
Dictionary<int, ISalesSalonsModel> headsetSalesSalons = new Dictionary<int, ISalesSalonsModel>();
foreach(int id in salessalons)
{
headsetSalesSalons.Add(id,_salessalon.ReadElement(new SalesSalonsSearchModel { Id = id}));
}
_headset.Create(new HeadsetBindingModel
{
ManagerId = APIClient.Manager.Id,
Title = title,
Size = size,
HeadsetModuleId = headsetmodule,
HeadsetSalesSalons = headsetSalesSalons
}) ;
Response.Redirect("Headsetes");
}
[HttpGet]
public IActionResult HeadsetSetting(int id)
{
var headset = _headset.ReadElement(new HeadsetSearchModel { Id = id });
var salessalons = _salessalon.ReadList(new SalesSalonsSearchModel { ManagerId = APIClient.Manager.Id}).Select(x => new {SalesSalonId = x.Id, SalesSalonName = x.Name}).ToList();
var selectedSalesSalons = headset.HeadsetSalesSalons.Select(x => x.Key).ToArray();
ViewBag.SalesSalons = new MultiSelectList(salessalons, "SalesSalonId", "SalesSalonName", selectedSalesSalons);
ViewBag.HeadsetModules = _headsetmodule.ReadList(null);
return View(headset);
}
[HttpPost]
public void UpdateHeadset(int idHeadset,string title, string size, int headsetmodule, int[] salessalons)
{
if (string.IsNullOrEmpty(title))
{
throw new Exception("Нет названия");
}
if (string.IsNullOrEmpty(size))
{
throw new Exception("Нет размера");
}
if (headsetmodule <= 0)
{
throw new Exception("Нет модуля гарнитура");
}
if (salessalons.Length == 0)
{
throw new Exception("Нет салонов продаж");
}
Dictionary<int, ISalesSalonsModel> headsetSalesSalons = new Dictionary<int, ISalesSalonsModel>();
foreach (int id in salessalons)
{
headsetSalesSalons.Add(id, _salessalon.ReadElement(new SalesSalonsSearchModel { Id = id }));
}
_headset.Update(new HeadsetBindingModel
{
Id = idHeadset,
ManagerId = APIClient.Manager.Id,
Title = title,
Size = size,
HeadsetModuleId = headsetmodule,
HeadsetSalesSalons = headsetSalesSalons
});
Response.Redirect("/Home/Headsetes");
}
public void DeleteHeadset(int id)
{
_headset.Delete( new HeadsetBindingModel
{
Id = id,
});
Response.Redirect("/Home/Headsetes");
}
[HttpGet]
public IActionResult Orders()
{
if (APIClient.Manager == null)
{
return Redirect("~/Home/Enter");
}
return View(_order.ReadList(new OrderSearchModel { ManagerId = APIClient.Manager.Id }));
}
[HttpGet]
public IActionResult CreateOrder()
{
var list = _salessalon.ReadList(new SalesSalonsSearchModel { ManagerId = APIClient.Manager.Id });
var simpSalesSalon = list.Select(x => new { SalesSalonId = x.Id, SalesSalonName = x.Name });
ViewBag.SalesSalons = new MultiSelectList(simpSalesSalon, "SalesSalonId", "SalesSalonName");
return View();
}
[HttpPost]
public void CreateOrder(string title, string status, string dateCreate, int[] salessalons)
{
if (APIClient.Manager == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(status) || string.IsNullOrEmpty(dateCreate)|| salessalons.Length == 0)
{
throw new Exception("Введите название и статус");
}
Dictionary<int, ISalesSalonsModel> orderSalesSalons = new Dictionary<int, ISalesSalonsModel>();
foreach (int id in salessalons)
{
orderSalesSalons.Add(id, _salessalon.ReadElement(new SalesSalonsSearchModel { Id = id }));
}
_order.Create(new OrdersBindingModel
{
ManagerId = APIClient.Manager.Id,
Title = title,
Status = status,
DateCreate = DateTime.SpecifyKind(DateTime.Parse(dateCreate), DateTimeKind.Utc),
OrdersSalesSalons = orderSalesSalons
});
Response.Redirect("Orders");
}
[HttpGet]
public IActionResult OrderSetting(int id)
{
var order = _order.ReadElement(new OrderSearchModel { Id = id });
var salessalons = _salessalon.ReadList(new SalesSalonsSearchModel { ManagerId = APIClient.Manager.Id }).Select(x => new { SalesSalonId = x.Id, SalesSalonName = x.Name }).ToList();
var selectedSalesSalons = order.OrdersSalesSalons.Select(x => x.Key).ToArray();
ViewBag.SalesSalons = new MultiSelectList(salessalons, "SalesSalonId", "SalesSalonName", selectedSalesSalons);
return View(order);
}
[HttpPost]
public void UpdateOrder(int idHeadset, string title, string status, string dateCreate, int[] salessalons)
{
if (string.IsNullOrEmpty(title))
{
throw new Exception("Нет названия");
}
if (string.IsNullOrEmpty(status))
{
throw new Exception("Нет статуса");
}
if (string.IsNullOrEmpty(dateCreate))
{
throw new Exception("Нет даты");
}
if (salessalons.Length == 0)
{
throw new Exception("Нет салонов продаж");
}
Dictionary<int, ISalesSalonsModel> orderSalesSalons = new Dictionary<int, ISalesSalonsModel>();
foreach (int id in salessalons)
{
orderSalesSalons.Add(id, _salessalon.ReadElement(new SalesSalonsSearchModel { Id = id }));
}
_order.Update(new OrdersBindingModel
{
Id = idHeadset,
ManagerId = APIClient.Manager.Id,
Title = title,
Status = status,
DateCreate = DateTime.SpecifyKind(DateTime.Parse(dateCreate), DateTimeKind.Utc),
OrdersSalesSalons = orderSalesSalons
});
Response.Redirect("/Home/Orders");
}
public void DeleteOrder(int id)
{
_order.Delete(new OrdersBindingModel
{
Id = id,
});
Response.Redirect("/Home/Orders");
}
public IActionResult Reports()
{
return View();
}
}
}