Coursach/Course/ImplementerApp/Controllers/DetailController.cs

92 lines
2.7 KiB
C#

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