DB integration added, multiple images for equipment

This commit is contained in:
retr0volkov
2025-07-15 11:14:28 +03:00 Unverified
parent 7a34fc2dbb
commit 8ddce4005f
38 changed files with 627 additions and 297 deletions
+114 -31
View File
@@ -1,4 +1,5 @@
using GazpromApp.Models;
using System;
using GazpromApp.Models;
using Microsoft.EntityFrameworkCore;
namespace GazpromApp.Utils;
@@ -7,50 +8,132 @@ public class AppDbContext : DbContext
{
public DbSet<Image> Image { get; set; }
public DbSet<Equipment> Equipment { get; set; }
public DbSet<Inspection> Inspection { get; set; }
public DbSet<Property> Property { get; set; }
public DbSet<Point> Point { get; set; }
public string DbPath { get; }
public AppDbContext()
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
DbPath = System.IO.Path.Combine(path, "GazpromApp.db");
}
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
DbPath = System.IO.Path.Combine(path, "GazpromApp.db");
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlite("Data Source=MainDB.db");
optionsBuilder.UseSqlite($"Data Source={DbPath}");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Images
// ==== Point RELATIONSHIPS ====
modelBuilder.Entity<Point>()
.HasMany(e => e.Images)
.WithMany(e => e.Points);
.HasMany(p => p.Equipment)
.WithOne(e => e.Point)
.HasForeignKey(e => e.PointId)
.OnDelete(DeleteBehavior.Cascade); // Deleting a Point deletes its Equipment.
modelBuilder.Entity<Equipment>()
.HasOne(e => e.Image)
.WithMany(e => e.Equipment)
.HasForeignKey(e => e.ImageId);
// Properties
modelBuilder.Entity<Point>()
.HasMany(e => e.Properties)
.WithMany(e => e.Points);
modelBuilder.Entity<Equipment>()
.HasMany(e => e.Properties)
.WithMany(e => e.Equipment);
modelBuilder.Entity<Property>()
.HasMany(e => e.Properties)
.WithOne(e => e.Parent)
.HasForeignKey(e => e.ParentId);
// Equipment
modelBuilder.Entity<Equipment>()
.HasOne(e => e.Point)
.WithMany(e => e.Equipment)
.HasForeignKey(e => e.PointId);
.HasMany(p => p.Images)
.WithOne(i => i.Point)
.HasForeignKey(i => i.PointId)
.OnDelete(DeleteBehavior.Cascade); // Deleting a Point deletes its Images.
// ==== Equipment RELATIONSHIPS ====
modelBuilder.Entity<Equipment>()
.HasMany(e => e.Inspections)
.WithOne(e => e.Equipment)
.HasForeignKey(e => e.EquipmentId);
.WithOne(i => i.Equipment)
.HasForeignKey(i => i.EquipmentId)
.OnDelete(DeleteBehavior.Cascade); // Deleting Equipment deletes its Inspections.
modelBuilder.Entity<Equipment>()
.HasMany(e => e.Images)
.WithOne(i => i.Equipment)
.HasForeignKey(i => i.EquipmentId)
.OnDelete(DeleteBehavior.Cascade); // Equipment has one Image.
// ==== PROPERTY RELATIONSHIPS ====
// Property as a child of a Point
modelBuilder.Entity<Point>()
.HasMany(p => p.Properties)
.WithOne(prop => prop.Point)
.HasForeignKey(prop => prop.PointId)
.IsRequired(false)
.OnDelete(DeleteBehavior.Cascade); // Deleting a Point deletes its Properties.
// Property as a child of an Equipment
modelBuilder.Entity<Equipment>()
.HasMany(e => e.Properties)
.WithOne(prop => prop.Equipment)
.HasForeignKey(prop => prop.EquipmentId)
.IsRequired(false)
.OnDelete(DeleteBehavior.Cascade); // Deleting Equipment deletes its Properties.
// Property as a child of another Property (self-referencing)
modelBuilder.Entity<Property>()
.HasMany(p => p.Properties)
.WithOne(p => p.Parent)
.HasForeignKey(p => p.ParentPropertyId)
.IsRequired(false)
.OnDelete(DeleteBehavior.Cascade); // Deleting a parent Property deletes its children.
// modelBuilder.Entity<Point>()
// .Property(p => p.Id)
// .ValueGeneratedOnAdd();
// modelBuilder.Entity<Equipment>()
// .Property(e => e.Id)
// .ValueGeneratedOnAdd();
// modelBuilder.Entity<Image>()
// .Property(i => i.Id)
// .ValueGeneratedOnAdd();
// modelBuilder.Entity<Property>()
// .Property(p => p.Id)
// .ValueGeneratedOnAdd();
// modelBuilder.Entity<Inspection>()
// .Property(i => i.Id)
// .ValueGeneratedOnAdd();
//
// modelBuilder.Entity<Point>()
// .HasMany(p => p.Images)
// .WithMany(i => i.Points);
// modelBuilder.Entity<Point>()
// .HasMany(p => p.Equipment)
// .WithOne(e => e.Point)
// .HasForeignKey(e => e.PointId);
// modelBuilder.Entity<Point>()
// .HasMany(p => p.Properties)
// .WithOne(p => p.Point)
// .HasForeignKey(p => p.ParentId)
// .IsRequired(false);
//
// modelBuilder.Entity<Equipment>()
// .HasOne(e => e.Image)
// .WithMany(i => i.Equipment);
// modelBuilder.Entity<Equipment>()
// .HasMany(e => e.Properties)
// .WithOne(p => p.Equipment)
// .HasForeignKey(p => p.ParentId)
// .IsRequired(false);
// modelBuilder.Entity<Equipment>()
// .HasMany(e => e.Inspections)
// .WithOne(i => i.Equipment)
// .HasForeignKey(i => i.EquipmentId);
//
// modelBuilder.Entity<Property>()
// .HasMany(p => p.Properties)
// .WithOne(p => p.Parent)
// .HasForeignKey(p => p.ParentId)
// .IsRequired(false);
}
}
+2 -2
View File
@@ -13,7 +13,7 @@ public static class DataSerialization
private static string LoadData(string path)
{
string text = System.IO.File.ReadAllText(path);
var text = System.IO.File.ReadAllText(path);
return text;
}
@@ -46,7 +46,7 @@ public static class Deserialization
}
catch
{
throw new ArgumentException("Ошибка расшивроки json файла");
throw new ArgumentException("Ошибка расшифровки json файла");
}
}
}
+20 -24
View File
@@ -1,34 +1,30 @@
using GazpromApp.Models;
using System.Collections.Generic;
using System.Linq;
namespace GazpromApp.Utils
{
public static class PropertyParentHelper
{
public static void SetPropertyParents(this Point point)
{
// Set parents for properties in point
foreach (var prop in point.Properties)
{
SetParentsRecursively(prop);
}
namespace GazpromApp.Utils;
// Set parents for properties in equipment
foreach (var prop in point.Equipment.SelectMany(equipment => equipment.Properties))
{
SetParentsRecursively(prop);
}
public static class PropertyParentHelper
{
public static void SetPropertyParents(this Point point)
{
foreach (var prop in point.Properties)
{
SetParentsRecursively(prop);
}
private static void SetParentsRecursively(Property property)
foreach (var prop in point.Equipment.SelectMany(equipment => equipment.Properties))
{
foreach (var childProp in property.Properties)
{
childProp.Parent = property;
childProp.ParentId = property.Id;
SetParentsRecursively(childProp);
}
SetParentsRecursively(prop);
}
}
private static void SetParentsRecursively(Property property)
{
foreach (var childProp in property.Properties)
{
childProp.Parent = property;
childProp.ParentPropertyId = property.Id;
SetParentsRecursively(childProp);
}
}
}