Coursach/Course/ImplementerApp/Controllers/DetailController.cs
2024-05-28 22:40:13 +04:00

82 lines
2.5 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 (UserImplementer.user != null)
{
var list = _data.GetDetails(UserImplementer.user.Id);
if (list != null)
return View(list);
return View(new List<DetailViewModel>());
}
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);
}
}
}