22using LantanaGroup . Link . Shared . Application . Models ;
33using LantanaGroup . Link . Shared . Application . Services . Security ;
44using LantanaGroup . Link . Shared . Domain . Repositories . Interfaces ;
5+ using Microsoft . Data . SqlClient ;
6+ using Microsoft . Data . Sqlite ;
7+ using Microsoft . EntityFrameworkCore ;
58using OpenTelemetry . Trace ;
69using System . Diagnostics ;
710
@@ -12,6 +15,15 @@ public interface IMeasureMappingManager
1215 Task < MeasureMapping > CreateAsync ( MeasureMapping newMeasureMapping , CancellationToken cancellationToken = default ) ;
1316 Task UpdateAsync ( string id , MeasureMapping measureMapping , CancellationToken cancellationToken = default ) ;
1417 Task DeleteAsync ( string id , CancellationToken cancellationToken = default ) ;
18+ Task DeleteAllAsync ( CancellationToken cancellationToken = default ) ;
19+ }
20+
21+ internal sealed class DuplicateMeasureMappingException : ApplicationException
22+ {
23+ public DuplicateMeasureMappingException ( Exception innerException )
24+ : base ( "A measure mapping with this measure and DQM already exists." , innerException )
25+ {
26+ }
1527 }
1628
1729 public class MeasureMappingManager : IMeasureMappingManager
@@ -38,16 +50,26 @@ public async Task<MeasureMapping> CreateAsync(MeasureMapping newMeasureMapping,
3850 {
3951 throw ;
4052 }
53+ catch ( DbUpdateException ex ) when ( IsUniqueIndexViolation ( ex ) )
54+ {
55+ Activity . Current ? . SetStatus ( ActivityStatusCode . Error ) ;
56+ Activity . Current ? . AddException ( ex ) ;
57+ throw new DuplicateMeasureMappingException ( ex ) ;
58+ }
4159 catch ( Exception ex )
4260 {
4361 Activity . Current ? . SetStatus ( ActivityStatusCode . Error ) ;
4462 Activity . Current ? . AddException ( ex ) ;
45- throw new ApplicationException ( "Measure mapping failed to create. " + ex . Message ) ;
63+ throw new ApplicationException ( "Measure mapping failed to create. " + ex . Message , ex ) ;
4664 }
4765
4866 return newMeasureMapping ;
4967 }
5068
69+ private static bool IsUniqueIndexViolation ( DbUpdateException exception ) =>
70+ exception . InnerException is SqliteException { SqliteExtendedErrorCode : 2067 }
71+ || exception . InnerException is SqlException { Number : 2601 or 2627 } ;
72+
5173 public async Task UpdateAsync ( string id , MeasureMapping measureMapping , CancellationToken cancellationToken = default )
5274 {
5375 using Activity ? activity = ServiceActivitySource . Instance . StartActivity ( "Update Measure Mapping" ) ;
@@ -59,7 +81,9 @@ public async Task UpdateAsync(string id, MeasureMapping measureMapping, Cancella
5981 throw new ApplicationException ( $ "Measure mapping with Id: { id } not found") ;
6082 }
6183
62- // TODO: Map `measureMapping` to `existing`
84+ existing . Measure = measureMapping . Measure ;
85+ existing . DQM = measureMapping . DQM ;
86+ existing . Frequency = measureMapping . Frequency ;
6387
6488 try
6589 {
@@ -70,6 +94,12 @@ public async Task UpdateAsync(string id, MeasureMapping measureMapping, Cancella
7094 {
7195 throw ;
7296 }
97+ catch ( DbUpdateException ex ) when ( IsUniqueIndexViolation ( ex ) )
98+ {
99+ Activity . Current ? . SetStatus ( ActivityStatusCode . Error ) ;
100+ Activity . Current ? . AddException ( ex ) ;
101+ throw new DuplicateMeasureMappingException ( ex ) ;
102+ }
73103 catch ( Exception ex )
74104 {
75105 Activity . Current ? . SetStatus ( ActivityStatusCode . Error ) ;
@@ -105,5 +135,30 @@ public async Task DeleteAsync(string id, CancellationToken cancellationToken = d
105135 throw new ApplicationException ( $ "Measure mapping { id } failed to delete. " + ex . Message ) ;
106136 }
107137 }
138+
139+ public async Task DeleteAllAsync ( CancellationToken cancellationToken = default )
140+ {
141+ using Activity ? activity = ServiceActivitySource . Instance . StartActivity ( "Delete All Measure Mappings" ) ;
142+
143+ try
144+ {
145+ var measureMappings = await _repository . GetAllAsync ( cancellationToken ) ;
146+ foreach ( var measureMapping in measureMappings )
147+ {
148+ _repository . Remove ( measureMapping ) ;
149+ }
150+ await _repository . SaveChangesAsync ( cancellationToken ) ;
151+ }
152+ catch ( OperationCanceledException )
153+ {
154+ throw ;
155+ }
156+ catch ( Exception ex )
157+ {
158+ Activity . Current ? . SetStatus ( ActivityStatusCode . Error ) ;
159+ Activity . Current ? . AddException ( ex ) ;
160+ throw new ApplicationException ( "Failed to delete all measure mappings. " + ex . Message ) ;
161+ }
162+ }
108163 }
109164}
0 commit comments