using Microsoft.EntityFrameworkCore; using SteUp.Shared.Core.Entities; using SteUp.Shared.Core.Interface.LocalDb; namespace SteUp.Data.LocalDb.EntityServices; public class IspezioniService(AppDbContext db) : IIspezioniService { public Task GetIspezioneAsync(string codMdep, DateOnly data, string rilevatore) => db.Ispezioni .Include(x => x.Schede) .FirstOrDefaultAsync(x => x.CodMdep == codMdep && x.Data == data && x.Rilevatore == rilevatore); public Task> GetAllIspezioniWithSchedeAsync() => db.Ispezioni .Include(x => x.Schede) .AsNoTracking() .OrderByDescending(x => x.Data) .ToListAsync(); public async Task AddIspezioneAsync(Ispezione ispezione) { db.Ispezioni.Add(ispezione); await db.SaveChangesAsync(); } public async Task GetOrCreateIspezioneAsync(string codMdep, DateOnly data, string rilevatore) { var existing = await db.Ispezioni .AsNoTracking() .FirstOrDefaultAsync(x => x.CodMdep == codMdep && x.Data == data && x.Rilevatore == rilevatore); if (existing != null) return existing; var created = new Ispezione { CodMdep = codMdep, Data = data, Rilevatore = rilevatore }; db.Ispezioni.Add(created); await db.SaveChangesAsync(); return await db.Ispezioni .AsNoTracking() .FirstAsync(x => x.CodMdep == codMdep && x.Data == data && x.Rilevatore == rilevatore); } public async Task UpdateIspezioneAsync(Ispezione ispezione) { db.Ispezioni.Update(ispezione); await db.SaveChangesAsync(); return true; } /// /// Cancella l'ispezione e tutte le schede collegate. /// public async Task DeleteIspezioneAsync(string codMdep, DateOnly data, string rilevatore) { var ispezione = await db.Ispezioni .FirstOrDefaultAsync(x => x.CodMdep == codMdep && x.Data == data && x.Rilevatore == rilevatore); if (ispezione is null) return false; db.Ispezioni.Remove(ispezione); await db.SaveChangesAsync(); return true; } 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; db.Schede.Add(scheda); await db.SaveChangesAsync(); } public Task> GetAllSchedeOfIspezioneAsync(string codMdep, DateOnly data, string rilevatore) => db.Schede .AsNoTracking() .Where(x => x.CodMdep == codMdep && x.Data == data && x.Rilevatore == rilevatore) .ToListAsync(); public Task GetSchedaAsync(int schedaId) => db.Schede .AsNoTracking() .FirstOrDefaultAsync(x => x.Id == schedaId); public Task GetSchedaWithIspezioneAsync(int schedaId) => db.Schede .Include(x => x.Ispezione) .AsNoTracking() .FirstOrDefaultAsync(x => x.Id == schedaId); public async Task DeleteSchedaAsync(int schedaId) { var scheda = await db.Schede.FirstOrDefaultAsync(x => x.Id == schedaId); if (scheda is null) return false; db.Schede.Remove(scheda); await db.SaveChangesAsync(); return true; } public async Task UpdateSchedaAsync(Scheda scheda) { var exists = await db.Schede.AnyAsync(x => x.Id == scheda.Id); if (!exists) return false; db.Schede.Update(scheda); await db.SaveChangesAsync(); return true; } /// /// Cancella tutte le schede di una ispezione senza cancellare l'ispezione. /// public async Task DeleteAllSchedeOfIspezioneAsync(string codMdep, DateOnly data, string rilevatore) { var schede = await db.Schede .Where(s => s.CodMdep == codMdep && s.Data == data && s.Rilevatore == rilevatore) .ToListAsync(); if (schede.Count == 0) return 0; db.Schede.RemoveRange(schede); return await db.SaveChangesAsync(); } }