Coursach/Course/ImplementerApp/Controllers/DetailController.cs

92 lines
2.7 KiB
C#
Raw Normal View History

2024-05-28 21:41:19 +04:00
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()
{
2024-05-29 15:37:04 +04:00
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg", "Home");
try
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +04:00
var list = _data.GetDetails(UserImplementer.user!.Id);
2024-05-28 21:41:19 +04:00
if (list != null)
return View(list);
}
2024-05-29 15:37:04 +04:00
catch (Exception)
{
return View(new List<DetailViewModel>()); ;
}
return View(new List<DetailViewModel>());
2024-05-28 21:41:19 +04:00
}
[HttpPost]
public void IndexDetail(int id)
{
2024-05-29 15:37:04 +04:00
if (IsLoggedIn)
2024-05-28 21:41:19 +04:00
{
_data.DeleteDetail(id);
}
Response.Redirect("IndexDetail");
}
[HttpGet]
public IActionResult CreateDetail(int id)
{
2024-05-29 15:37:04 +04:00
if (!IsLoggedIn)
{
return RedirectToAction("IndexNonReg", "Home");
}
2024-05-28 21:41:19 +04:00
if (id != 0)
{
var value = _data.GetDetail(id);
if (value != null)
return View(value);
}
return View(new DetailViewModel());
}
[HttpPost]
public IActionResult CreateDetail(DetailBindingModel model)
{
2024-05-29 15:37:04 +04:00
try
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +04:00
if (model.Id == 0)
{
model.DateCreate = DateTime.Now;
model.UserId = UserId;
2024-05-28 22:40:13 +04:00
if (_data.CreateDetail(model))
return RedirectToAction("IndexDetail");
2024-05-29 15:37:04 +04:00
}
else
2024-05-28 22:40:13 +04:00
{
2024-05-29 15:37:04 +04:00
if (_data.UpdateDetail(model))
return RedirectToAction("IndexDetail");
2024-05-28 22:40:13 +04:00
}
2024-05-29 15:37:04 +04:00
} catch (Exception)
2024-05-28 21:41:19 +04:00
{
2024-05-29 15:37:04 +04:00
return RedirectToAction("IndexDetail");
2024-05-28 21:41:19 +04:00
}
2024-05-29 15:37:04 +04:00
return RedirectToAction("IndexDetail");
2024-05-28 21:41:19 +04:00
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
2024-05-28 22:40:13 +04:00
public IActionResult Error(string ex)
2024-05-28 21:41:19 +04:00
{
2024-05-28 22:40:13 +04:00
return View(ex);
2024-05-28 21:41:19 +04:00
}
}
}