Coursach/Course/ImplementerApp/Controllers/HomeController.cs

416 lines
12 KiB
C#
Raw Normal View History

2024-04-28 17:11:40 +04:00
using ImplementerApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using BusinessLogic.BusinessLogic;
using Contracts.BusinessLogicsContracts;
2024-04-29 22:21:21 +04:00
using Contracts.ViewModels;
using DataModels.Models;
2024-05-23 20:02:34 +04:00
using Contracts.BindingModels;
2024-05-26 23:23:24 +04:00
using DatabaseImplement.Models;
2024-04-28 17:11:40 +04:00
namespace ImplementerApp.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
2024-05-23 20:02:34 +04:00
private readonly ImplementerData _data;
public HomeController(ILogger<HomeController> logger, ImplementerData data)
2024-04-28 17:11:40 +04:00
{
_logger = logger;
2024-05-23 20:02:34 +04:00
_data = data;
}
2024-05-28 19:21:01 +04:00
private bool IsLoggedIn { get { return UserImplementer.user != null; } }
2024-05-26 23:23:24 +04:00
private int UserId { get { return UserImplementer.user!.Id; } }
2024-05-23 20:02:34 +04:00
public IActionResult IndexNonReg()
{
2024-05-25 21:36:21 +04:00
if (!IsLoggedIn)
2024-05-23 20:02:34 +04:00
return View();
return RedirectToAction("Index");
2024-04-28 17:11:40 +04:00
}
public IActionResult Index()
{
2024-05-25 21:36:21 +04:00
if (!IsLoggedIn)
2024-05-23 20:02:34 +04:00
return RedirectToAction("IndexNonReg");
2024-04-28 17:11:40 +04:00
return View();
}
2024-05-23 20:02:34 +04:00
[HttpGet]
public IActionResult Enter()
{
2024-05-25 21:36:21 +04:00
if (!IsLoggedIn)
2024-05-23 20:02:34 +04:00
return View();
return RedirectToAction("Index");
}
2024-05-23 20:02:34 +04:00
[HttpPost]
public void Enter(string login, string password)
{
var user = _data.Login(login, password);
if (user != null)
{
UserImplementer.user = user;
Response.Redirect("Index");
}
}
[HttpGet]
public IActionResult Register()
{
return View();
}
2024-05-26 23:23:24 +04:00
public IActionResult Logout()
{
UserImplementer.user = null;
return RedirectToAction("IndexNonReg");
}
2024-05-23 20:02:34 +04:00
[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 }))
{
Response.Redirect("Index");
}
}
[HttpGet]
2024-04-28 17:11:40 +04:00
public IActionResult IndexDetail()
{
2024-05-23 20:02:34 +04:00
if (UserImplementer.user != null)
{
2024-05-23 20:02:34 +04:00
var list = _data.GetDetails(UserImplementer.user.Id);
if (list != null)
return View(list);
return View(new List<DetailViewModel>());
}
return RedirectToAction("IndexNonReg");
2024-04-28 17:11:40 +04:00
}
2024-05-23 20:02:34 +04:00
[HttpPost]
public void IndexDetail(int id)
{
if (UserImplementer.user != null)
{
_data.DeleteDetail(id);
}
Response.Redirect("IndexDetail");
}
[HttpGet]
2024-05-28 19:21:01 +04:00
public IActionResult CreateDetail(int id)
2024-05-23 20:02:34 +04:00
{
if (id != 0)
{
var value = _data.GetDetail(id);
if (value != null)
return View(value);
}
return View(new DetailViewModel());
2024-05-28 19:21:01 +04:00
}
2024-05-23 20:02:34 +04:00
[HttpPost]
public IActionResult CreateDetail(DetailBindingModel model)
{
if (model.Id == 0)
{
model.DateCreate = DateTime.Now;
model.UserId = UserImplementer.user!.Id;
if (_data.CreateDetail(model))
return RedirectToAction("IndexDetail");
}
else
{
if (_data.UpdateDetail(model))
return RedirectToAction("IndexDetail");
}
return View();
}
[HttpGet]
2024-05-28 19:21:01 +04:00
public IActionResult IndexProduct()
{
2024-05-25 21:36:21 +04:00
if (IsLoggedIn) {
var products = _data.GetProducts(UserImplementer.user!.Id);
2024-05-23 20:02:34 +04:00
return View(products);
}
return RedirectToAction("IndexNonReg");
}
2024-05-25 21:36:21 +04:00
[HttpPost]
public IActionResult IndexProduct(int id)
{
_data.DeleteProduct(id);
return RedirectToAction("IndexProduct");
}
[HttpGet]
2024-05-28 19:21:01 +04:00
public IActionResult CreateProduct(int id)
{
2024-05-25 21:36:21 +04:00
var details = _data.GetDetails(UserImplementer.user!.Id);
ViewBag.AllDetails = details;
2024-05-28 19:21:01 +04:00
if (id != 0)
2024-05-25 21:36:21 +04:00
{
var value = _data.GetProduct(id);
if (value != null)
return View(value);
}
2024-05-28 19:21:01 +04:00
return View(new ProductViewModel());
}
2024-05-25 21:36:21 +04:00
[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;
2024-05-28 19:21:01 +04:00
model.ProductDetails[detailIds[i]] = (detail, counts[i]);
2024-05-25 21:36:21 +04:00
sum += detail.Cost * counts[i];
}
if (model.ProductDetails.Count == 0)
2024-05-28 19:21:01 +04:00
return RedirectToAction("IndexProduct");
model.Cost = sum;
2024-05-25 21:36:21 +04:00
if (id != 0)
{
_data.UpdateProduct(model);
}
else
{
_data.CreateProduct(model);
}
return RedirectToAction("IndexProduct");
2024-05-28 19:21:01 +04:00
}
2024-05-23 20:02:34 +04:00
[HttpGet]
2024-05-28 19:21:01 +04:00
public IActionResult IndexProduction()
{
2024-05-23 20:02:34 +04:00
if (UserImplementer.user != null)
{
2024-05-23 20:02:34 +04:00
var productions = _data.GetProductions(UserImplementer.user.Id);
return View(productions);
}
return RedirectToAction("IndexNonReg");
}
2024-05-25 21:36:21 +04:00
[HttpPost]
public IActionResult IndexProduction(int id)
{
_data.DeleteProduction(id);
return RedirectToAction("IndexProduction");
}
[HttpGet]
2024-05-28 19:21:01 +04:00
public IActionResult CreateProduction(int id)
{
var details = _data.GetDetails(UserImplementer.user!.Id);
2024-05-25 21:36:21 +04:00
ViewBag.AllDetails = details;
if (id != 0)
{
var value = _data.GetProduction(id);
if (value != null)
return View(value);
}
2024-05-28 19:21:01 +04:00
return View(new ProductionViewModel());
}
2024-05-25 21:36:21 +04:00
[HttpPost]
public IActionResult CreateProduction(int id, string title, int[] detailIds)
{
2024-05-28 19:21:01 +04:00
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++)
{
var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!;
model.ProductionDetails[detailIds[i]] = detail;
sum += detail.Cost;
}
model.Cost = sum;
2024-05-25 21:36:21 +04:00
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
{
2024-05-28 19:21:01 +04:00
ViewBag.AllDetails = details;
2024-05-25 21:36:21 +04:00
return View(model);
2024-05-28 19:21:01 +04:00
}
}
2024-05-26 23:23:24 +04:00
[HttpGet]
2024-05-28 19:21:01 +04:00
public IActionResult Privacy()
{
2024-05-25 21:36:21 +04:00
if (IsLoggedIn)
return View(UserImplementer.user);
return RedirectToAction("IndexNonReg");
2024-04-28 17:11:40 +04:00
}
2024-05-26 23:23:24 +04:00
[HttpPost]
public IActionResult Privacy(int id, string login, string email, string password, string name)
{
2024-05-26 23:23:24 +04:00
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
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);
}
2024-05-26 23:23:24 +04:00
[HttpGet]
public IActionResult DetailTimeChoose()
{
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
return View();
}
2024-05-28 19:21:01 +04:00
[HttpPost]
public IActionResult SendReport(DateTime startDate, DateTime endDate)
{
2024-05-26 23:23:24 +04:00
2024-05-28 19:21:01 +04:00
return Ok();
}
[HttpPost]
public IActionResult TimeReportWeb(DateTime startDate, DateTime endDate)
{
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
2024-05-26 23:23:24 +04:00
2024-05-28 19:21:01 +04:00
HttpContext.Session.SetString("StartDate", startDate.ToString());
HttpContext.Session.SetString("EndDate", endDate.ToString());
2024-05-26 23:23:24 +04:00
2024-05-28 19:21:01 +04:00
return RedirectToAction("DetailTimeReport");
}
[HttpGet]
public IActionResult DetailTimeReport()
{
var startDateStr = HttpContext.Session.GetString("StartDate");
var endDateStr = HttpContext.Session.GetString("EndDate");
var startDate = DateTime.Parse(startDateStr);
var endDate = DateTime.Parse(endDateStr).AddDays(1);
var values = _data.GetTimeReport(startDate, endDate, UserId);
ViewBag.StartDate = startDate;
ViewBag.EndDate = endDate;
return View(values);
}
[HttpPost]
public void DetailTimeMail()
{
2024-05-26 23:23:24 +04:00
var startDateStr = HttpContext.Session.GetString("StartDate");
var endDateStr = HttpContext.Session.GetString("EndDate");
var startDate = DateTime.Parse(startDateStr);
var endDate = DateTime.Parse(endDateStr).AddDays(1);
2024-05-28 19:21:01 +04:00
using (MemoryStream memoryStream = new MemoryStream()) {
_data.SendMailReport(startDate, endDate, UserId, memoryStream);
}
Response.Redirect("DetailTimeReport");
2024-05-26 23:23:24 +04:00
}
2024-05-27 22:06:37 +04:00
[HttpGet]
public IActionResult DetailWorkshopChoose()
{
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
var details = _data.GetDetails(UserId);
return View(details);
}
[HttpPost]
2024-05-28 19:21:01 +04:00
public IActionResult DetailWorkshopChoose(List<int> selectedItems, string reportType)
2024-05-27 22:06:37 +04:00
{
string value = string.Join("/", selectedItems);
2024-05-28 19:21:01 +04:00
HttpContext.Session.SetString("Details", value);
2024-05-27 22:06:37 +04:00
if (reportType.Equals("default"))
return RedirectToAction("DetailWorkshopReport");
2024-05-28 15:06:59 +04:00
else if (reportType.Equals("excel"))
2024-05-28 19:21:01 +04:00
return RedirectToAction("ExcelGenerate");
2024-05-27 22:06:37 +04:00
else
2024-05-28 19:21:01 +04:00
return RedirectToAction("WordGenerate");
2024-05-27 22:06:37 +04:00
}
2024-05-28 19:21:01 +04:00
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())
{
_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");
}
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())
{
_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");
}
2024-05-27 22:06:37 +04:00
[HttpGet]
public IActionResult DetailWorkshopReport()
{
2024-05-27 22:06:37 +04:00
var value = HttpContext.Session.GetString("Details");
if (value != null)
{
2024-05-27 22:06:37 +04:00
List<int> rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList();
var reports = _data.GetWorkshopReports(rawReports);
return View(reports);
}
return View(new List<DetailWorkshopReportViewModel>());
}
public IActionResult ReportsMenu()
{
return View();
}
2024-05-26 23:23:24 +04:00
[HttpGet]
public IActionResult ProductMachineAdd(int id)
{
2024-05-26 23:23:24 +04:00
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
var product = _data.GetProduct(id);
ViewBag.Product = product;
var machines = _data.GetMachines();
2024-05-28 19:21:01 +04:00
return View(machines);
}
2024-05-26 23:23:24 +04:00
[HttpPost]
2024-05-28 19:21:01 +04:00
public IActionResult ProductMachineAdd(int productId, int machineId)
{
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
2024-05-26 23:23:24 +04:00
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);
return RedirectToAction("IndexProduct");
2024-05-28 19:21:01 +04:00
}
2024-04-28 17:11:40 +04:00
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
2024-05-28 19:21:01 +04:00
}