Sicherung

This commit is contained in:
Maier Stephan SI
2023-10-03 10:39:27 +02:00
parent 032b2747b6
commit d594901eee
7 changed files with 68 additions and 33 deletions

View File

@@ -21,7 +21,8 @@ namespace FSI.Prj.Mgt.Controllers
public async Task<IActionResult> Index(string searchString, int? page) public async Task<IActionResult> Index(string searchString, int? page)
{ {
IQueryable<Organization> items = _context.Organizations.Include(x => x.Parent); await UpdateFullShortName(_context.Organizations.ToList());
IQueryable<Organization> items = _context.Organizations.Include(x => x.Parent).OrderBy(x => x.FullShortName);
if (!string.IsNullOrEmpty(searchString)) if (!string.IsNullOrEmpty(searchString))
{ {
@@ -80,7 +81,7 @@ namespace FSI.Prj.Mgt.Controllers
{ {
var item = await _context.Organizations.FindAsync(id); var item = await _context.Organizations.FindAsync(id);
if (item == null) if (item == null) // kein Fund
{ {
return NotFound(); return NotFound();
} }
@@ -106,6 +107,13 @@ namespace FSI.Prj.Mgt.Controllers
ModelState.Remove("Parents"); ModelState.Remove("Parents");
ModelState.Remove("FullShortName"); ModelState.Remove("FullShortName");
if (string.IsNullOrEmpty(item.FullShortName))
{
item.FullShortName = GetFullShortName(_context.Organizations.ToList(), item.ParentId, item.ShortName);
}
item.ModificationTimeStamp = DateTime.Now; // Zeitstempel für Modifikation
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
@@ -116,17 +124,21 @@ namespace FSI.Prj.Mgt.Controllers
if (id == 0) // Insert if (id == 0) // Insert
{ {
try
{
item.CreationTimeStamp = item.ModificationTimeStamp; // Zeitstempel für Erstellung
_context.Add(item); _context.Add(item);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
}
catch (Exception ex)
{
}
} }
else // Update else // Update
{ {
try try
{ {
_context.Update(item);
_context.Update(item); _context.Update(item);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
@@ -142,6 +154,7 @@ namespace FSI.Prj.Mgt.Controllers
{ {
var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception)); var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));
} }
item.Parents.Add(new Organization()); item.Parents.Add(new Organization());
return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", item) }); return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", item) });
} }
@@ -174,18 +187,18 @@ namespace FSI.Prj.Mgt.Controllers
// return RedirectToAction("Index", "Organization"); // return RedirectToAction("Index", "Organization");
//} //}
public async void UpdateFullShortName() public async Task UpdateFullShortName(List<Organization> organizations)
{ {
foreach (var organization in _context.Organizations) foreach (var organization in organizations)
{ {
foreach (var item in _context.Organizations) foreach (var item in organizations)
{ {
item.FullShortName = GetFullShortName(_context.Organizations.ToList(), item.ParentId, item.ShortName); item.FullShortName = GetFullShortName(organizations, item.ParentId, item.ShortName);
_context.Update(item); _context.Update(item);
}
}
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
}
}
private string GetFullShortName(List<Organization> items, int? parentId, string? name) private string GetFullShortName(List<Organization> items, int? parentId, string? name)
{ {
@@ -204,9 +217,7 @@ namespace FSI.Prj.Mgt.Controllers
} }
} }
return string.Empty; return name;
} }
} }
} }

View File

