diff --git a/Course/Contracts/SearchModels/MachineSearchModel.cs b/Course/Contracts/SearchModels/MachineSearchModel.cs index 4dfdf81..e562f82 100644 --- a/Course/Contracts/SearchModels/MachineSearchModel.cs +++ b/Course/Contracts/SearchModels/MachineSearchModel.cs @@ -6,7 +6,7 @@ public string? Title { get; set; } public int? UserId { get; set; } public int? WorkerId { get; set; } - public int? WorkshopId { get; set; } + public int? WorkshopId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/Course/DatabaseImplement/Implements/MachineStorage.cs b/Course/DatabaseImplement/Implements/MachineStorage.cs index 52331b1..bda3d90 100644 --- a/Course/DatabaseImplement/Implements/MachineStorage.cs +++ b/Course/DatabaseImplement/Implements/MachineStorage.cs @@ -36,7 +36,7 @@ namespace DatabaseImplement.Implements using var context = new FactoryGoWorkDatabase(); if (model.DateFrom.HasValue) return context.Machines.Where(x => x.UserId == model.UserId).Where(x => x.DateCreate <= model.DateTo && x.DateCreate >= model.DateFrom).Select(x => x.GetViewModel).ToList(); - else + else return context.Machines.Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList(); } diff --git a/Course/DatabaseImplement/Models/Machine.cs b/Course/DatabaseImplement/Models/Machine.cs index 2604a85..d06f436 100644 --- a/Course/DatabaseImplement/Models/Machine.cs +++ b/Course/DatabaseImplement/Models/Machine.cs @@ -40,7 +40,7 @@ namespace DatabaseImplement.Models public virtual List Workers { get; set; } = new(); public virtual Guarantor Guarantor { get; set; } [ForeignKey("MachineId")] - public virtual List Products { get; set; } + public virtual List Products { get; set; } = new(); public static Machine Create(MachineBindingModel model, FactoryGoWorkDatabase context) { return new Machine() diff --git a/Course/ImplementerApp/Controllers/DetailController.cs b/Course/ImplementerApp/Controllers/DetailController.cs index 21c1413..1dbdef0 100644 --- a/Course/ImplementerApp/Controllers/DetailController.cs +++ b/Course/ImplementerApp/Controllers/DetailController.cs @@ -20,19 +20,24 @@ namespace ImplementerApp.Controllers [HttpGet] public IActionResult IndexDetail() { - if (UserImplementer.user != null) + if (!IsLoggedIn) + return RedirectToAction("IndexNonReg", "Home"); + try { - var list = _data.GetDetails(UserImplementer.user.Id); + var list = _data.GetDetails(UserImplementer.user!.Id); if (list != null) return View(list); - return View(new List()); } - return RedirectToAction("IndexNonReg"); + catch (Exception) + { + return View(new List()); ; + } + return View(new List()); } [HttpPost] public void IndexDetail(int id) { - if (UserImplementer.user != null) + if (IsLoggedIn) { _data.DeleteDetail(id); } @@ -41,6 +46,10 @@ namespace ImplementerApp.Controllers [HttpGet] public IActionResult CreateDetail(int id) { + if (!IsLoggedIn) + { + return RedirectToAction("IndexNonReg", "Home"); + } if (id != 0) { var value = _data.GetDetail(id); @@ -52,24 +61,25 @@ namespace ImplementerApp.Controllers [HttpPost] public IActionResult CreateDetail(DetailBindingModel model) { - if (model.Id == 0) + try { - model.DateCreate = DateTime.Now; - model.UserId = UserImplementer.user!.Id; - try { + if (model.Id == 0) + { + model.DateCreate = DateTime.Now; + model.UserId = UserId; if (_data.CreateDetail(model)) return RedirectToAction("IndexDetail"); - } catch (ArgumentException ex) - { - return RedirectToAction("Error", ex.Message); } - } - else + else + { + if (_data.UpdateDetail(model)) + return RedirectToAction("IndexDetail"); + } + } catch (Exception) { - if (_data.UpdateDetail(model)) - return RedirectToAction("IndexDetail"); + return RedirectToAction("IndexDetail"); } - return View(); + return RedirectToAction("IndexDetail"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] diff --git a/Course/ImplementerApp/Controllers/HomeController.cs b/Course/ImplementerApp/Controllers/HomeController.cs index 6aaf48d..96cfb6e 100644 --- a/Course/ImplementerApp/Controllers/HomeController.cs +++ b/Course/ImplementerApp/Controllers/HomeController.cs @@ -25,8 +25,14 @@ namespace ImplementerApp.Controllers [HttpPost] public JsonResult CheckLogin(string login) { - var unique = _data.CheckLogin(login); - return Json(new { isUnique = unique }); + try + { + var unique = _data.CheckLogin(login); + return Json(new { isUnique = unique }); + } + catch (Exception) { + return Json(new { isUnique = false }); + } } public IActionResult IndexNonReg() { @@ -50,13 +56,19 @@ namespace ImplementerApp.Controllers [HttpPost] public void Enter(string login, string password) { - var user = _data.Login(login, password); - if (user != null) + try { + var user = _data.Login(login, password); + if (user != null) + { + UserImplementer.user = user; + Response.Redirect("Index"); + } + Response.Redirect("Enter"); + } catch (Exception) { - UserImplementer.user = user; - Response.Redirect("Index"); + Response.Redirect("IndexNonReg"); } - Response.Redirect("Enter"); + } [HttpGet] public IActionResult Register() @@ -71,9 +83,15 @@ namespace ImplementerApp.Controllers [HttpPost] public void Register(string name, string login, string email, string password1, string password2) { - if (password1 == password2 && _data.Register(new() { Email = email, Login = login, Name = name, Password = password1 })) + try { - Response.Redirect("Index"); + if (password1 == password2 && _data.Register(new() { Email = email, Login = login, Name = name, Password = password1 })) + { + Response.Redirect("Index"); + } + } catch (Exception) + { + Response.Redirect("IndexNonReg"); } } [HttpGet] @@ -88,12 +106,17 @@ namespace ImplementerApp.Controllers { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); - ImplementerBindingModel user = new() { Id = id, Login = login, Email = email, Password = password, Name = name }; - if (_data.UpdateUser(user)) + try { + ImplementerBindingModel user = new() { Id = id, Login = login, Email = email, Password = password, Name = name }; + if (_data.UpdateUser(user)) + { + UserImplementer.user = new ImplementerViewModel { Id = id, Login = login, Password = password, Name = name, Email = email }; + } + return View(user); + } catch (Exception) { - UserImplementer.user = new ImplementerViewModel { Id = id, Login = login, Password = password, Name = name, Email = email }; + return RedirectToAction("IndexNonReg"); } - return View(user); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] diff --git a/Course/ImplementerApp/Controllers/ProductController.cs b/Course/ImplementerApp/Controllers/ProductController.cs index c52a317..cf80a86 100644 --- a/Course/ImplementerApp/Controllers/ProductController.cs +++ b/Course/ImplementerApp/Controllers/ProductController.cs @@ -23,52 +23,70 @@ namespace ImplementerApp.Controllers { if (IsLoggedIn) { - var products = _data.GetProducts(UserImplementer.user!.Id); - return View(products); + try { + var products = _data.GetProducts(UserImplementer.user!.Id); + return View(products); + } catch (Exception) + { + return RedirectToAction("IndexNonReg", "Home"); + } } - return RedirectToAction("IndexNonReg"); + return RedirectToAction("IndexNonReg", "Home"); } [HttpPost] public IActionResult IndexProduct(int id) { - _data.DeleteProduct(id); + try + { + _data.DeleteProduct(id); + } + catch (Exception) + { } return RedirectToAction("IndexProduct"); } [HttpGet] public IActionResult CreateProduct(int id) { - var details = _data.GetDetails(UserImplementer.user!.Id); - ViewBag.AllDetails = details; - if (id != 0) + if (!IsLoggedIn) + return RedirectToAction("IndexNonReg", "Home"); + try { - var value = _data.GetProduct(id); - if (value != null) - return View(value); + var details = _data.GetDetails(UserImplementer.user!.Id); + ViewBag.AllDetails = details; + if (id != 0) + { + var value = _data.GetProduct(id); + if (value != null) + return View(value); + } } + catch (Exception) + { } return View(new ProductViewModel()); } [HttpPost] public IActionResult CreateProduct(int id, string title, int[] detailIds, int[] counts) { - ProductBindingModel model = new ProductBindingModel(); - model.Id = id; - model.Name = title; - model.UserId = UserImplementer.user!.Id; - var details = _data.GetDetails(UserImplementer.user!.Id); - double sum = 0; - for (int i = 0; i < detailIds.Length; i++) - { - var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!; - if (counts[i] <= 0) - continue; - model.ProductDetails[detailIds[i]] = (detail, counts[i]); - sum += detail.Cost * counts[i]; - } - if (model.ProductDetails.Count == 0) - return RedirectToAction("IndexProduct"); - model.Cost = sum; try { + ProductBindingModel model = new ProductBindingModel(); + model.Id = id; + model.Name = title; + model.UserId = UserImplementer.user!.Id; + var details = _data.GetDetails(UserImplementer.user!.Id); + double sum = 0; + for (int i = 0; i < detailIds.Length; i++) + { + var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!; + if (counts[i] <= 0) + continue; + model.ProductDetails[detailIds[i]] = (detail, counts[i]); + sum += detail.Cost * counts[i]; + } + if (model.ProductDetails.Count == 0) + return RedirectToAction("IndexProduct"); + model.Cost = sum; + if (id != 0) { _data.UpdateProduct(model); @@ -89,22 +107,29 @@ namespace ImplementerApp.Controllers public IActionResult ProductMachineAdd(int id) { if (!IsLoggedIn) - return RedirectToAction("IndexNonReg"); - var product = _data.GetProduct(id); - ViewBag.Product = product; - var machines = _data.GetMachines(); - return View(machines); + return RedirectToAction("IndexNonReg", "Home"); + try + { + var product = _data.GetProduct(id); + ViewBag.Product = product; + var machines = _data.GetMachines(); + return View(machines); + } catch (Exception) + { + return RedirectToAction("IndexProduct"); + } } [HttpPost] public IActionResult ProductMachineAdd(int productId, int machineId) { - if (!IsLoggedIn) - return RedirectToAction("IndexNonReg"); - var product = _data.GetProduct(productId); - if (product == null) - return RedirectToAction("Index"); - ProductBindingModel productBinding = new() { Id = productId, Cost = product.Cost, Name = product.Name, UserId = product.UserId, ProductDetails = product.DetailProducts, MachineId = machineId }; - _data.UpdateProduct(productBinding); + try { + var product = _data.GetProduct(productId); + if (product == null) + return RedirectToAction("Index"); + ProductBindingModel productBinding = new() { Id = productId, Cost = product.Cost, Name = product.Name, UserId = product.UserId, ProductDetails = product.DetailProducts, MachineId = machineId }; + _data.UpdateProduct(productBinding); + } catch (Exception) + { } return RedirectToAction("IndexProduct"); } diff --git a/Course/ImplementerApp/Controllers/ProductionController.cs b/Course/ImplementerApp/Controllers/ProductionController.cs index c19e39a..bb46487 100644 --- a/Course/ImplementerApp/Controllers/ProductionController.cs +++ b/Course/ImplementerApp/Controllers/ProductionController.cs @@ -19,66 +19,91 @@ namespace ImplementerApp.Controllers [HttpGet] public IActionResult IndexProduction() { - if (UserImplementer.user != null) + if (!IsLoggedIn) + return RedirectToAction("IndexNonReg", "Home"); + try { - var productions = _data.GetProductions(UserImplementer.user.Id); + var productions = _data.GetProductions(UserImplementer.user!.Id); return View(productions); + } catch + { + return RedirectToAction("IndexNonReg", "Home"); } - return RedirectToAction("IndexNonReg"); } [HttpPost] public IActionResult IndexProduction(int id) { - _data.DeleteProduction(id); - return RedirectToAction("IndexProduction"); + try + { + _data.DeleteProduction(id); + return RedirectToAction("IndexProduction"); + + } catch + { + return RedirectToAction("IndexNonReg"); + } } [HttpGet] public IActionResult CreateProduction(int id) { - var details = _data.GetDetails(UserImplementer.user!.Id); - ViewBag.AllDetails = details; - if (id != 0) + if (!IsLoggedIn) + return RedirectToAction("IndexNonReg", "Home"); + try { - var value = _data.GetProduction(id); - if (value != null) - return View(value); + var details = _data.GetDetails(UserImplementer.user!.Id); + ViewBag.AllDetails = details; + if (id != 0) + { + var value = _data.GetProduction(id); + if (value != null) + return View(value); + } + } catch + { + return RedirectToAction("IndexProduction"); } return View(new ProductionViewModel()); } [HttpPost] public IActionResult CreateProduction(int id, string title, int[] detailIds) { - ProductionBindingModel model = new ProductionBindingModel(); - model.Id = id; - model.Name = title; - model.UserId = UserImplementer.user!.Id; - var details = _data.GetDetails(UserImplementer.user!.Id); - double sum = 0; - for (int i = 0; i < detailIds.Length; i++) + try { - var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!; - model.ProductionDetails[detailIds[i]] = detail; - sum += detail.Cost; - } - model.Cost = sum; - bool changed = false; - if (model.ProductionDetails.Count > 0) - { - if (id != 0) + ProductionBindingModel model = new ProductionBindingModel(); + model.Id = id; + model.Name = title; + model.UserId = UserImplementer.user!.Id; + var details = _data.GetDetails(UserImplementer.user!.Id); + double sum = 0; + for (int i = 0; i < detailIds.Length; i++) { - changed = _data.UpdateProduction(model); + var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!; + model.ProductionDetails[detailIds[i]] = detail; + sum += detail.Cost; } + model.Cost = sum; + bool changed = false; + if (model.ProductionDetails.Count > 0) + { + if (id != 0) + { + changed = _data.UpdateProduction(model); + } + else + { + changed = _data.CreateProduction(model); + } + } + if (changed) + return RedirectToAction("IndexProduction"); else { - changed = _data.CreateProduction(model); + ViewBag.AllDetails = details; + return View(model); } - } - if (changed) - return RedirectToAction("IndexProduction"); - else + } catch { - ViewBag.AllDetails = details; - return View(model); + return RedirectToAction("IndexProduction"); } } diff --git a/Course/ImplementerApp/Controllers/ReportController.cs b/Course/ImplementerApp/Controllers/ReportController.cs index 7403caa..b7b102f 100644 --- a/Course/ImplementerApp/Controllers/ReportController.cs +++ b/Course/ImplementerApp/Controllers/ReportController.cs @@ -22,22 +22,12 @@ namespace ImplementerApp.Controllers public IActionResult DetailTimeChoose() { if (!IsLoggedIn) - return RedirectToAction("IndexNonReg"); + return RedirectToAction("IndexNonReg", "Home"); return View(); } [HttpPost] - public IActionResult SendReport(DateTime startDate, DateTime endDate) - { - - return Ok(); - } - [HttpPost] public IActionResult TimeReportWeb(DateTime startDate, DateTime endDate) { - if (!IsLoggedIn) - return RedirectToAction("IndexNonReg"); - - HttpContext.Session.SetString("StartDate", startDate.ToString()); HttpContext.Session.SetString("EndDate", endDate.ToString()); @@ -46,6 +36,9 @@ namespace ImplementerApp.Controllers [HttpGet] public IActionResult DetailTimeReport() { + if (!IsLoggedIn) + return RedirectToAction("IndexNonReg", "Home"); + try { var startDateStr = HttpContext.Session.GetString("StartDate"); var endDateStr = HttpContext.Session.GetString("EndDate"); var startDate = DateTime.Parse(startDateStr); @@ -57,85 +50,131 @@ namespace ImplementerApp.Controllers ViewBag.EndDate = endDate; return View(values); + } catch + { + return RedirectToAction("Index", "Home"); + } } [HttpPost] public void DetailTimeMail() { - var startDateStr = HttpContext.Session.GetString("StartDate"); - var endDateStr = HttpContext.Session.GetString("EndDate"); - var startDate = DateTime.Parse(startDateStr); - var endDate = DateTime.Parse(endDateStr).AddDays(1); - using (MemoryStream memoryStream = new MemoryStream()) + try { - _data.SendMailReport(startDate, endDate, UserId, memoryStream); + var startDateStr = HttpContext.Session.GetString("StartDate"); + var endDateStr = HttpContext.Session.GetString("EndDate"); + var startDate = DateTime.Parse(startDateStr); + var endDate = DateTime.Parse(endDateStr).AddDays(1); + using (MemoryStream memoryStream = new MemoryStream()) + { + _data.SendMailReport(startDate, endDate, UserId, memoryStream); + } + Response.Redirect("DetailTimeReport"); + } + catch + { + Response.Redirect("Error"); } - Response.Redirect("DetailTimeReport"); } [HttpGet] public IActionResult DetailWorkshopChoose() { if (!IsLoggedIn) - return RedirectToAction("IndexNonReg"); - var details = _data.GetDetails(UserId); - return View(details); + return RedirectToAction("IndexNonReg", "Home"); + try + { + var details = _data.GetDetails(UserId); + return View(details); + } + catch + { + return RedirectToAction("Error"); + } } [HttpPost] public IActionResult DetailWorkshopChoose(List selectedItems, string reportType) { - string value = string.Join("/", selectedItems); - HttpContext.Session.SetString("Details", value); - if (reportType.Equals("default")) - return RedirectToAction("DetailWorkshopReport"); - else if (reportType.Equals("excel")) - return RedirectToAction("ExcelGenerate"); - else - return RedirectToAction("WordGenerate"); + try + { + string value = string.Join("/", selectedItems); + HttpContext.Session.SetString("Details", value); + if (reportType.Equals("default")) + return RedirectToAction("DetailWorkshopReport"); + else if (reportType.Equals("excel")) + return RedirectToAction("ExcelGenerate"); + else + return RedirectToAction("WordGenerate"); + } + catch + { + return RedirectToAction("Error"); + } } public async Task ExcelGenerate() { - var value = HttpContext.Session.GetString("Details"); - if (value != null) - { - List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); - using (MemoryStream memoryStream = new MemoryStream()) + try { + var value = HttpContext.Session.GetString("Details"); + if (value != null) { - _data.SaveReportExcel(rawReports, memoryStream); - memoryStream.Seek(0, SeekOrigin.Begin); - var outputStream = new MemoryStream(); - await memoryStream.CopyToAsync(outputStream); - outputStream.Seek(0, SeekOrigin.Begin); - return File(outputStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ReportExcel.xlsx"); + List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); + using (MemoryStream memoryStream = new MemoryStream()) + { + _data.SaveReportExcel(rawReports, memoryStream); + memoryStream.Seek(0, SeekOrigin.Begin); + var outputStream = new MemoryStream(); + await memoryStream.CopyToAsync(outputStream); + outputStream.Seek(0, SeekOrigin.Begin); + return File(outputStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ReportExcel.xlsx"); + } } + return RedirectToAction("DetailWorkshopChoose"); + } + catch + { + return RedirectToAction("Error"); } - return RedirectToAction("DetailWorkshopChoose"); } public async Task WordGenerate() { - var value = HttpContext.Session.GetString("Details"); - if (value != null) - { - List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); - using (MemoryStream memoryStream = new MemoryStream()) + try { + var value = HttpContext.Session.GetString("Details"); + if (value != null) { - _data.SaveReportWord(rawReports, memoryStream); - memoryStream.Seek(0, SeekOrigin.Begin); - var outputStream = new MemoryStream(); - await memoryStream.CopyToAsync(outputStream); - outputStream.Seek(0, SeekOrigin.Begin); - return File(outputStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ReportWord.docx"); + List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); + using (MemoryStream memoryStream = new MemoryStream()) + { + _data.SaveReportWord(rawReports, memoryStream); + memoryStream.Seek(0, SeekOrigin.Begin); + var outputStream = new MemoryStream(); + await memoryStream.CopyToAsync(outputStream); + outputStream.Seek(0, SeekOrigin.Begin); + return File(outputStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ReportWord.docx"); + } } + return RedirectToAction("DetailWorkshopChoose"); + } + catch + { + return RedirectToAction("Error"); } - return RedirectToAction("DetailWorkshopChoose"); } [HttpGet] public IActionResult DetailWorkshopReport() { - var value = HttpContext.Session.GetString("Details"); - if (value != null) + if (!IsLoggedIn) + return RedirectToAction("IndexNonReg", "Home"); + try { - List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); - var reports = _data.GetWorkshopReports(rawReports); - return View(reports); + var value = HttpContext.Session.GetString("Details"); + if (value != null) + { + List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); + var reports = _data.GetWorkshopReports(rawReports); + return View(reports); + } + } + catch + { + return RedirectToAction("Error"); } return View(new List()); } @@ -147,7 +186,7 @@ namespace ImplementerApp.Controllers [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { - return View(); + return RedirectToAction("IndexNonReg", "Home"); } } }