Coursach/Course/GuarantorAPP/Controllers/HomeController.cs

337 lines
8.9 KiB
C#
Raw Normal View History

2024-05-27 21:03:42 +04:00
using Contracts.BindingModels;
using Contracts.BusinessLogicsContracts;
2024-04-30 00:42:26 +04:00
using Contracts.ViewModels;
using GuarantorAPP.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace GuarantorAPP.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
2024-05-27 21:03:42 +04:00
private readonly GuarantorData _data;
2024-04-30 00:42:26 +04:00
2024-05-27 21:03:42 +04:00
public HomeController(ILogger<HomeController> logger, GuarantorData data)
2024-04-30 00:42:26 +04:00
{
_logger = logger;
2024-05-27 21:03:42 +04:00
_data = data;
}
private bool IsLoggedIn { get { return UserGuarantor.user != null; } }
private int UserId { get { return UserGuarantor.user!.Id; } }
public IActionResult IndexNonReg()
{
if (!IsLoggedIn)
return View();
return RedirectToAction("Index");
2024-04-30 00:42:26 +04:00
}
public IActionResult Index()
{
2024-05-27 21:03:42 +04:00
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
2024-04-30 00:42:26 +04:00
return View();
}
2024-05-27 21:03:42 +04:00
[HttpGet]
public IActionResult Enter()
{
if (!IsLoggedIn)
return View();
return RedirectToAction("Index");
}
[HttpPost]
public void Enter(string login, string password)
{
var user = _data.Login(login, password);
if (user != null)
{
UserGuarantor.user = user;
Response.Redirect("Index");
}
}
[HttpGet]
2024-04-30 00:42:26 +04:00
public IActionResult Register()
{
return View();
}
2024-05-27 21:03:42 +04:00
public IActionResult Logout()
2024-04-30 00:42:26 +04:00
{
2024-05-27 21:03:42 +04:00
UserGuarantor.user = null;
return RedirectToAction("IndexNonReg");
}
[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 }))
2024-04-30 21:51:55 +04:00
{
2024-05-27 21:03:42 +04:00
Response.Redirect("Index");
}
}
[HttpGet]
public IActionResult IndexMachine()
{
if (UserGuarantor.user != null)
2024-04-30 21:51:55 +04:00
{
2024-05-27 21:03:42 +04:00
var machines = _data.GetMachines(UserGuarantor.user.Id);
return View(machines);
}
return RedirectToAction("IndexNonReg");
}
[HttpPost]
public IActionResult IndexMachine(int id)
{
_data.DeleteMachine(id);
return RedirectToAction("IndexMachine");
}
[HttpGet]
public IActionResult CreateMachine(int id)
{
var workers = _data.GetWorkers(UserGuarantor.user!.Id);
ViewBag.AllWorkers = workers;
if (id != 0)
2024-04-30 21:51:55 +04:00
{
2024-05-27 21:03:42 +04:00
var value = _data.GetMachine(id);
if (value != null)
return View(value);
2024-04-30 21:51:55 +04:00
}
2024-05-27 21:03:42 +04:00
return View(new MachineViewModel());
}
[HttpPost]
public IActionResult CreateMachine(int id, string title, int[] workerIds)
2024-04-30 00:42:26 +04:00
{
2024-05-27 21:03:42 +04:00
MachineBindingModel model = new MachineBindingModel();
model.Id = id;
model.Title = title;
model.DateCreate = DateTime.Now;
model.UserId = UserGuarantor.user!.Id;
var workers = _data.GetWorkers(UserGuarantor.user!.Id);
for (int i = 0; i < workerIds.Length; i++)
{
var worker = workers!.FirstOrDefault(x => x.Id == workerIds[i]);
model.MachineWorker[workerIds[i]] = worker;
}
bool changed = false;
if (model.MachineWorker.Count > 0)
{
if (id != 0)
2024-04-30 21:51:55 +04:00
{
2024-05-27 21:03:42 +04:00
changed = _data.UpdateMachine(model);
}
else
2024-04-30 21:51:55 +04:00
{
2024-05-27 21:03:42 +04:00
changed = _data.CreateMachine(model);
}
}
if (changed)
return RedirectToAction("IndexMachine");
else
{
ViewBag.AllWorkers = workers;
return View(model);
}
}
[HttpGet]
public IActionResult IndexWorker()
{
if (UserGuarantor.user != null)
{
var list = _data.GetWorkers(UserGuarantor.user.Id);
if (list != null)
return View(list);
return View(new List<WorkerViewModel>());
}
return RedirectToAction("IndexNonReg");
2024-04-30 00:42:26 +04:00
}
2024-05-27 21:03:42 +04:00
[HttpPost]
public void IndexWorker(int id)
2024-04-30 00:42:26 +04:00
{
2024-05-27 21:03:42 +04:00
if (UserGuarantor.user != null)
{
_data.DeleteWorker(id);
}
Response.Redirect("IndexWorker");
}
[HttpGet]
public IActionResult CreateWorker(int id)
{
if (id != 0)
{
var value = _data.GetWorker(id);
if (value != null)
return View(value);
}
return View(new WorkerViewModel());
}
[HttpPost]
public IActionResult CreateWorker(WorkerBindingModel model)
{
if (model.Id == 0)
{
model.UserId = UserGuarantor.user!.Id;
if (_data.CreateWorker(model))
return RedirectToAction("IndexWorker");
}
else
{
if (_data.UpdateWorker(model))
return RedirectToAction("IndexWorker");
}
2024-04-30 00:42:26 +04:00
return View();
}
2024-05-27 21:03:42 +04:00
[HttpGet]
2024-04-30 00:42:26 +04:00
public IActionResult IndexWorkshop()
{
2024-05-27 21:03:42 +04:00
if (IsLoggedIn)
2024-04-30 21:51:55 +04:00
{
2024-05-27 21:03:42 +04:00
var workshops = _data.GetWorkshops(UserGuarantor.user!.Id);
return View(workshops);
}
return RedirectToAction("IndexNonReg");
}
[HttpPost]
public IActionResult IndexWorkshop(int id)
{
_data.DeleteWorkshop(id);
return RedirectToAction("IndexWorkshop");
}
[HttpGet]
public IActionResult CreateWorkshop(int id)
{
var workers = _data.GetWorkers(UserGuarantor.user!.Id);
ViewBag.AllWorkers = workers;
if (id != 0)
{
var value = _data.GetWorkshop(id);
if (value != null)
return View(value);
}
return View(new WorkshopViewModel());
}
[HttpPost]
public IActionResult CreateWorkshop(int id, string title, int[] workerIds)
{
WorkshopBindingModel model = new WorkshopBindingModel();
model.Id = id;
model.Title = title;
model.DateCreate = DateTime.Now;
model.UserId = UserGuarantor.user!.Id;
var workers = _data.GetWorkers(UserGuarantor.user!.Id);
for (int i = 0; i < workerIds.Length; i++)
{
var worker = workers!.FirstOrDefault(x => x.Id == workerIds[i])!;
model.WorkshopWorker[workerIds[i]] = (worker);
}
if (model.WorkshopWorker.Count == 0)
return RedirectToAction("IndexWorkshop");
if (id != 0)
{
_data.UpdateWorkshop(model);
}
else
{
_data.CreateWorkshop(model);
}
return RedirectToAction("IndexWorkshop");
}
[HttpGet]
2024-04-30 00:42:26 +04:00
public IActionResult Privacy()
2024-04-30 21:51:55 +04:00
{
2024-05-27 21:03:42 +04:00
if (IsLoggedIn)
return View(UserGuarantor.user);
return RedirectToAction("IndexNonReg");
}
[HttpPost]
public IActionResult Privacy(int id, string login, string email, string password, string name)
{
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
GuarantorBindingModel user = new() { Id = id, Login = login, Email = email, Password = password, Name = name };
if (_data.UpdateUser(user))
2024-04-30 21:51:55 +04:00
{
2024-05-27 21:03:42 +04:00
UserGuarantor.user = new GuarantorViewModel { Id = id, Login = login, Password = password, Name = name, Email = email };
}
2024-04-30 21:51:55 +04:00
return View(user);
}
2024-05-27 21:03:42 +04:00
[HttpGet]
public IActionResult MachineWorkshopTimeChoose()
{
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
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());
return RedirectToAction("MachineWorkshopTimeReport");
}
[HttpGet]
2024-04-30 21:51:55 +04:00
public IActionResult MachineWorkshopTimeReport()
{
2024-05-27 21:03:42 +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);
var values = _data.GetTimeReport(startDate, endDate, UserId);
ViewBag.StartDate = startDate;
ViewBag.EndDate = endDate;
return View(values);
2024-04-30 21:51:55 +04:00
}
public IActionResult WorkerProductReport()
{
List<WorkerProductReportViewModel> workerProductReports = new List<WorkerProductReportViewModel>
{
new WorkerProductReportViewModel
{
WorkerName = "Работник 1",
Products = new List<string> { "Изделие первое", "Изделие второе" }
},
new WorkerProductReportViewModel
{
WorkerName = "Работник 2",
Products = new List<string> { "Изделие одно", "Изделие второе" }
}
};
return View(workerProductReports);
}
public IActionResult ReportsMenu()
2024-04-30 00:42:26 +04:00
{
return View();
}
2024-05-27 21:03:42 +04:00
[HttpGet]
public IActionResult WorkshopProductionAdd(int id)
2024-04-30 21:51:55 +04:00
{
2024-05-27 21:03:42 +04:00
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
var workshop = _data.GetWorkshop(id);
ViewBag.Workshop = workshop;
var productions = _data.GetProductions();
return View(productions);
}
[HttpPost]
public IActionResult WorkshopProductionAdd(int workshopId, int productionId)
{
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
var workshop = _data.GetWorkshop(workshopId);
if (workshop == null)
return RedirectToAction("Index");
WorkshopBindingModel workshopBinding = new() { Id = workshopId, Title = workshop.Title, Address = workshop.Address, Director = workshop.Director, UserId = workshop.UserId, ProductionId = workshop.ProductionId, WorkshopWorker = workshop.WorkerWorkshops };
_data.UpdateWorkshop(workshopBinding);
return RedirectToAction("IndexWorkshop");
2024-04-30 21:51:55 +04:00
}
2024-04-30 00:42:26 +04:00
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}