@@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace FSI.Prj.Mgt.Data.Migrations namespace FSI.Prj.Mgt.Data.Migrations
{ {
[DbContext(typeof(ApplicationDbContext))] [DbContext(typeof(ApplicationDbContext))]
[Migration("20230929113426_Init")] [Migration("20231003081806_Init")]
partial class Init partial class Init
{ {
/// <inheritdoc /> /// <inheritdoc />
@@ -33,6 +33,9 @@ namespace FSI.Prj.Mgt.Data.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id")); SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("CreationTimeStamp")
.HasColumnType("datetime2");
b.Property<string>("Description") b.Property<string>("Description")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
@@ -40,6 +43,9 @@ namespace FSI.Prj.Mgt.Data.Migrations
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.Property<DateTime>("ModificationTimeStamp")
.HasColumnType("datetime2");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");

View File

@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore.Migrations; using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable #nullable disable
@@ -21,6 +22,8 @@ namespace FSI.Prj.Mgt.Data.Migrations
Description = table.Column<string>(type: "nvarchar(max)", nullable: true), Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
ParentId = table.Column<int>(type: "int", nullable: true), ParentId = table.Column<int>(type: "int", nullable: true),
Type = table.Column<int>(type: "int", nullable: false), Type = table.Column<int>(type: "int", nullable: false),
CreationTimeStamp = table.Column<DateTime>(type: "datetime2", nullable: false),
ModificationTimeStamp = table.Column<DateTime>(type: "datetime2", nullable: false),
FullShortName = table.Column<string>(type: "nvarchar(max)", nullable: false) FullShortName = table.Column<string>(type: "nvarchar(max)", nullable: false)
}, },
constraints: table => constraints: table =>

View File

@@ -30,6 +30,9 @@ namespace FSI.Prj.Mgt.Data.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id")); SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("CreationTimeStamp")
.HasColumnType("datetime2");
b.Property<string>("Description") b.Property<string>("Description")
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
@@ -37,6 +40,9 @@ namespace FSI.Prj.Mgt.Data.Migrations
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.Property<DateTime>("ModificationTimeStamp")
.HasColumnType("datetime2");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");

View File

@@ -38,8 +38,15 @@ namespace FSI.Prj.Mgt.Models
[Required(ErrorMessage = "Dieses Feld wird benötigt.")] [Required(ErrorMessage = "Dieses Feld wird benötigt.")]
public OrganizationType Type { get; set; } public OrganizationType Type { get; set; }
[DisplayName("Erstellungsdatum")]
public DateTime CreationTimeStamp { get; set; }
[DisplayName("Änderungsdatum")]
public DateTime ModificationTimeStamp { get; set; }
[DisplayName("vollständiger Kurzname")] [DisplayName("vollständiger Kurzname")]
public virtual string FullShortName { get; set; } public string FullShortName { get; set; }
} }
public enum OrganizationType public enum OrganizationType

View File

@@ -11,6 +11,8 @@
<input hidden asp-for="Id" class="form-control" /> <input hidden asp-for="Id" class="form-control" />
<input hidden asp-for="FullShortName" class="form-control" /> <input hidden asp-for="FullShortName" class="form-control" />
<input hidden asp-for="CreationTimeStamp" class="form-control" />
<input hidden asp-for="ModificationTimeStamp" class="form-control" />
<div class="form-group"> <div class="form-group">
<label asp-for="ShortName" class="control-label"></label> <label asp-for="ShortName" class="control-label"></label>
@@ -38,7 +40,7 @@
<div class="form-group"> <div class="form-group">
<label asp-for="ParentId" class="control-label"></label> <label asp-for="ParentId" class="control-label"></label>
<select asp-for="ParentId" asp-items="@(new SelectList(Model.Parents, "Id", "Name"))" class="form-control" ></select> <select asp-for="ParentId" asp-items="@(new SelectList(from x in Model.Parents select new { Value= x.Id, Text = x.FullShortName + " - " + x.Name}, "Value", "Text"))" class="form-control"></select>
</div> </div>
<div class="form-group"> <div class="form-group">

View File

@@ -10,10 +10,10 @@
<thead> <thead>
<tr> <tr>
<th> <th>
@Html.DisplayNameFor(x => x.GetEnumerator().Current.Name) @Html.DisplayNameFor(x => x.GetEnumerator().Current.ShortName)
</th> </th>
<th> <th>
@Html.DisplayNameFor(x => x.GetEnumerator().Current.ShortName) @Html.DisplayNameFor(x => x.GetEnumerator().Current.Name)
</th> </th>
<th> <th>
@Html.DisplayNameFor(x => x.GetEnumerator().Current.Description) @Html.DisplayNameFor(x => x.GetEnumerator().Current.Description)
@@ -30,7 +30,7 @@
<th> <th>
<a onclick="showInPopup('@Url.Action("AddOrEdit", "Organization", new {id = 0}, Context.Request.Scheme)','neue Organisation')" class="btn btn-success text-white"> <a onclick="showInPopup('@Url.Action("AddOrEdit", "Organization", new {id = 0}, Context.Request.Scheme)','neue Organisation')" class="btn btn-success text-white">
<i class="bi bi-plus-square"></i> <i class="bi bi-plus-square"></i>
neue neu
</a> </a>
</th> </th>
</tr> </tr>
@@ -45,10 +45,10 @@
@Html.DisplayFor(modelItem => item.Id) @Html.DisplayFor(modelItem => item.Id)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.Name) @Html.DisplayFor(modelItem => item.ShortName)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.ShortName) @Html.DisplayFor(modelItem => item.Name)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => @item.Description) @Html.DisplayFor(modelItem => @item.Description)