2026-12-02 17:34:58 +01:00
|
|
|
|
using System.Net;
|
2026-12-02 13:50:01 +01:00
|
|
|
|
using Helpdesk.Web.Api;
|
2026-12-02 17:34:58 +01:00
|
|
|
|
using Helpdesk.Web.Infrastructure;
|
2026-12-02 13:50:01 +01:00
|
|
|
|
using Helpdesk.Web.Models;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.Web.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Hlášení závad (FR-10 – FR-17).</summary>
|
|
|
|
|
|
[Route("issues")]
|
|
|
|
|
|
public sealed class IssuesController(HelpdeskApiClient api) : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>Moje hlášení – závady, které uživatel nahlásil, od nejnovějších.</summary>
|
|
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<IActionResult> Index(string? status, int page = 1, CancellationToken ct = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (status is not null && !IssueListViewModel.Statuses.Contains(status))
|
|
|
|
|
|
status = null;
|
|
|
|
|
|
|
|
|
|
|
|
var issues = await api.GetIssuesAsync(new IssueListQuery(status, Mine: true, Page: Math.Max(page, 1)), ct);
|
|
|
|
|
|
// API vrací u závady jen ID kategorie a budovy – názvy doplníme z číselníků.
|
|
|
|
|
|
var categories = await api.GetIssueCategoriesAsync(ct);
|
|
|
|
|
|
var buildings = await api.GetBuildingsAsync(ct);
|
|
|
|
|
|
|
|
|
|
|
|
return View(new IssueListViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Issues = issues,
|
|
|
|
|
|
CategoryNames = categories.ToDictionary(c => c.Id, c => c.Name),
|
|
|
|
|
|
BuildingNames = buildings.ToDictionary(b => b.Id, b => b.Name),
|
|
|
|
|
|
Status = status,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-12-02 17:34:58 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>Formulář nového hlášení (T1 – závada vznikne ve stavu Přijata).</summary>
|
|
|
|
|
|
[HttpGet("new")]
|
|
|
|
|
|
public async Task<IActionResult> New(CancellationToken ct) => View(await WithCatalogsAsync(new NewIssueViewModel(), ct));
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("new")]
|
|
|
|
|
|
[RequestSizeLimit(AttachmentRules.MaxRequestBytes)]
|
|
|
|
|
|
[RequestFormLimits(MultipartBodyLengthLimit = AttachmentRules.MaxRequestBytes)]
|
|
|
|
|
|
public async Task<IActionResult> New(NewIssueViewModel model, CancellationToken ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var error in AttachmentRules.Validate(model.Files))
|
|
|
|
|
|
ModelState.AddModelError(nameof(NewIssueViewModel.Files), error);
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
|
return View(await WithCatalogsAsync(model, ct));
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var issue = await api.CreateIssueAsync(
|
|
|
|
|
|
new CreateIssueRequest(model.Title.Trim(), model.Description.Trim(), model.CategoryId!.Value, model.BuildingId, model.LocationDetail?.Trim()),
|
|
|
|
|
|
model.Files.Select(f => new UploadFile(f.FileName, f.ContentType, f.OpenReadStream)).ToList(),
|
|
|
|
|
|
ct);
|
|
|
|
|
|
TempData.SetSuccess($"Závada #{issue.Id} je nahlášená. Vedení školy ji přiřadí řešiteli.");
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.RequestEntityTooLarge
|
|
|
|
|
|
or HttpStatusCode.UnsupportedMediaType or HttpStatusCode.TooManyRequests)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 400 validace nebo nepovolený obsah souboru, 413 příliš velké přílohy, 429 limit zápisů (10/min).
|
|
|
|
|
|
ModelState.AddApiErrors(ex, typeof(NewIssueViewModel));
|
|
|
|
|
|
return View(await WithCatalogsAsync(model, ct));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<NewIssueViewModel> WithCatalogsAsync(NewIssueViewModel model, CancellationToken ct)
|
|
|
|
|
|
{
|
|
|
|
|
|
model.SetCatalogs(await api.GetIssueCategoriesAsync(ct), await api.GetBuildingsAsync(ct));
|
|
|
|
|
|
return model;
|
|
|
|
|
|
}
|
2026-12-02 13:50:01 +01:00
|
|
|
|
}
|