66using rubberduckvba . Server . Model . Entity ;
77using rubberduckvba . Server . Services ;
88using rubberduckvba . Server . Services . rubberduckdb ;
9+ using System . Security . Principal ;
910
1011namespace rubberduckvba . Server . Api . Features ;
1112
@@ -21,17 +22,19 @@ public class FeaturesController : RubberduckApiController
2122{
2223 private readonly CacheService cache ;
2324 private readonly IRubberduckDbService db ;
25+ private readonly IAuditService admin ;
2426 private readonly FeatureServices features ;
2527 private readonly IRepository < TagAssetEntity > assetsRepository ;
2628 private readonly IRepository < TagEntity > tagsRepository ;
2729 private readonly IMarkdownFormattingService markdownService ;
2830
29- public FeaturesController ( CacheService cache , IRubberduckDbService db , FeatureServices features , IMarkdownFormattingService markdownService ,
31+ public FeaturesController ( CacheService cache , IRubberduckDbService db , IAuditService admin , FeatureServices features , IMarkdownFormattingService markdownService ,
3032 IRepository < TagAssetEntity > assetsRepository , IRepository < TagEntity > tagsRepository , ILogger < FeaturesController > logger )
3133 : base ( logger )
3234 {
3335 this . cache = cache ;
3436 this . db = db ;
37+ this . admin = admin ;
3538 this . features = features ;
3639 this . assetsRepository = assetsRepository ;
3740 this . tagsRepository = tagsRepository ;
@@ -175,11 +178,15 @@ public async Task<ActionResult<FeatureEditViewModel>> Create([FromBody] FeatureE
175178 }
176179
177180 var feature = model . ToFeature ( ) ;
178-
179- var result = await db . SaveFeature ( feature ) ;
180- var features = await GetFeatureOptions ( model . RepositoryId ) ;
181-
182- return Ok ( new FeatureEditViewModel ( result , features , RepositoryOptions ) ) ;
181+ if ( User . Identity is IIdentity identity )
182+ {
183+ await admin . CreateFeature ( feature , identity ) ;
184+ return Ok ( feature ) ;
185+ }
186+ else
187+ {
188+ return Unauthorized ( "User identity is not available." ) ;
189+ }
183190 }
184191
185192 [ HttpPost ( "features/update" ) ]
@@ -198,11 +205,15 @@ public async Task<ActionResult<FeatureEditViewModel>> Update([FromBody] FeatureE
198205 }
199206
200207 var feature = model . ToFeature ( ) ;
201-
202- var result = await db . SaveFeature ( feature ) ;
203- var features = await GetFeatureOptions ( model . RepositoryId ) ;
204-
205- return new FeatureEditViewModel ( result , features , RepositoryOptions ) ;
208+ if ( User . Identity is IIdentity identity )
209+ {
210+ await admin . UpdateFeature ( feature , identity ) ;
211+ return Ok ( feature ) ;
212+ }
213+ else
214+ {
215+ return Unauthorized ( "User identity is not available." ) ;
216+ }
206217 }
207218
208219 [ HttpPost ( "features/delete" ) ]
@@ -213,13 +224,20 @@ public async Task Delete([FromBody] IFeature model)
213224 {
214225 throw new ArgumentException ( "Model is invalid for this endpoint." ) ;
215226 }
216- var existingId = await db . GetFeatureId ( RepositoryId . Rubberduck , model . Name ) ;
217- if ( existingId is null )
227+ var existing = await db . ResolveFeature ( RepositoryId . Rubberduck , model . Name ) ;
228+ if ( existing is null )
218229 {
219230 throw new ArgumentException ( "Model is invalid for this endpoint." ) ;
220231 }
221232
222- await db . DeleteFeature ( existingId . Value ) ;
233+ if ( User . Identity is IIdentity identity )
234+ {
235+ await admin . DeleteFeature ( existing , identity ) ;
236+ }
237+ else
238+ {
239+ throw new UnauthorizedAccessException ( "User identity is not available." ) ;
240+ }
223241 }
224242
225243 [ HttpPost ( "markdown/format" ) ]
0 commit comments