using Contracts.BindingModels; using Contracts.ViewModels; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace ImplementerApp.Controllers { public class DetailController : Controller { private readonly ILogger _logger; private readonly ImplementerData _data; public DetailController(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 IndexDetail() { if (UserImplementer.user != null) { var list = _data.GetDetails(UserImplementer.user.Id); if (list != null) return View(list); return View(new List()); } return RedirectToAction("IndexNonReg"); } [HttpPost] public void IndexDetail(int id) { if (UserImplementer.user != null) { _data.DeleteDetail(id); } Response.Redirect("IndexDetail"); } [HttpGet] public IActionResult CreateDetail(int id) { if (id != 0) { var value = _data.GetDetail(id); if (value != null) return View(value); } return View(new DetailViewModel()); } [HttpPost] public IActionResult CreateDetail(DetailBindingModel model) { if (model.Id == 0) { model.DateCreate = DateTime.Now; model.UserId = UserImplementer.user!.Id; try { if (_data.CreateDetail(model)) return RedirectToAction("IndexDetail"); } catch (ArgumentException ex) { return RedirectToAction("Error", ex.Message); } } else { if (_data.UpdateDetail(model)) return RedirectToAction("IndexDetail"); } return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error(string ex) { return View(ex); } } }