Review namespace Task --> Job + fix JobStep/JobTask
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
using EgwCoreLib.Lux.Data.DbModel.Job;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EgwCoreLib.Lux.Data.Repository.Job
|
||||
{
|
||||
public class JobStepRepository : BaseRepository, IJobStepRepository
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
public JobStepRepository(IDbContextFactory<DataLayerContext> ctxFactory) : base(ctxFactory)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public async Task<bool> AddAsync(JobStepModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
await dbCtx.DbSetJobStep.AddAsync(entity);
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(JobStepModel rec2del)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
|
||||
var dbResult = await dbCtx.DbSetJobStep
|
||||
.FirstOrDefaultAsync(x => x.JobStepID == rec2del.JobStepID);
|
||||
|
||||
if (dbResult == null)
|
||||
return false;
|
||||
|
||||
var list2Move = await dbCtx.DbSetJobStep
|
||||
.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.DbSetJobStep.Remove(dbResult);
|
||||
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<JobStepModel?> GetByIdAsync(int recId)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetJobStep.FirstOrDefaultAsync(x => x.JobStepID == recId);
|
||||
}
|
||||
|
||||
public async Task<List<JobStepModel>> GetByParentAsync(int jobID)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
return await dbCtx.DbSetJobStep
|
||||
.Where(x => x.JobID == jobID)
|
||||
.Include(c => c.JobNav)
|
||||
.Include(c => c.PhaseNav)
|
||||
.Include(c => c.ResourceNav)
|
||||
.Include(c => c.TagNav)
|
||||
.Include(c => c.ResourceNav.DriverNav)
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> MoveAsync(JobStepModel selRec, bool moveUp)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
var currRec = await dbCtx.DbSetJobStep
|
||||
.FirstOrDefaultAsync(x => x.JobStepID == selRec.JobStepID);
|
||||
|
||||
if (currRec == null)
|
||||
return false;
|
||||
|
||||
int numRec = await dbCtx.DbSetJobStep
|
||||
.CountAsync(x => x.JobID == selRec.JobID);
|
||||
|
||||
int newPos = moveUp ? currRec.Index - 1 : currRec.Index + 1;
|
||||
|
||||
bool canMove = moveUp ? newPos > 0 : newPos <= numRec;
|
||||
if (!canMove)
|
||||
return false;
|
||||
|
||||
var otherRec = await dbCtx.DbSetJobStep
|
||||
.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;
|
||||
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(JobStepModel entity)
|
||||
{
|
||||
await using var dbCtx = await CreateContextAsync();
|
||||
var trackedEntity = dbCtx.DbSetJobStep.Local.FirstOrDefault(x => x.JobStepID == entity.JobStepID);
|
||||
|
||||
if (trackedEntity != null)
|
||||
{
|
||||
dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
dbCtx.DbSetJobStep.Update(entity);
|
||||
}
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user