214 lines
6.8 KiB
C#
214 lines
6.8 KiB
C#
namespace EgwCoreLib.Lux.Data.Repository.Job
|
|
{
|
|
public class JobTaskRepository : BaseRepository, IJobTaskRepository
|
|
{
|
|
#region Public Constructors
|
|
|
|
public JobTaskRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
|
{
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> AddAsync(JobTaskModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
await dbCtx.DbSetJobTask.AddAsync(entity);
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> DeleteAsync(JobTaskModel rec2del)
|
|
{
|
|
// Add validation for null entity
|
|
if (rec2del == null) return false;
|
|
|
|
await using var dbCtx = await CreateContextAsync();
|
|
|
|
// Wrap in transaction for atomicity (multi-row update + delete)
|
|
await using var tx = await dbCtx.Database.BeginTransactionAsync();
|
|
try
|
|
{
|
|
var dbResult = await dbCtx.DbSetJobTask
|
|
.FirstOrDefaultAsync(x => x.JobID == rec2del.JobID);
|
|
|
|
if (dbResult == null)
|
|
return false;
|
|
|
|
var list2Move = await dbCtx.DbSetJobTask
|
|
.Where(x => x.JobID == rec2del.JobID && x.Index > dbResult.Index)
|
|
.ToListAsync();
|
|
|
|
foreach (var item in list2Move)
|
|
{
|
|
item.Index--;
|
|
dbCtx.Entry(item).State = EntityState.Modified;
|
|
}
|
|
|
|
dbCtx.DbSetJobTask.Remove(dbResult);
|
|
|
|
bool done = await dbCtx.SaveChangesAsync() > 0;
|
|
|
|
if (done)
|
|
await tx.CommitAsync();
|
|
|
|
return done;
|
|
}
|
|
catch
|
|
{
|
|
await tx.RollbackAsync();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<JobTaskModel>> GetAllAsync()
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetJobTask
|
|
.Include(c => c.TagNav)
|
|
.Include(c => c.JobStepNav)
|
|
.AsNoTracking()
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<JobTaskModel?> GetByIdAsync(int recId)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
return await dbCtx.DbSetJobTask.FirstOrDefaultAsync(x => x.JobID == recId);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> MergeTagsAsync(int JobID, List<string> reqTagList)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
|
|
// Wrap in transaction for atomicity (multi-row add + multi-row remove)
|
|
await using var tx = await dbCtx.Database.BeginTransactionAsync();
|
|
try
|
|
{
|
|
var currRec = await dbCtx.DbSetJobTask
|
|
.Where(x => x.JobID == JobID)
|
|
.Include(t => t.TagNav)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (currRec == null)
|
|
return false;
|
|
|
|
var currentTags = currRec.TagNav.Select(t => t.CodTag).ToList();
|
|
|
|
// calcolo modifiche
|
|
var toAdd = reqTagList.Except(currentTags).ToList();
|
|
var toRemove = currentTags.Except(reqTagList).ToList();
|
|
|
|
// aggiunte
|
|
foreach (var tag in toAdd)
|
|
{
|
|
currRec.TagNav.Add(new JobTaskTagModel
|
|
{
|
|
JobID = JobID,
|
|
CodTag = tag
|
|
});
|
|
}
|
|
// rimozioni
|
|
foreach (var tag in toRemove)
|
|
{
|
|
var entity = currRec.TagNav.FirstOrDefault(t => t.CodTag == tag);
|
|
if (entity != null)
|
|
dbCtx.Remove(entity);
|
|
}
|
|
|
|
dbCtx.Entry(currRec).State = EntityState.Modified;
|
|
|
|
bool done = await dbCtx.SaveChangesAsync() > 0;
|
|
|
|
if (done)
|
|
await tx.CommitAsync();
|
|
|
|
return done;
|
|
}
|
|
catch
|
|
{
|
|
await tx.RollbackAsync();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> MoveAsync(JobTaskModel selRec, bool moveUp)
|
|
{
|
|
// Add validation for null entity
|
|
if (selRec == null) return false;
|
|
|
|
await using var dbCtx = await CreateContextAsync();
|
|
|
|
// Wrap in transaction for atomicity (multi-row update)
|
|
await using var tx = await dbCtx.Database.BeginTransactionAsync();
|
|
try
|
|
{
|
|
var currRec = await dbCtx.DbSetJobTask
|
|
.FirstOrDefaultAsync(x => x.JobID == selRec.JobID);
|
|
|
|
if (currRec == null)
|
|
return false;
|
|
|
|
int numRec = await dbCtx.DbSetJobTask
|
|
.CountAsync();
|
|
|
|
int newPos = moveUp ? currRec.Index - 1 : currRec.Index + 1;
|
|
|
|
bool canMove = moveUp ? newPos > 0 : newPos <= numRec;
|
|
if (!canMove)
|
|
return false;
|
|
|
|
var otherRec = await dbCtx.DbSetJobTask
|
|
.FirstOrDefaultAsync(x => x.JobID == selRec.JobID && x.Index == newPos);
|
|
|
|
if (otherRec == null)
|
|
return false;
|
|
|
|
otherRec.Index = currRec.Index;
|
|
currRec.Index = newPos;
|
|
|
|
dbCtx.Entry(otherRec).State = EntityState.Modified;
|
|
dbCtx.Entry(currRec).State = EntityState.Modified;
|
|
|
|
bool done = await dbCtx.SaveChangesAsync() > 0;
|
|
|
|
if (done)
|
|
await tx.CommitAsync();
|
|
|
|
return done;
|
|
}
|
|
catch
|
|
{
|
|
await tx.RollbackAsync();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> UpdateAsync(JobTaskModel entity)
|
|
{
|
|
await using var dbCtx = await CreateContextAsync();
|
|
var trackedEntity = await dbCtx.DbSetJobTask.FirstOrDefaultAsync(x => x.JobID == entity.JobID);
|
|
|
|
if (trackedEntity != null)
|
|
{
|
|
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
|
|
}
|
|
else
|
|
{
|
|
dbCtx.DbSetJobTask.Update(entity);
|
|
}
|
|
return await dbCtx.SaveChangesAsync() > 0;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
}
|