@@ -161,6 +161,13 @@ public ReportInstance Update(ReportInstance entity, bool updateChildren = false)
161161 var originalInstanceContent = originalInstanceContents . FirstOrDefault ( o => o . ContentId == ric . ContentId && o . SectionName == ric . SectionName ) ;
162162 if ( originalInstanceContent == null )
163163 {
164+ // Fetch the original content from the database to compare.
165+ originalContent = this . Context . Contents
166+ . AsNoTracking ( )
167+ . Include ( c => c . TonePoolsManyToMany )
168+ . FirstOrDefault ( c => c . Id == ric . ContentId ) ;
169+ var addContent = originalContent != null ;
170+
164171 // If the content is new too, upload it to the database.
165172 if ( ric . Content ? . Id == 0 )
166173 {
@@ -171,46 +178,51 @@ public ReportInstance Update(ReportInstance entity, bool updateChildren = false)
171178 }
172179 else if ( ric . Content != null )
173180 {
174- // Fetch the original content from the database to compare.
175- originalContent = this . Context . Contents
176- . AsNoTracking ( )
177- . Include ( c => c . TonePoolsManyToMany )
178- . FirstOrDefault ( c => c . Id == ric . ContentId ) ?? throw new InvalidOperationException ( $ "Content in report instance does not exist { entity . Id } :{ ric . ContentId } ") ;
179-
180- // Do not allow updating content not owned by the user.
181- // Content can be in more than one section. Only the first copy that is modified will be used for the update. All other copies will be synced.
182- if ( ! updatedContent . TryGetValue ( ric . ContentId , out Content ? reportContent ) &&
183- originalContent . IsPrivate &&
184- ( ric . Content . Headline != originalContent . Headline ||
185- ric . Content . Summary != originalContent . Summary ||
186- ric . Content . SourceId != originalContent . SourceId ||
187- ric . Content . OtherSource != originalContent . OtherSource ||
188- ric . Content . PublishedOn != originalContent . PublishedOn ||
189- ric . Content . Byline != originalContent . Byline ||
190- ric . Content . SourceUrl != originalContent . SourceUrl ||
191- ric . Content . Uid != originalContent . Uid ||
192- ric . Content . TonePoolsManyToMany . Any ( tp =>
193- {
194- var otp = originalContent . TonePoolsManyToMany . FirstOrDefault ( otp => otp . TonePoolId == tp . TonePoolId ) ;
195- return otp ? . Value != tp . Value ;
196- } ) ) )
181+ if ( originalContent == null )
197182 {
198- updatedContent . Add ( ric . ContentId , ric . Content ) ;
199- this . Context . Entry ( ric . Content ) . State = EntityState . Modified ;
183+ addContent = false ;
184+ this . Logger . LogWarning ( " Content in report instance does not exist {entityId}:{contentId}" , entity . Id , ric . ContentId ) ;
200185 }
201186 else
202187 {
203- // This content exists in another section and was updated so we need to make them the same content.
204- // If the client incorrectly submits the same content in more than one section and updates both of them differently, it will only use the first entry.
205- ric . Content = reportContent ;
188+ // Do not allow updating content not owned by the user.
189+ // Content can be in more than one section. Only the first copy that is modified will be used for the update. All other copies will be synced.
190+ if ( ! updatedContent . TryGetValue ( ric . ContentId , out Content ? reportContent ) &&
191+ originalContent . IsPrivate &&
192+ ( ric . Content . Headline != originalContent . Headline ||
193+ ric . Content . Summary != originalContent . Summary ||
194+ ric . Content . SourceId != originalContent . SourceId ||
195+ ric . Content . OtherSource != originalContent . OtherSource ||
196+ ric . Content . PublishedOn != originalContent . PublishedOn ||
197+ ric . Content . Byline != originalContent . Byline ||
198+ ric . Content . SourceUrl != originalContent . SourceUrl ||
199+ ric . Content . Uid != originalContent . Uid ||
200+ ric . Content . TonePoolsManyToMany . Any ( tp =>
201+ {
202+ var otp = originalContent . TonePoolsManyToMany . FirstOrDefault ( otp => otp . TonePoolId == tp . TonePoolId ) ;
203+ return otp ? . Value != tp . Value ;
204+ } ) ) )
205+ {
206+ updatedContent . Add ( ric . ContentId , ric . Content ) ;
207+ this . Context . Entry ( ric . Content ) . State = EntityState . Modified ;
208+ }
209+ else
210+ {
211+ // This content exists in another section and was updated so we need to make them the same content.
212+ // If the client incorrectly submits the same content in more than one section and updates both of them differently, it will only use the first entry.
213+ ric . Content = reportContent ;
214+ }
206215 }
207216 }
208217
209- // Add new content to the report.
210- ric . Instance = null ;
211- ric . InstanceId = entity . Id ;
212- this . Context . Entry ( ric ) . State = EntityState . Added ;
213- original . ContentManyToMany . Add ( ric ) ;
218+ if ( addContent )
219+ {
220+ // Add new content to the report.
221+ ric . Instance = null ;
222+ ric . InstanceId = entity . Id ;
223+ this . Context . Entry ( ric ) . State = EntityState . Added ;
224+ original . ContentManyToMany . Add ( ric ) ;
225+ }
214226 }
215227 else
216228 {
0 commit comments