using FurnitureContracts.BindingModels; using FurnitureContracts.BusinessLogicsContracts; using FurnitureContracts.SearchModels; using FurnitureFactoryDataModels.Models; using MasterWebClient.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using System.Diagnostics; namespace MasterWebClient.Controllers { public class HomeController : Controller { private readonly ILogger _logger; private readonly IOrderLogic _order; private readonly IFurnitureLogic _furniture; private readonly IHeadsetModuleLogic _headsetModule; private readonly IMaterialLogic _material; private readonly IMasterLogic _master; private readonly IReportLogic _report; public HomeController(ILogger logger, IOrderLogic order, IFurnitureLogic furniture, IHeadsetModuleLogic headsetModule, IMaterialLogic material, IMasterLogic master, IReportLogic report) { _logger = logger; _order = order; _furniture = furniture; _headsetModule = headsetModule; _material = material; _master = master; _report = report; } [HttpGet] public IActionResult Index() { if (APIClient.Master == null) { return Redirect("~/Home/Enter"); } return View(); } public IActionResult Privacy() { if (APIClient.Master == null) { return Redirect("~/Home/Enter"); } return View(APIClient.Master); } [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 post, string email, string login, string password) { if (APIClient.Master == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(post)) { throw new Exception("Введите данные"); } _master.Update(new MasterBindingModel { Id = APIClient.Master.Id, Name = name, Email = email, Password = password, Login = login, Post = post }); APIClient.Master.Name = name; APIClient.Master.Email = email; APIClient.Master.Password = password; APIClient.Master.Login = login; APIClient.Master.Post = post; 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.Master = _master.ReadElement(new MasterSearchModel { Login = login, Password = password }); if (APIClient.Master == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Register() { return View(); } [HttpPost] public void Register(string name, string post, string email, string login, string password) { if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(post)) { throw new Exception("Введите ФИО, должность, почту, логин, пароль"); } _master.Create(new MasterBindingModel { Name = name, Email = email, Password = password, Login = login, Post = post }); Response.Redirect("Enter"); return; } [HttpGet] public IActionResult Materials() { if (APIClient.Master == null) { return Redirect("~/Home/Enter"); } return View(_material.ReadList(new MaterialSearchModel { MasterId = APIClient.Master.Id })); } [HttpGet] public IActionResult CreateMaterial() { return View(); } [HttpPost] public void CreateMaterial(string title, int cost) { if (APIClient.Master == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(cost.ToString())) { throw new Exception("Введите название и цену "); } _material.Create(new MaterialBindingModel { MasterId = APIClient.Master.Id, Name = title, Cost = cost }); Response.Redirect("Materials"); } [HttpGet] public IActionResult MaterialSetting(int id) { return View(_material.ReadElement(new MaterialSearchModel { Id = id })); } [HttpPost] public void UpdateMaterial(int id, string name, int cost) { if (string.IsNullOrEmpty(name)) { throw new Exception("Нет названия"); } if (string.IsNullOrEmpty(cost.ToString())) { throw new Exception("Нет цены "); } _material.Update(new MaterialBindingModel { Id = id, Name = name, Cost = cost, MasterId = APIClient.Master.Id, }); Response.Redirect("/Home/Materials"); } public void DeleteMaterial(int id) { _material.Delete(new MaterialBindingModel { Id = id, }); Response.Redirect("/Home/Materials"); } [HttpGet] public IActionResult Furnitures() { if (APIClient.Master == null) { return Redirect("~/Home/Enter"); } return View(_furniture.ReadList(new FurnitureSearchModel { MasterId = APIClient.Master.Id })); } [HttpGet] public IActionResult CreateFurniture() { ViewBag.Orders = _order.ReadList(null); var list = _material.ReadList(new MaterialSearchModel { MasterId = APIClient.Master.Id }); var simpMaterial = list.Select(x => new { MaterialId = x.Id, MaterialName = x.Name }); ViewBag.Materials = new MultiSelectList(simpMaterial, "MaterialId", "MaterialName"); return View(); } [HttpPost] public void CreateFurniture(string title, string dateEvent, int order, int[] materials) { if (APIClient.Master == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(dateEvent) && order <= 0 || materials.Length == 0) { throw new Exception("Введите название и дату"); } Dictionary furnitureMaterials = new Dictionary(); foreach (int id in materials) { furnitureMaterials.Add(id, _material.ReadElement(new MaterialSearchModel { Id = id })); } _furniture.Create(new FurnitureBindingModel { MasterId = APIClient.Master.Id, MasterName = APIClient.Master.Name, Name = title, DateTo = DateTime.SpecifyKind(DateTime.Parse(dateEvent), DateTimeKind.Utc), OrdersId = order, OrdersName = _order.ReadElement(new OrderSearchModel { Id = order }).Title, FurnitureMaterials = furnitureMaterials }); Response.Redirect("Furnitures"); } [HttpGet] public IActionResult FurnitureSetting(int id) { var furniture = _furniture.ReadElement(new FurnitureSearchModel { Id = id }); var materials = _material.ReadList(new MaterialSearchModel { MasterId = APIClient.Master.Id }).Select(x => new { MaterialId = x.Id, MaterialName = x.Name }).ToList(); var selectedMaterials = furniture.FurnitureMaterials.Select(x => x.Key).ToArray(); ViewBag.Materials = new MultiSelectList(materials, "MaterialId", "MaterialName", selectedMaterials); ViewBag.Orders = _order.ReadList(null); return View(furniture); } [HttpPost] public void UpdateFurniture(int idFurniture, string title, string dateEvent, int order, int[] materials) { if (string.IsNullOrEmpty(title)) { throw new Exception("Нет названия"); } if (string.IsNullOrEmpty(dateEvent)) { throw new Exception("Нет даты"); } if (order <= 0) { throw new Exception("Нет изделия"); } if (materials.Length == 0) { throw new Exception("Нет материалов"); } Dictionary furnitureMaterials = new Dictionary(); foreach (int id in materials) { furnitureMaterials.Add(id, _material.ReadElement(new MaterialSearchModel { Id = id })); } _furniture.Update(new FurnitureBindingModel { Id = idFurniture, MasterId = APIClient.Master.Id, MasterName = APIClient.Master.Name, Name = title, DateTo = DateTime.SpecifyKind(DateTime.Parse(dateEvent), DateTimeKind.Utc), OrdersId = order, OrdersName = _order.ReadElement(new OrderSearchModel { Id = order }).Title, FurnitureMaterials = furnitureMaterials }); Response.Redirect("/Home/Furnitures"); } public void DeleteFurniture(int id) { _furniture.Delete(new FurnitureBindingModel { Id = id, }); Response.Redirect("/Home/Furnitures"); } [HttpGet] public IActionResult HeadsetModules() { if (APIClient.Master == null) { return Redirect("~/Home/Enter"); } return View(_headsetModule.ReadList(new HeadsetModuleSearchModel { MasterId = APIClient.Master.Id })); } [HttpGet] public IActionResult CreateHeadsetModule() { var list = _material.ReadList(new MaterialSearchModel { MasterId = APIClient.Master.Id }); var simpMaterial = list.Select(x => new { MaterialId = x.Id, MaterialName = x.Name }); ViewBag.Materials = new MultiSelectList(simpMaterial, "MaterialId", "MaterialName"); return View(); } [HttpPost] public void CreateHeadsetModule(string title, string dateIssue, string dateDelivery, int[] materials) { if (APIClient.Master == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(dateIssue) || string.IsNullOrEmpty(dateDelivery) || materials.Length == 0) { throw new Exception("Введите название и даты"); } if (DateTime.SpecifyKind(DateTime.Parse(dateIssue), DateTimeKind.Utc) >= DateTime.SpecifyKind(DateTime.Parse(dateDelivery), DateTimeKind.Utc)) { throw new Exception("Дата выдачи должна быть раньше срока сдачи"); } Dictionary headsetModuleMaterials = new Dictionary(); foreach (int id in materials) { headsetModuleMaterials.Add(id, _material.ReadElement(new MaterialSearchModel { Id = id })); } _headsetModule.Create(new HeadsetModuleBindingModel { MasterId = APIClient.Master.Id, MasterName = APIClient.Master.Name, Name = title, DateIssue = DateTime.SpecifyKind(DateTime.Parse(dateIssue), DateTimeKind.Utc), DateDelivery = DateTime.SpecifyKind(DateTime.Parse(dateDelivery), DateTimeKind.Utc), HeadsetModuleMaterial = headsetModuleMaterials }); Response.Redirect("HeadsetModules"); } [HttpGet] public IActionResult HeadsetModuleSetting(int id) { var headsetModule = _headsetModule.ReadElement(new HeadsetModuleSearchModel { Id = id }); var materials = _material.ReadList(new MaterialSearchModel { MasterId = APIClient.Master.Id }).Select(x => new { MaterialId = x.Id, MaterialName = x.Name }).ToList(); var selectedMaterials = headsetModule.HeadsetModuleMaterial.Select(x => x.Key).ToArray(); ViewBag.Materials = new MultiSelectList(materials, "MaterialId", "MaterialName", selectedMaterials); return View(headsetModule); } [HttpPost] public void UpdateHeadsetModule(int idHeadsetModule, string title, string dateIssue, string dateDelivery, int[] materials) { if (string.IsNullOrEmpty(title)) { throw new Exception("Нет названия"); } if (string.IsNullOrEmpty(dateIssue)) { throw new Exception("Нет даты выдачи"); } if (string.IsNullOrEmpty(dateDelivery)) { throw new Exception("Нет срока сдачи"); } if (materials.Length == 0) { throw new Exception("Нет материалов"); } if (DateTime.SpecifyKind(DateTime.Parse(dateIssue), DateTimeKind.Utc) >= DateTime.SpecifyKind(DateTime.Parse(dateDelivery), DateTimeKind.Utc)) { throw new Exception("Дата выдачи должна быть раньше срока сдачи"); } Dictionary headsetModuleMaterials = new Dictionary(); foreach (int id in materials) { headsetModuleMaterials.Add(id, _material.ReadElement(new MaterialSearchModel { Id = id })); } _headsetModule.Update(new HeadsetModuleBindingModel { Id = idHeadsetModule, MasterId = APIClient.Master.Id, MasterName = APIClient.Master.Name, Name = title, DateIssue = DateTime.SpecifyKind(DateTime.Parse(dateIssue), DateTimeKind.Utc), DateDelivery = DateTime.SpecifyKind(DateTime.Parse(dateDelivery), DateTimeKind.Utc), HeadsetModuleMaterial = headsetModuleMaterials }); Response.Redirect("/Home/HeadsetModules"); } public void DeleteHeadsetModule(int id) { _headsetModule.Delete(new HeadsetModuleBindingModel { Id = id, }); Response.Redirect("/Home/HeadsetModules"); } public IActionResult Reports() { var list = _material.ReadList(new MaterialSearchModel { MasterId = APIClient.Master.Id }); var simpMaterial = list.Select(x => new { MaterialId = x.Id, MaterialName = x.Name }); ViewBag.Materials = new MultiSelectList(simpMaterial, "MaterialId", "MaterialName"); return View(); } } }