88using System . Net . Http . Headers ;
99using System . Web ;
1010using System . Web . Http ;
11+ using Amazon . Runtime ;
1112using eFormAPI . Common . API ;
1213using eFormAPI . Common . Models ;
1314using eFormAPI . Common . Models . Templates ;
@@ -21,7 +22,7 @@ namespace eFormAPI.Web.Controllers
2122 public class TemplatesController : ApiController
2223 {
2324 private readonly EFormCoreHelper _coreHelper = new EFormCoreHelper ( ) ;
24-
25+
2526 [ HttpPost ]
2627 public OperationDataResult < TemplateListModel > Index ( TemplateRequestModel templateRequestModel )
2728 {
@@ -30,7 +31,12 @@ public OperationDataResult<TemplateListModel> Index(TemplateRequestModel templat
3031 try
3132 {
3233 var core = _coreHelper . GetCore ( ) ;
33- var templatesDto = core . TemplateItemReadAll ( false ) ;
34+ var templatesDto = core . TemplateItemReadAll ( false ,
35+ "" ,
36+ templateRequestModel . NameFilter ,
37+ templateRequestModel . IsSortDsc ,
38+ templateRequestModel . Sort ,
39+ templateRequestModel . TagIds ) ;
3440
3541 var model = new TemplateListModel
3642 {
@@ -47,7 +53,7 @@ public OperationDataResult<TemplateListModel> Index(TemplateRequestModel templat
4753 if ( ex . Message . Contains ( "PrimeDb" ) )
4854 {
4955 var lines = File . ReadAllLines (
50- System . Web . Hosting . HostingEnvironment . MapPath ( "~/bin/Input.txt" ) ) ;
56+ System . Web . Hosting . HostingEnvironment . MapPath ( "~/bin/Input.txt" ) ) ;
5157
5258 var connectionStr = lines . First ( ) ;
5359 var adminTool = new AdminTools ( connectionStr ) ;
@@ -128,24 +134,35 @@ public OperationDataResult<Template_Dto> Get(int id)
128134 [ HttpPost ]
129135 public OperationResult Create ( EFormXmlModel eFormXmlModel )
130136 {
131- var core = _coreHelper . GetCore ( ) ;
132-
133- var newTemplate = core . TemplateFromXml ( eFormXmlModel . EFormXml ) ;
134-
135- newTemplate = core . TemplateUploadData ( newTemplate ) ;
136- var errors = core . TemplateValidation ( newTemplate ) ;
137-
138- if ( ! errors . Any ( ) )
137+ try
139138 {
140- if ( newTemplate == null ) return new OperationResult ( false , "eForm could not be created!" ) ;
141-
139+ var core = _coreHelper . GetCore ( ) ;
140+ // Create tags
141+ var tagList = eFormXmlModel . NewTag . Replace ( " " , "" ) . Split ( ',' ) ;
142+ foreach ( var tag in tagList )
143+ {
144+ eFormXmlModel . TagIds . Add ( core . TagCreate ( tag ) ) ;
145+ }
146+ // Create eform
147+ var newTemplate = core . TemplateFromXml ( eFormXmlModel . EFormXml ) ;
148+ newTemplate = core . TemplateUploadData ( newTemplate ) ;
149+ // Check errors
150+ var errors = core . TemplateValidation ( newTemplate ) ;
151+ if ( errors . Any ( ) )
152+ {
153+ var message = errors . Aggregate ( "" , ( current , str ) => current + ( "<br>" + str ) ) ;
154+ throw new Exception ( "eForm could not be created!" + message ) ;
155+ }
156+ if ( newTemplate == null ) throw new Exception ( "eForm could not be created!" ) ;
157+ // Set tags to eform
142158 core . TemplateCreate ( newTemplate ) ;
159+ core . TemplateSetTags ( newTemplate . Id , eFormXmlModel . TagIds ) ;
143160 return new OperationResult ( true , $ "eForm \" { newTemplate . Label } \" created successfully") ;
144161 }
145-
146- var message = errors . Aggregate ( "" , ( current , str ) => current + ( "<br>" + str ) ) ;
147-
148- return new OperationResult ( false , "eForm could not be created!" + message ) ;
162+ catch ( Exception e )
163+ {
164+ return new OperationResult ( false , e . Message ) ;
165+ }
149166 }
150167
151168 [ HttpGet ]
@@ -158,7 +175,7 @@ public OperationResult Delete(int id)
158175 core . CaseDelete ( templateDto . Id , siteUId . SiteUId ) ;
159176 }
160177 var result = core . TemplateDelete ( id ) ;
161-
178+
162179 try
163180 {
164181 return result
@@ -300,7 +317,7 @@ public OperationResult RotateImage(string fileName)
300317 {
301318 return new OperationResult ( false , "File not found" ) ;
302319 }
303-
320+
304321 var img = Image . FromFile ( filePath ) ;
305322 img . RotateFlip ( RotateFlipType . Rotate90FlipNone ) ;
306323 img . Save ( filePath ) ;
@@ -351,7 +368,7 @@ public HttpResponseMessage GetPdfFile(string fileName)
351368 new MediaTypeHeaderValue ( "application/pdf" ) ;
352369 return result ;
353370 }
354-
371+
355372 [ HttpGet ]
356373 [ Authorize ]
357374 [ Route ( "api/templates/download-eform-pdf/{templateId}" ) ]
@@ -380,15 +397,13 @@ public HttpResponseMessage DownloadEFormPDF(int templateId)
380397 {
381398 return new HttpResponseMessage ( HttpStatusCode . InternalServerError ) ;
382399 }
383-
384400 }
385-
401+
386402 [ HttpGet ]
387403 [ Authorize ]
388404 [ Route ( "api/templates/download-eform-xml/{templateId}" ) ]
389405 public HttpResponseMessage DownloadEFormXML ( int templateId )
390406 {
391-
392407 try
393408 {
394409 var core = _coreHelper . GetCore ( ) ;
0 commit comments