@@ -16,16 +16,13 @@ public CorrelationIdsManager(DbContext dbContext = null)
1616 }
1717
1818 #region Create
19-
19+
2020 private async Task < CorrelationId > AddAsync ( CorrelationId correlationId )
2121 {
22- using ( var dbContext = new CSPDatabaseModelEntities ( ) )
23- {
24- CorrelationId newCorrelationId = dbContext . CorrelationIds . Add ( correlationId ) ;
25- await dbContext . SaveChangesAsync ( ) ;
22+ CorrelationId newCorrelationId = _dbContext . CorrelationIds . Add ( correlationId ) ;
23+ await _dbContext . SaveChangesAsync ( ) ;
2624
27- return newCorrelationId ;
28- }
25+ return newCorrelationId ;
2926 }
3027
3128 public Task < CorrelationId > AddNewRunAsync ( )
@@ -35,7 +32,7 @@ public Task<CorrelationId> AddNewRunAsync()
3532 Id = Guid . NewGuid ( ) ,
3633 StartDateTime = DateTime . UtcNow ,
3734 Status = "RUNNING" ,
38- EndDateTime = null
35+ EndDateTime = null
3936 } ) ;
4037 }
4138
@@ -45,25 +42,28 @@ public Task<CorrelationId> AddNewRunAsync()
4542
4643 private async Task < CorrelationId > UpdateAsync ( CorrelationId correlationId )
4744 {
48- using ( var dbContext = new CSPDatabaseModelEntities ( ) )
49- {
50- dbContext . Entry ( correlationId ) . State = EntityState . Modified ;
51- await dbContext . SaveChangesAsync ( ) ;
5245
53- return correlationId ;
54- }
46+ _dbContext . Entry ( correlationId ) . State = EntityState . Modified ;
47+ await _dbContext . SaveChangesAsync ( ) ;
48+ return correlationId ;
5549 }
5650
5751 public Task < CorrelationId > UpdateEndStatusAsync ( Guid correlationId , string endStatus )
5852 {
59- CorrelationId databaseCorrelationId = new CorrelationId ( )
53+
54+ // Get object to update
55+ var existentCorrelation = _dbContext . CorrelationIds . FirstOrDefault ( c => c . Id == correlationId ) ;
56+ if ( existentCorrelation == null )
6057 {
61- Id = correlationId ,
62- EndDateTime = DateTime . UtcNow ,
63- Status = endStatus
64- } ;
58+ throw new ArgumentException ( "Could not find existent correlation id: " + correlationId ) ;
59+ }
60+
61+ // else. Update
62+ existentCorrelation . EndDateTime = DateTime . UtcNow ;
63+ existentCorrelation . Status = endStatus ;
64+
65+ return this . UpdateAsync ( existentCorrelation ) ;
6566
66- return this . UpdateAsync ( databaseCorrelationId ) ;
6767 }
6868
6969 #endregion
0 commit comments