Implemetato databese locale con EntityFramework
This commit is contained in:
127
SteUp.Data/LocalDb/EntityServices/IspezioniService.cs
Normal file
127
SteUp.Data/LocalDb/EntityServices/IspezioniService.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
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<Ispezione?> 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<List<Ispezione>> 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<Ispezione> GetOrCreateIspezioneAsync(string codMdep, DateOnly data, string rilevatore)
|
||||
{
|
||||
var existing = await db.Ispezioni
|
||||
.Include(x => x.Schede)
|
||||
.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 created;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancella l'ispezione e tutte le schede collegate.
|
||||
/// </summary>
|
||||
public async Task<bool> 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<Scheda?> GetSchedaAsync(int schedaId) =>
|
||||
db.Schede
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.Id == schedaId);
|
||||
|
||||
public Task<Scheda?> GetSchedaWithIspezioneAsync(int schedaId) =>
|
||||
db.Schede
|
||||
.Include(x => x.Ispezione)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.Id == schedaId);
|
||||
|
||||
public async Task<bool> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancella tutte le schede di una ispezione senza cancellare l'ispezione.
|
||||
/// </summary>
|
||||
public async Task<int> 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user