117 lines
3.8 KiB
C#
117 lines
3.8 KiB
C#
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()
|
|
{
|
|
if (!IsLoggedIn)
|
|
return RedirectToAction("IndexNonReg", "Home");
|
|
try
|
|
{
|
|
var productions = _data.GetProductions(UserImplementer.user!.Id);
|
|
return View(productions);
|
|
} catch
|
|
{
|
|
return RedirectToAction("IndexNonReg", "Home");
|
|
}
|
|
}
|
|
[HttpPost]
|
|
public IActionResult IndexProduction(int id)
|
|
{
|
|
try
|
|
{
|
|
_data.DeleteProduction(id);
|
|
return RedirectToAction("IndexProduction");
|
|
|
|
} catch
|
|
{
|
|
return RedirectToAction("IndexNonReg");
|
|
}
|
|
}
|
|
[HttpGet]
|
|
public IActionResult CreateProduction(int id)
|
|
{
|
|
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
|
|
{
|
|
return RedirectToAction("IndexProduction");
|
|
}
|
|
return View(new ProductionViewModel());
|
|
}
|
|
[HttpPost]
|
|
public IActionResult CreateProduction(int id, string title, int[] detailIds)
|
|
{
|
|
try
|
|
{
|
|
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)
|
|
{
|
|
if (id != 0)
|
|
{
|
|
changed = _data.UpdateProduction(model);
|
|
}
|
|
else
|
|
{
|
|
changed = _data.CreateProduction(model);
|
|
}
|
|
}
|
|
if (changed)
|
|
return RedirectToAction("IndexProduction");
|
|
else
|
|
{
|
|
ViewBag.AllDetails = details;
|
|
return View(model);
|
|
}
|
|
} catch
|
|
{
|
|
return RedirectToAction("IndexProduction");
|
|
}
|
|
}
|
|
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
public IActionResult Error()
|
|
{
|
|
return View();
|
|
}
|
|
}
|
|
}
|