Coursach/Course/ImplementerApp/Controllers/ProductionController.cs

117 lines
3.8 KiB
C#
Raw Normal View History

2024-05-28 21:41:19 +04:00
using Contracts.BindingModels;
using Contracts.ViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ImplementerApp.Controllers
{
public class ProductionController : Controller
{
private readonly ILogger<ProductionController> _logger;
private readonly ImplementerData _data;
public ProductionController(ILogger<ProductionController> logger, ImplementerData data)
{
_logger = logger;
_data = data;
}
private bool IsLoggedIn { get { return UserImplementer.user != null; } }
private int UserId { get { return UserImplementer.user!.Id; } }
[HttpGet]
public IActionResult IndexProduction()
{
2024-05-29 15:37:04 +04:00
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg", "Home");
try
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +04:00
var productions = _data.GetProductions(UserImplementer.user!.Id);
2024-05-28 21:41:19 +04:00
return View(productions);
2024-05-29 15:37:04 +04:00
} catch
{
return RedirectToAction("IndexNonReg", "Home");
2024-05-28 21:41:19 +04:00
}
}
[HttpPost]
public IActionResult IndexProduction(int id)
{
2024-05-29 15:37:04 +04:00
try
{
_data.DeleteProduction(id);
return RedirectToAction("IndexProduction");
} catch
{
return RedirectToAction("IndexNonReg");
}
2024-05-28 21:41:19 +04:00
}
[HttpGet]
public IActionResult CreateProduction(int id)
{
2024-05-29 15:37:04 +04:00
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg", "Home");
try
{
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
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +04:00
return RedirectToAction("IndexProduction");
2024-05-28 21:41:19 +04:00
}
return View(new ProductionViewModel());
}
[HttpPost]
public IActionResult CreateProduction(int id, string title, int[] detailIds)
{
2024-05-29 15:37:04 +04:00
try
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +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;
bool changed = false;
if (model.ProductionDetails.Count > 0)
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +04:00
if (id != 0)
{
changed = _data.UpdateProduction(model);
}
else
{
changed = _data.CreateProduction(model);
}
2024-05-28 21:41:19 +04:00
}
2024-05-29 15:37:04 +04:00
if (changed)
return RedirectToAction("IndexProduction");
2024-05-28 21:41:19 +04:00
else
{
2024-05-29 15:37:04 +04:00
ViewBag.AllDetails = details;
return View(model);
2024-05-28 21:41:19 +04:00
}
2024-05-29 15:37:04 +04:00
} catch
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +04:00
return RedirectToAction("IndexProduction");
2024-05-28 21:41:19 +04:00
}
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View();
}
}
}