99
1010namespace rubberduckvba . Server . Api . Features ;
1111
12+ public class MarkdownContentViewModel
13+ {
14+ public string Content { get ; init ; } = string . Empty ;
15+ }
16+
1217
1318[ AllowAnonymous ]
19+ [ EnableCors ( CorsPolicies . AllowAll ) ]
1420public class FeaturesController : RubberduckApiController
1521{
1622 private readonly CacheService cache ;
1723 private readonly IRubberduckDbService db ;
1824 private readonly FeatureServices features ;
1925 private readonly IRepository < TagAssetEntity > assetsRepository ;
2026 private readonly IRepository < TagEntity > tagsRepository ;
27+ private readonly IMarkdownFormattingService markdownService ;
2128
22- public FeaturesController ( CacheService cache , IRubberduckDbService db , FeatureServices features ,
29+ public FeaturesController ( CacheService cache , IRubberduckDbService db , FeatureServices features , IMarkdownFormattingService markdownService ,
2330 IRepository < TagAssetEntity > assetsRepository , IRepository < TagEntity > tagsRepository , ILogger < FeaturesController > logger )
2431 : base ( logger )
2532 {
@@ -28,6 +35,7 @@ public FeaturesController(CacheService cache, IRubberduckDbService db, FeatureSe
2835 this . features = features ;
2936 this . assetsRepository = assetsRepository ;
3037 this . tagsRepository = tagsRepository ;
38+ this . markdownService = markdownService ;
3139 }
3240
3341 private static RepositoryOptionViewModel [ ] RepositoryOptions { get ; } =
@@ -38,8 +46,6 @@ await db.GetTopLevelFeatures(repositoryId)
3846 . ContinueWith ( t => t . Result . Select ( e => new FeatureOptionViewModel { Id = e . Id , Name = e . Name , Title = e . Title } ) . ToArray ( ) ) ;
3947
4048 [ HttpGet ( "features" ) ]
41- [ EnableCors ( CorsPolicies . AllowAll ) ]
42- [ AllowAnonymous ]
4349 public IActionResult Index ( )
4450 {
4551 return GuardInternalAction ( ( ) =>
@@ -68,8 +74,6 @@ public IActionResult Index()
6874 }
6975
7076 [ HttpGet ( "features/{name}" ) ]
71- [ EnableCors ( CorsPolicies . AllowAll ) ]
72- [ AllowAnonymous ]
7377 public IActionResult Info ( [ FromRoute ] string name )
7478 {
7579 return GuardInternalAction ( ( ) =>
@@ -85,8 +89,6 @@ public IActionResult Info([FromRoute] string name)
8589 }
8690
8791 [ HttpGet ( "inspections/{name}" ) ]
88- [ EnableCors ( CorsPolicies . AllowAll ) ]
89- [ AllowAnonymous ]
9092 public IActionResult Inspection ( [ FromRoute ] string name )
9193 {
9294 return GuardInternalAction ( ( ) =>
@@ -107,8 +109,6 @@ public IActionResult Inspection([FromRoute] string name)
107109 }
108110
109111 [ HttpGet ( "annotations/{name}" ) ]
110- [ EnableCors ( CorsPolicies . AllowAll ) ]
111- [ AllowAnonymous ]
112112 public IActionResult Annotation ( [ FromRoute ] string name )
113113 {
114114 return GuardInternalAction ( ( ) =>
@@ -129,8 +129,6 @@ public IActionResult Annotation([FromRoute] string name)
129129 }
130130
131131 [ HttpGet ( "quickfixes/{name}" ) ]
132- [ EnableCors ( CorsPolicies . AllowAll ) ]
133- [ AllowAnonymous ]
134132 public IActionResult QuickFix ( [ FromRoute ] string name )
135133 {
136134 return GuardInternalAction ( ( ) =>
@@ -151,7 +149,6 @@ public IActionResult QuickFix([FromRoute] string name)
151149 }
152150
153151 [ HttpGet ( "features/create" ) ]
154- [ EnableCors ( CorsPolicies . AllowAuthenticated ) ]
155152 [ Authorize ( "github" ) ]
156153 public async Task < ActionResult < FeatureEditViewModel > > Create ( [ FromQuery ] RepositoryId repository = RepositoryId . Rubberduck , [ FromQuery ] int ? parentId = default )
157154 {
@@ -163,7 +160,6 @@ public async Task<ActionResult<FeatureEditViewModel>> Create([FromQuery] Reposit
163160 }
164161
165162 [ HttpPost ( "create" ) ]
166- [ EnableCors ( CorsPolicies . AllowAuthenticated ) ]
167163 [ Authorize ( "github" ) ]
168164 public async Task < ActionResult < FeatureEditViewModel > > Create ( [ FromBody ] FeatureEditViewModel model )
169165 {
@@ -179,14 +175,14 @@ public async Task<ActionResult<FeatureEditViewModel>> Create([FromBody] FeatureE
179175 }
180176
181177 var feature = model . ToFeature ( ) ;
182- var result = await db . SaveFeature ( feature ) ;
183178
179+ var result = await db . SaveFeature ( feature ) ;
184180 var features = await GetFeatureOptions ( model . RepositoryId ) ;
181+
185182 return Ok ( new FeatureEditViewModel ( result , features , RepositoryOptions ) ) ;
186183 }
187184
188185 [ HttpPost ( "features/update" ) ]
189- [ EnableCors ( CorsPolicies . AllowAuthenticated ) ]
190186 [ Authorize ( "github" ) ]
191187 public async Task < ActionResult < FeatureEditViewModel > > Update ( [ FromBody ] FeatureEditViewModel model )
192188 {
@@ -201,12 +197,25 @@ public async Task<ActionResult<FeatureEditViewModel>> Update([FromBody] FeatureE
201197 return BadRequest ( "Model is invalid for this endpoint." ) ;
202198 }
203199
204- var result = await db . SaveFeature ( model . ToFeature ( ) ) ;
200+ var feature = model . ToFeature ( ) ;
201+
202+ var result = await db . SaveFeature ( feature ) ;
205203 var features = await GetFeatureOptions ( model . RepositoryId ) ;
206204
207205 return new FeatureEditViewModel ( result , features , RepositoryOptions ) ;
208206 }
209207
208+ [ HttpPost ( "markdown/format" ) ]
209+ public MarkdownContentViewModel FormatMarkdown ( [ FromBody ] MarkdownContentViewModel model )
210+ {
211+ var markdown = model . Content ;
212+ var formatted = markdownService . FormatMarkdownDocument ( markdown , withSyntaxHighlighting : true ) ;
213+ return new MarkdownContentViewModel
214+ {
215+ Content = formatted
216+ } ;
217+ }
218+
210219 private InspectionsFeatureViewModel GetInspections ( )
211220 {
212221 InspectionsFeatureViewModel result ;
@@ -274,5 +283,4 @@ private FeatureViewModel GetFeature(string name)
274283
275284 return result ;
276285 }
277-
278- }
286+ }
0 commit comments