using Contracts.BindingModels; using Contracts.ViewModels; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace ImplementerApp.Controllers { public class ProductionController : Controller { private readonly ILogger _logger; private readonly ImplementerData _data; public ProductionController(ILogger 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 (UserImplementer.user != null) { var productions = _data.GetProductions(UserImplementer.user.Id); return View(productions); } return RedirectToAction("IndexNonReg"); } [HttpPost] public IActionResult IndexProduction(int id) { _data.DeleteProduction(id); return RedirectToAction("IndexProduction"); } [HttpGet] public IActionResult CreateProduction(int id) { var details = _data.GetDetails(UserImplementer.user!.Id); ViewBag.AllDetails = details; if (id != 0) { var value = _data.GetProduction(id); if (value != null) return View(value); } 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++) { 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); } } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(); } } }