FixErrorsForControllers

This commit is contained in:
Sergey Kozyrev 2024-05-29 15:37:04 +04:00
parent 17db7a656c
commit 2fa9375278
8 changed files with 288 additions and 166 deletions

View File

@ -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; }
}

View File

@ -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();
}

View File

@ -40,7 +40,7 @@ namespace DatabaseImplement.Models
public virtual List<WorkerMachine> Workers { get; set; } = new();
public virtual Guarantor Guarantor { get; set; }
[ForeignKey("MachineId")]
public virtual List<Product> Products { get; set; }
public virtual List<Product> Products { get; set; } = new();
public static Machine Create(MachineBindingModel model, FactoryGoWorkDatabase context)
{
return new Machine()

View File

@ -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<DetailViewModel>());
}
return RedirectToAction("IndexNonReg");
catch (Exception)
{
return View(new List<DetailViewModel>()); ;
}
return View(new List<DetailViewModel>());
}
[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)]

View File

@ -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)]

View File

@ -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");
}

View File

@ -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");
}
}

View File

@ -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<int> 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<IActionResult> ExcelGenerate()
{
var value = HttpContext.Session.GetString("Details");
if (value != null)
{
List<int> 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<int> 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<IActionResult> WordGenerate()
{
var value = HttpContext.Session.GetString("Details");
if (value != null)
{
List<int> 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<int> 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<int> 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<int> 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<DetailWorkshopReportViewModel>());
}
@ -147,7 +186,7 @@ namespace ImplementerApp.Controllers
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View();
return RedirectToAction("IndexNonReg", "Home");
}
}
}