101 lines
4.0 KiB
C#
101 lines
4.0 KiB
C#
using System.ComponentModel.DataAnnotations;
|
||
using Helpdesk.Web.Api;
|
||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||
|
||
namespace Helpdesk.Web.Models;
|
||
|
||
/// <summary>Limity příloh – stejné jako v API (FR-11).</summary>
|
||
public static class AttachmentRules
|
||
{
|
||
public const int MaxFiles = 5;
|
||
public const long MaxFileBytes = 10 * 1024 * 1024;
|
||
/// <summary>Celý požadavek: všechny soubory plus pole formuláře.</summary>
|
||
public const long MaxRequestBytes = MaxFiles * MaxFileBytes + 1024 * 1024;
|
||
|
||
public static readonly IReadOnlyList<string> Extensions = [".jpg", ".jpeg", ".png", ".heic", ".pdf", ".docx"];
|
||
|
||
/// <summary>Atribut <c>accept</c> pro výběr souborů.</summary>
|
||
public static string Accept => string.Join(",", Extensions);
|
||
|
||
/// <summary>
|
||
/// Rychlá kontrola na webu, aby uživatel nečekal na nahrání 50 MB jen kvůli špatnému typu.
|
||
/// Skutečný obsah souboru (signaturu) ověřuje API.
|
||
/// </summary>
|
||
public static IEnumerable<string> Validate(IReadOnlyCollection<IFormFile> files)
|
||
{
|
||
if (files.Count > MaxFiles)
|
||
yield return $"Najednou můžete přiložit nejvýše {MaxFiles} souborů.";
|
||
|
||
foreach (var file in files)
|
||
{
|
||
if (!Extensions.Contains(Path.GetExtension(file.FileName).ToLowerInvariant()))
|
||
yield return $"Soubor {file.FileName} nemá povolený typ. Povolené jsou JPG, PNG, HEIC, PDF a DOCX.";
|
||
else if (file.Length > MaxFileBytes)
|
||
yield return $"Soubor {file.FileName} je větší než 10 MB.";
|
||
else if (file.Length == 0)
|
||
yield return $"Soubor {file.FileName} je prázdný.";
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>Údaje hlášení společné pro nové hlášení i jeho úpravu (partial <c>_IssueFields</c>).</summary>
|
||
public abstract class IssueFormViewModel
|
||
{
|
||
[Required(ErrorMessage = "Zadejte název.")]
|
||
[StringLength(200, ErrorMessage = "Název může mít nejvýše 200 znaků.")]
|
||
[Display(Name = "Co se stalo")]
|
||
public string Title { get; set; } = "";
|
||
|
||
[Required(ErrorMessage = "Popište závadu.")]
|
||
[StringLength(10_000, ErrorMessage = "Popis může mít nejvýše 10 000 znaků.")] // stejně jako v mobilu
|
||
[Display(Name = "Popis")]
|
||
public string Description { get; set; } = "";
|
||
|
||
[Required(ErrorMessage = "Vyberte kategorii.")]
|
||
[Display(Name = "Kategorie")]
|
||
public int? CategoryId { get; set; }
|
||
|
||
[Display(Name = "Budova")]
|
||
public int? BuildingId { get; set; }
|
||
|
||
[StringLength(255, ErrorMessage = "Upřesnění místa může mít nejvýše 255 znaků.")]
|
||
[Display(Name = "Upřesnění místa")]
|
||
public string? LocationDetail { get; set; }
|
||
|
||
// Nabídky pro výběr – plní je kontroler z číselníků API.
|
||
public IReadOnlyList<SelectListItem> Categories { get; set; } = [];
|
||
public IReadOnlyList<SelectListItem> Buildings { get; set; } = [];
|
||
|
||
public void SetCatalogs(IEnumerable<CategoryDto> categories, IEnumerable<BuildingDto> buildings)
|
||
{
|
||
Categories = categories.Select(c => new SelectListItem(c.Name, c.Id.ToString())).ToList();
|
||
Buildings = buildings.Select(b => new SelectListItem(b.Name, b.Id.ToString())).ToList();
|
||
}
|
||
}
|
||
|
||
public sealed class NewIssueViewModel : IssueFormViewModel
|
||
{
|
||
[Display(Name = "Fotografie nebo soubory")]
|
||
public List<IFormFile> Files { get; set; } = [];
|
||
}
|
||
|
||
/// <summary>Úprava hlášení hlásičem, dokud ho vedení nepřiřadí (stav Přijata).</summary>
|
||
public sealed class EditIssueViewModel : IssueFormViewModel
|
||
{
|
||
public int Id { get; set; }
|
||
|
||
/// <summary>Verze, kterou uživatel upravuje; když se závada mezitím změní, API vrátí 409.</summary>
|
||
public int? RowVersion { get; set; }
|
||
|
||
public static EditIssueViewModel From(IssueDetail issue) => new()
|
||
{
|
||
Id = issue.Id,
|
||
RowVersion = issue.RowVersion,
|
||
Title = issue.Title,
|
||
Description = issue.Description,
|
||
CategoryId = issue.CategoryId,
|
||
BuildingId = issue.BuildingId,
|
||
LocationDetail = issue.LocationDetail,
|
||
};
|
||
}
|