yavsc/src/Yavsc.Org/Controllers/Survey/BugController.cs

186 lines
5.6 KiB
C#
Raw Normal View History

2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Mvc;
2017-10-02 04:43:20 +02:00
using Yavsc.Models;
using Yavsc.Models.IT.Fixing;
2018-12-27 23:53:27 +00:00
using Yavsc.Models.IT.Evolution;
using Yavsc.Server.Helpers;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Mvc.Rendering;
2019-01-01 16:28:47 +00:00
using Microsoft.Extensions.Localization;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
2017-10-02 04:43:20 +02:00
namespace Yavsc.Controllers
{
public class BugController : Controller
{
2020-10-09 19:35:39 +01:00
readonly ApplicationDbContext _context;
readonly IStringLocalizer<BugController> _localizer;
readonly IStringLocalizer<Yavsc.Models.IT.Fixing.Resources> _statusLocalizer;
2017-10-02 04:43:20 +02:00
2019-09-27 06:59:20 +01:00
public BugController(ApplicationDbContext context,
IStringLocalizer<BugController> localizer,
IStringLocalizer<Resources> statusLocalizer
)
2017-10-02 04:43:20 +02:00
{
2019-01-01 16:28:47 +00:00
_context = context;
_localizer = localizer;
2019-09-27 06:59:20 +01:00
_statusLocalizer = statusLocalizer;
2017-10-02 04:43:20 +02:00
}
// GET: Bug
2024-12-25 14:55:57 +00:00
public async Task<IActionResult> Index(int skip = 0, int take = 25)
2017-10-02 04:43:20 +02:00
{
2024-12-25 14:55:57 +00:00
if (take > 50) return BadRequest();
ViewData["skip"]=skip;
ViewData["take"]=take;
return View(await _context.Bug.Skip(skip).Take(take).ToListAsync());
2017-10-02 04:43:20 +02:00
}
// GET: Bug/Details/5
public async Task<IActionResult> Details(long? id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-10-02 04:43:20 +02:00
}
Bug bug = await _context.Bug.SingleAsync(m => m.Id == id);
if (bug == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-10-02 04:43:20 +02:00
}
return View(bug);
}
// GET: Bug/Create
public IActionResult Create()
{
2019-01-01 16:28:47 +00:00
ViewBag.Features = Features(_context);
ViewBag.Statuses = Statuses(default(BugStatus));
2017-10-02 04:43:20 +02:00
return View();
}
2019-01-01 16:28:47 +00:00
IEnumerable<SelectListItem> Statuses(BugStatus ?status) =>
2019-09-27 06:59:20 +01:00
_statusLocalizer.CreateSelectListItems(typeof(BugStatus), status);
2019-01-01 16:28:47 +00:00
IEnumerable<SelectListItem> Features(ApplicationDbContext context) =>
2019-09-27 06:59:20 +01:00
context.Feature.CreateSelectListItems<Feature>(f => f.Id.ToString(), f => $"{f.ShortName} ({f.Description})", null)
2019-01-01 16:28:47 +00:00
.AddNull(_localizer["noAttachedFID"]);
2017-10-02 04:43:20 +02:00
// POST: Bug/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Bug bug)
{
if (ModelState.IsValid)
{
_context.Bug.Add(bug);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
2019-06-16 01:10:52 +01:00
ViewBag.Features = Features(_context);
ViewBag.Statuses = Statuses(default(BugStatus));
2017-10-02 04:43:20 +02:00
return View(bug);
}
// GET: Bug/Edit/5
2019-09-27 06:59:20 +01:00
[Authorize("AdministratorOnly")]
2017-10-02 04:43:20 +02:00
public async Task<IActionResult> Edit(long? id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-10-02 04:43:20 +02:00
}
Bug bug = await _context.Bug.SingleAsync(m => m.Id == id);
if (bug == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-10-02 04:43:20 +02:00
}
2019-01-01 16:28:47 +00:00
ViewBag.Features = Features(_context);
ViewBag.Statuses = Statuses(bug.Status);
2017-10-02 04:43:20 +02:00
return View(bug);
}
// POST: Bug/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
2019-09-27 06:59:20 +01:00
[Authorize("AdministratorOnly")]
2017-10-02 04:43:20 +02:00
public async Task<IActionResult> Edit(Bug bug)
{
if (ModelState.IsValid)
{
_context.Update(bug);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(bug);
}
// GET: Bug/Delete/5
[ActionName("Delete")]
2019-09-27 06:59:20 +01:00
[Authorize("AdministratorOnly")]
2017-10-02 04:43:20 +02:00
public async Task<IActionResult> Delete(long? id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-10-02 04:43:20 +02:00
}
Bug bug = await _context.Bug.SingleAsync(m => m.Id == id);
if (bug == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2017-10-02 04:43:20 +02:00
}
return View(bug);
}
2024-12-25 14:55:57 +00:00
2017-10-02 04:43:20 +02:00
// POST: Bug/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
2019-09-27 06:59:20 +01:00
[Authorize("AdministratorOnly")]
2017-10-02 04:43:20 +02:00
public async Task<IActionResult> DeleteConfirmed(long id)
{
Bug bug = await _context.Bug.SingleAsync(m => m.Id == id);
_context.Bug.Remove(bug);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
2023-03-19 16:02:50 +00:00
[Authorize("AdministratorOnly")]
public async Task<IActionResult> DeleteAllLike(long? id)
{
if (id == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2023-03-19 16:02:50 +00:00
}
Bug bugref = await _context.Bug.SingleAsync(m => m.Id == id);
if (bugref == null)
{
return null;
}
var bugs = _context.Bug.Where(b => b.Description == bugref.Description);
return View(bugs);
}
// POST: Bug/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize("AdministratorOnly")]
2024-12-25 14:55:57 +00:00
public async Task<IActionResult> DeleteAllLikeConfirmed(long id, int prefixLen = 25)
2023-03-19 16:02:50 +00:00
{
Bug bug = await _context.Bug.SingleAsync(m => m.Id == id);
2024-12-25 14:55:57 +00:00
var bugs = await _context.Bug.Where(b => b.Description.Substring(0, prefixLen) == bug.Description.Substring(0, prefixLen)).ToArrayAsync();
2023-03-19 16:02:50 +00:00
foreach (var btd in bugs)
_context.Bug.Remove(btd);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
2017-10-02 04:43:20 +02:00
}
}