Implentata scansione barcode e lista articoli nel form scheda

This commit is contained in:
2026-02-26 15:46:49 +01:00
parent a5e27bcf06
commit e027d8e5cf
20 changed files with 770 additions and 19 deletions

View File

@@ -8,6 +8,7 @@ public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(op
{
public DbSet<Ispezione> Ispezioni => Set<Ispezione>();
public DbSet<Scheda> Schede => Set<Scheda>();
public DbSet<SchedaArticolo> SchedaArticoli => Set<SchedaArticolo>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@@ -15,7 +16,7 @@ public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(op
modelBuilder.Entity<Ispezione>()
.HasKey(x => new { x.CodMdep, x.Data, x.Rilevatore });
modelBuilder.Entity<Scheda>()
.HasOne(x => x.Ispezione)
.WithMany(x => x.Schede)
@@ -31,5 +32,15 @@ public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(op
v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null),
v => JsonSerializer.Deserialize<List<string>>(v, (JsonSerializerOptions?)null) ?? new List<string>()
);
modelBuilder.Entity<SchedaArticolo>()
.HasOne(a => a.Scheda)
.WithMany(s => s.Articoli)
.HasForeignKey(a => a.SchedaId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<SchedaArticolo>()
.HasIndex(a => new { a.SchedaId, a.Barcode })
.IsUnique();
}
}

View File

@@ -9,6 +9,7 @@ public class IspezioniService(AppDbContext db) : IIspezioniService
public Task<Ispezione?> GetIspezioneAsync(string codMdep, DateOnly data, string rilevatore) =>
db.Ispezioni
.Include(x => x.Schede)
.ThenInclude(s => s.Articoli)
.FirstOrDefaultAsync(x =>
x.CodMdep == codMdep &&
x.Data == data &&
@@ -17,6 +18,7 @@ public class IspezioniService(AppDbContext db) : IIspezioniService
public Task<List<Ispezione>> GetAllIspezioniWithSchedeAsync() =>
db.Ispezioni
.Include(x => x.Schede)
.ThenInclude(s => s.Articoli)
.AsNoTracking()
.OrderByDescending(x => x.Data)
.ToListAsync();
@@ -48,7 +50,7 @@ public class IspezioniService(AppDbContext db) : IIspezioniService
db.Ispezioni.Add(created);
await db.SaveChangesAsync();
return await db.Ispezioni
.AsNoTracking()
.FirstAsync(x =>
@@ -65,7 +67,7 @@ public class IspezioniService(AppDbContext db) : IIspezioniService
}
/// <summary>
/// Cancella l'ispezione e tutte le schede collegate.
/// Cancella l'ispezione e tutte le schede collegate (e relativi articoli via cascade).
/// </summary>
public async Task<bool> DeleteIspezioneAsync(string codMdep, DateOnly data, string rilevatore)
{
@@ -85,13 +87,20 @@ public class IspezioniService(AppDbContext db) : IIspezioniService
public async Task AddSchedaAsync(string codMdep, DateOnly data, string rilevatore, Scheda scheda)
{
// assicura che il parent esista
await GetOrCreateIspezioneAsync(codMdep, data, rilevatore);
scheda.CodMdep = codMdep;
scheda.Data = data;
scheda.Rilevatore = rilevatore;
if (scheda.Articoli is { Count: > 0 })
{
foreach (var a in scheda.Articoli)
{
a.SchedaId = scheda.Id;
}
}
db.Schede.Add(scheda);
await db.SaveChangesAsync();
}
@@ -99,6 +108,7 @@ public class IspezioniService(AppDbContext db) : IIspezioniService
public Task<List<Scheda>> GetAllSchedeOfIspezioneAsync(string codMdep, DateOnly data, string rilevatore) =>
db.Schede
.AsNoTracking()
.Include(s => s.Articoli)
.Where(x => x.CodMdep == codMdep &&
x.Data == data &&
x.Rilevatore == rilevatore)
@@ -107,11 +117,13 @@ public class IspezioniService(AppDbContext db) : IIspezioniService
public Task<Scheda?> GetSchedaAsync(int schedaId) =>
db.Schede
.AsNoTracking()
.Include(s => s.Articoli)
.FirstOrDefaultAsync(x => x.Id == schedaId);
public Task<Scheda?> GetSchedaWithIspezioneAsync(int schedaId) =>
db.Schede
.Include(x => x.Ispezione)
.Include(x => x.Articoli)
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == schedaId);
@@ -128,11 +140,56 @@ public class IspezioniService(AppDbContext db) : IIspezioniService
public async Task<bool> UpdateSchedaAsync(Scheda scheda)
{
var exists = await db.Schede.AnyAsync(x => x.Id == scheda.Id);
if (!exists)
var existing = await db.Schede
.Include(s => s.Articoli)
.FirstOrDefaultAsync(s => s.Id == scheda.Id);
if (existing is null)
return false;
db.Schede.Update(scheda);
db.Entry(existing).CurrentValues.SetValues(scheda);
var incoming = scheda.Articoli;
foreach (var toRemove in existing.Articoli
.Where(ea => incoming.All(ia => ia.Id != ea.Id))
.ToList())
{
existing.Articoli.Remove(toRemove);
}
foreach (var ia in incoming)
{
if (ia.Id == 0)
{
existing.Articoli.Add(new SchedaArticolo
{
Barcode = ia.Barcode,
Descrizione = ia.Descrizione,
SchedaId = existing.Id
});
}
else
{
var ea = existing.Articoli.FirstOrDefault(x => x.Id == ia.Id);
if (ea is null)
{
existing.Articoli.Add(new SchedaArticolo
{
Id = ia.Id,
Barcode = ia.Barcode,
Descrizione = ia.Descrizione,
SchedaId = existing.Id
});
}
else
{
ea.Barcode = ia.Barcode;
ea.Descrizione = ia.Descrizione;
}
}
}
await db.SaveChangesAsync();
return true;
}

View File

@@ -0,0 +1,148 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SteUp.Data.LocalDb;
#nullable disable
namespace SteUp.Data.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20260225104013_AddListArticoli")]
partial class AddListArticoli
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.3");
modelBuilder.Entity("SteUp.Shared.Core.Entities.Ispezione", b =>
{
b.Property<string>("CodMdep")
.HasColumnType("TEXT");
b.Property<DateOnly>("Data")
.HasColumnType("TEXT");
b.Property<string>("Rilevatore")
.HasColumnType("TEXT");
b.Property<int>("Stato")
.HasColumnType("INTEGER");
b.HasKey("CodMdep", "Data", "Rilevatore");
b.ToTable("Ispezioni");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.Scheda", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("ActivityTypeId")
.HasColumnType("TEXT");
b.Property<string>("CodJfas")
.HasColumnType("TEXT");
b.Property<string>("CodMdep")
.IsRequired()
.HasColumnType("TEXT");
b.Property<DateOnly>("Data")
.HasColumnType("TEXT");
b.Property<string>("DescrizioneReparto")
.HasColumnType("TEXT");
b.Property<string>("ImageNames")
.HasColumnType("TEXT");
b.Property<string>("Note")
.HasColumnType("TEXT");
b.Property<string>("Responsabile")
.HasColumnType("TEXT");
b.Property<string>("Rilevatore")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("Scadenza")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("CodMdep", "Data", "Rilevatore");
b.ToTable("Schede");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.SchedaArticolo", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Barcode")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("TEXT");
b.Property<string>("Descrizione")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("TEXT");
b.Property<int>("SchedaId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("SchedaId", "Barcode")
.IsUnique();
b.ToTable("SchedaArticoli");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.Scheda", b =>
{
b.HasOne("SteUp.Shared.Core.Entities.Ispezione", "Ispezione")
.WithMany("Schede")
.HasForeignKey("CodMdep", "Data", "Rilevatore")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Ispezione");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.SchedaArticolo", b =>
{
b.HasOne("SteUp.Shared.Core.Entities.Scheda", "Scheda")
.WithMany("Articoli")
.HasForeignKey("SchedaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Scheda");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.Ispezione", b =>
{
b.Navigation("Schede");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.Scheda", b =>
{
b.Navigation("Articoli");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,48 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SteUp.Data.Migrations
{
/// <inheritdoc />
public partial class AddListArticoli : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "SchedaArticoli",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
SchedaId = table.Column<int>(type: "INTEGER", nullable: false),
Barcode = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
Descrizione = table.Column<string>(type: "TEXT", maxLength: 256, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SchedaArticoli", x => x.Id);
table.ForeignKey(
name: "FK_SchedaArticoli_Schede_SchedaId",
column: x => x.SchedaId,
principalTable: "Schede",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_SchedaArticoli_SchedaId_Barcode",
table: "SchedaArticoli",
columns: new[] { "SchedaId", "Barcode" },
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "SchedaArticoli");
}
}
}

View File

@@ -81,6 +81,33 @@ namespace SteUp.Data.Migrations
b.ToTable("Schede");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.SchedaArticolo", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Barcode")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("TEXT");
b.Property<string>("Descrizione")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("TEXT");
b.Property<int>("SchedaId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("SchedaId", "Barcode")
.IsUnique();
b.ToTable("SchedaArticoli");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.Scheda", b =>
{
b.HasOne("SteUp.Shared.Core.Entities.Ispezione", "Ispezione")
@@ -92,10 +119,26 @@ namespace SteUp.Data.Migrations
b.Navigation("Ispezione");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.SchedaArticolo", b =>
{
b.HasOne("SteUp.Shared.Core.Entities.Scheda", "Scheda")
.WithMany("Articoli")
.HasForeignKey("SchedaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Scheda");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.Ispezione", b =>
{
b.Navigation("Schede");
});
modelBuilder.Entity("SteUp.Shared.Core.Entities.Scheda", b =>
{
b.Navigation("Articoli");
});
#pragma warning restore 612, 618
}
}