Neuerstellung
This commit is contained in:
15
scr/FSI.BT.IR.Organization.Db/Data/AppDbContext.cs
Normal file
15
scr/FSI.BT.IR.Organization.Db/Data/AppDbContext.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FSI.BT.IR.Organization.Db.Data
|
||||
{
|
||||
public class AppDbContext : DbContext, IDataContext
|
||||
{
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DbSet<Models.Organization> Organizations { get; set; }
|
||||
}
|
||||
}
|
||||
16
scr/FSI.BT.IR.Organization.Db/Data/AppDbContextFactory.cs
Normal file
16
scr/FSI.BT.IR.Organization.Db/Data/AppDbContextFactory.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FSI.BT.IR.Organization.Db.Data
|
||||
{
|
||||
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
|
||||
{
|
||||
public AppDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
|
||||
optionsBuilder.UseSqlite("Data Source=FSI.BT.IR.Organization.db");
|
||||
|
||||
return new AppDbContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
scr/FSI.BT.IR.Organization.Db/Data/IDataContext.cs
Normal file
15
scr/FSI.BT.IR.Organization.Db/Data/IDataContext.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FSI.BT.IR.Organization.Db.Data
|
||||
{
|
||||
public interface IDataContext
|
||||
{
|
||||
|
||||
public DbSet<Models.Organization> Organizations { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
BIN
scr/FSI.BT.IR.Organization.Db/FSI.BT.IR.Organization.db
Normal file
BIN
scr/FSI.BT.IR.Organization.Db/FSI.BT.IR.Organization.db
Normal file
Binary file not shown.
82
scr/FSI.BT.IR.Organization.Db/Migrations/20241025073330_Init.Designer.cs
generated
Normal file
82
scr/FSI.BT.IR.Organization.Db/Migrations/20241025073330_Init.Designer.cs
generated
Normal file
@@ -0,0 +1,82 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using FSI.BT.IR.Organization.Db.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace FSI.BT.IR.Organization.Db.Migrations
|
||||
{
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20241025073330_Init")]
|
||||
partial class Init
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "8.0.10");
|
||||
|
||||
modelBuilder.Entity("FSI.BT.IR.Organization.Db.Models.Organization", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<DateTime>("Created")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasMaxLength(128)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("FullShortName")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("ParentId")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("ShortName")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Type")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<DateTime>("Updated")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ParentId");
|
||||
|
||||
b.ToTable("Organizations");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FSI.BT.IR.Organization.Db.Models.Organization", b =>
|
||||
{
|
||||
b.HasOne("FSI.BT.IR.Organization.Db.Models.Organization", "Parent")
|
||||
.WithMany("Parents")
|
||||
.HasForeignKey("ParentId");
|
||||
|
||||
b.Navigation("Parent");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FSI.BT.IR.Organization.Db.Models.Organization", b =>
|
||||
{
|
||||
b.Navigation("Parents");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace FSI.BT.IR.Organization.Db.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Init : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Organizations",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
Name = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
|
||||
ShortName = table.Column<string>(type: "TEXT", maxLength: 16, nullable: false),
|
||||
Description = table.Column<string>(type: "TEXT", maxLength: 128, nullable: true),
|
||||
ParentId = table.Column<int>(type: "INTEGER", nullable: true),
|
||||
Type = table.Column<int>(type: "INTEGER", nullable: false),
|
||||
Created = table.Column<DateTime>(type: "TEXT", nullable: false),
|
||||
Updated = table.Column<DateTime>(type: "TEXT", nullable: false),
|
||||
FullShortName = table.Column<string>(type: "TEXT", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Organizations", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Organizations_Organizations_ParentId",
|
||||
column: x => x.ParentId,
|
||||
principalTable: "Organizations",
|
||||
principalColumn: "Id");
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Organizations_ParentId",
|
||||
table: "Organizations",
|
||||
column: "ParentId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Organizations");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using FSI.BT.IR.Organization.Db.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace FSI.BT.IR.Organization.Db.Migrations
|
||||
{
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
partial class AppDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "8.0.10");
|
||||
|
||||
modelBuilder.Entity("FSI.BT.IR.Organization.Db.Models.Organization", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<DateTime>("Created")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasMaxLength(128)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("FullShortName")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("ParentId")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("ShortName")
|
||||
.IsRequired()
|
||||
.HasMaxLength(16)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Type")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<DateTime>("Updated")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ParentId");
|
||||
|
||||
b.ToTable("Organizations");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FSI.BT.IR.Organization.Db.Models.Organization", b =>
|
||||
{
|
||||
b.HasOne("FSI.BT.IR.Organization.Db.Models.Organization", "Parent")
|
||||
.WithMany("Parents")
|
||||
.HasForeignKey("ParentId");
|
||||
|
||||
b.Navigation("Parent");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FSI.BT.IR.Organization.Db.Models.Organization", b =>
|
||||
{
|
||||
b.Navigation("Parents");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
48
scr/FSI.BT.IR.Organization.Db/Models/Organization.cs
Normal file
48
scr/FSI.BT.IR.Organization.Db/Models/Organization.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace FSI.BT.IR.Organization.Db.Models
|
||||
{
|
||||
public class Organization
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[DisplayName("Bezeichnung")]
|
||||
[Required(ErrorMessage = "Pflichtfeld")]
|
||||
[StringLength(64)]
|
||||
public string Name { get; set; }
|
||||
|
||||
[DisplayName("Kurzname")]
|
||||
[Required(ErrorMessage = "Pflichtfeld")]
|
||||
[StringLength(16)]
|
||||
public string ShortName { get; set; }
|
||||
|
||||
[DisplayName("Beschreibung")]
|
||||
[StringLength(128)]
|
||||
public string? Description { get; set; }
|
||||
|
||||
[DisplayName("Übergeordnetes Objekt")]
|
||||
public int? ParentId { get; set; }
|
||||
|
||||
[DisplayName("Typ")]
|
||||
[Required(ErrorMessage = "Pflichtfeld")]
|
||||
public OrganizationType Type { get; set; }
|
||||
|
||||
[DisplayName("Erstellungsdatum")]
|
||||
[Required(ErrorMessage = "Pflichtfeld")]
|
||||
public DateTime Created { get; set; }
|
||||
|
||||
[DisplayName("Bearbeitungsdatum")]
|
||||
[Required(ErrorMessage = "Pflichtfeld")]
|
||||
public DateTime Updated { get; set; }
|
||||
|
||||
[DisplayName("Bereich")]
|
||||
public string FullShortName { get; set; }
|
||||
|
||||
|
||||
public virtual Organization? Parent { get; set; }
|
||||
public virtual ICollection<Organization> Parents { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
18
scr/FSI.BT.IR.Organization.Db/Models/OrganizationType.cs
Normal file
18
scr/FSI.BT.IR.Organization.Db/Models/OrganizationType.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace FSI.BT.IR.Organization.Db.Models
|
||||
{
|
||||
public enum OrganizationType
|
||||
{
|
||||
[Display(Name = "Firma")]
|
||||
company,
|
||||
[Display(Name = "Werk")]
|
||||
factory,
|
||||
[Display(Name = "Bereich")]
|
||||
area,
|
||||
[Display(Name = "Anlage")]
|
||||
plant,
|
||||
[Display(Name = "Teilanlage")]
|
||||
subPlant,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace FSI.BT.IR.Organization.Db.Services
|
||||
{
|
||||
public interface IOrganizationService
|
||||
{
|
||||
Task<Models.Organization> GetDataByIdAsync(int id);
|
||||
|
||||
Task<List<Models.Organization>> GetDatasAsync();
|
||||
|
||||
Task<Models.Organization> SetFullShortName(Models.Organization organization);
|
||||
|
||||
Task AddAsync(Models.Organization organization);
|
||||
|
||||
Task UpdateAsync(Models.Organization organization);
|
||||
|
||||
Task RemoveAsync(Models.Organization organization);
|
||||
|
||||
Task SetParentIdNullAsync (int id);
|
||||
}
|
||||
}
|
||||
100
scr/FSI.BT.IR.Organization.Db/Services/OrganizationService.cs
Normal file
100
scr/FSI.BT.IR.Organization.Db/Services/OrganizationService.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using FSI.BT.IR.Organization.Db.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FSI.BT.IR.Organization.Db.Services
|
||||
{
|
||||
public class OrganizationService : IOrganizationService
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public OrganizationService(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Models.Organization> GetDataByIdAsync(int id)
|
||||
{
|
||||
return await _context.Organizations.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<List<Models.Organization>> GetDatasAsync()
|
||||
{
|
||||
return await _context.Organizations.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Models.Organization> SetFullShortName(Models.Organization organization)
|
||||
{
|
||||
organization.FullShortName = await GetFullShortName(organization.ParentId, organization.ShortName);
|
||||
return organization;
|
||||
}
|
||||
|
||||
public async Task AddAsync(Models.Organization organization)
|
||||
{
|
||||
organization.FullShortName = await GetFullShortName(organization.ParentId, organization.ShortName); // Kürzel updaten
|
||||
_context.Add(organization);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(Models.Organization organization)
|
||||
{
|
||||
|
||||
_context.Update(organization);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
organization.FullShortName = await GetFullShortName(organization.ParentId, organization.ShortName);
|
||||
_context.Update(organization);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(Models.Organization organization)
|
||||
{
|
||||
_context.Remove(organization);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task SetParentIdNullAsync(int id)
|
||||
{
|
||||
_context.Organizations.Where(x => x.ParentId == id).ForEachAsync(x => x.ParentId = null);
|
||||
}
|
||||
|
||||
|
||||
#region Methods
|
||||
private async Task UpdateAllFullShortName()
|
||||
{
|
||||
List<Models.Organization> organizations = await GetDatasAsync();
|
||||
|
||||
foreach (var organization in organizations)
|
||||
{
|
||||
organization.FullShortName = await GetFullShortName(organization.ParentId, organization.ShortName);
|
||||
_context.Update(organization);
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
}
|
||||
|
||||
private async Task<string> GetFullShortName(int? parentId, string? shortName)
|
||||
{
|
||||
|
||||
foreach (var item in _context.Organizations.ToList())
|
||||
{
|
||||
if (item.Id == parentId)
|
||||
{
|
||||
if (item.ParentId.HasValue)
|
||||
{
|
||||
return await GetFullShortName(item.ParentId.Value, item.ShortName + " " + shortName);
|
||||
}
|
||||
else
|
||||
{
|
||||
return item.ShortName + shortName + " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return shortName;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user