Implentata scansione barcode e lista articoli nel form scheda
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user