11using System ;
22using System . Collections . Generic ;
3+ using System . Drawing ;
34using System . IO ;
45using System . Linq ;
56using System . Net ;
@@ -27,20 +28,20 @@ public OperationDataResult<List<Template_Dto>> Index()
2728 {
2829 try
2930 {
30- Core core = _coreHelper . GetCore ( ) ;
31+ var core = _coreHelper . GetCore ( ) ;
3132 var templatesDto = core . TemplateItemReadAll ( false ) ;
3233 return new OperationDataResult < List < Template_Dto > > ( true , templatesDto ) ;
3334 }
3435 catch ( Exception ex )
3536 {
3637 if ( ex . Message . Contains ( "PrimeDb" ) )
3738 {
38- string [ ] lines =
39+ var lines =
3940 System . IO . File . ReadAllLines (
4041 System . Web . Hosting . HostingEnvironment . MapPath ( "~/bin/Input.txt" ) ) ;
4142
42- string connectionStr = lines . First ( ) ;
43- AdminTools adminTool = new AdminTools ( connectionStr ) ;
43+ var connectionStr = lines . First ( ) ;
44+ var adminTool = new AdminTools ( connectionStr ) ;
4445 adminTool . DbSettingsReloadRemote ( ) ;
4546 return new OperationDataResult < List < Template_Dto > > ( false , "Check connection string" ) ;
4647 }
@@ -50,7 +51,7 @@ public OperationDataResult<List<Template_Dto>> Index()
5051 {
5152 try
5253 {
53- Core core = _coreHelper . GetCore ( ) ;
54+ var core = _coreHelper . GetCore ( ) ;
5455 }
5556 catch ( Exception ex2 )
5657 {
@@ -62,7 +63,55 @@ public OperationDataResult<List<Template_Dto>> Index()
6263 return new OperationDataResult < List < Template_Dto > > ( false , "Check settings before proceed" ) ;
6364 }
6465 }
65- catch ( Exception exception )
66+ catch ( Exception )
67+ {
68+ throw new HttpResponseException ( HttpStatusCode . Unauthorized ) ;
69+ }
70+ }
71+
72+ [ HttpGet ]
73+ public OperationDataResult < Template_Dto > Get ( int id )
74+ {
75+ try
76+ {
77+ try
78+ {
79+ var core = _coreHelper . GetCore ( ) ;
80+ var templateDto = core . TemplateItemRead ( id ) ;
81+ return new OperationDataResult < Template_Dto > ( true , templateDto ) ;
82+ }
83+ catch ( Exception ex )
84+ {
85+ if ( ex . Message . Contains ( "PrimeDb" ) )
86+ {
87+ var lines =
88+ System . IO . File . ReadAllLines (
89+ System . Web . Hosting . HostingEnvironment . MapPath ( "~/bin/Input.txt" ) ) ;
90+
91+ var connectionStr = lines . First ( ) ;
92+ var adminTool = new AdminTools ( connectionStr ) ;
93+ adminTool . DbSettingsReloadRemote ( ) ;
94+ return new OperationDataResult < Template_Dto > ( false , "Check connection string" ) ;
95+ }
96+ else
97+ {
98+ if ( ex . InnerException . Message . Contains ( "Cannot open database" ) )
99+ {
100+ try
101+ {
102+ var core = _coreHelper . GetCore ( ) ;
103+ }
104+ catch ( Exception ex2 )
105+ {
106+ return new OperationDataResult < Template_Dto > ( false , "Core is not started." ) ;
107+ }
108+ return new OperationDataResult < Template_Dto > ( false , "Check settings before proceed" ) ;
109+ }
110+ }
111+ return new OperationDataResult < Template_Dto > ( false , "Check settings before proceed" ) ;
112+ }
113+ }
114+ catch ( Exception )
66115 {
67116 throw new HttpResponseException ( HttpStatusCode . Unauthorized ) ;
68117 }
@@ -72,12 +121,12 @@ public OperationDataResult<List<Template_Dto>> Index()
72121 [ HttpPost ]
73122 public OperationResult Create ( EFormXmlModel eFormXmlModel )
74123 {
75- Core core = _coreHelper . GetCore ( ) ;
124+ var core = _coreHelper . GetCore ( ) ;
76125
77126 var newTemplate = core . TemplateFromXml ( eFormXmlModel . EFormXml ) ;
78127
79128 newTemplate = core . TemplateUploadData ( newTemplate ) ;
80- List < string > errors = core . TemplateValidation ( newTemplate ) ;
129+ var errors = core . TemplateValidation ( newTemplate ) ;
81130
82131 if ( ! errors . Any ( ) )
83132 {
@@ -87,19 +136,25 @@ public OperationResult Create(EFormXmlModel eFormXmlModel)
87136 return new OperationResult ( true , $ "eForm \" { newTemplate . Label } \" created successfully") ;
88137 }
89138
90- string message = errors . Aggregate ( "" , ( current , str ) => current + ( "<br>" + str ) ) ;
139+ var message = errors . Aggregate ( "" , ( current , str ) => current + ( "<br>" + str ) ) ;
91140
92141 return new OperationResult ( false , "eForm could not be created!" + message ) ;
93142 }
94143
95144 [ HttpGet ]
96145 public OperationResult Delete ( int id )
97146 {
98- Core core = _coreHelper . GetCore ( ) ;
147+ var core = _coreHelper . GetCore ( ) ;
99148 var templateDto = core . TemplateItemRead ( id ) ;
149+ foreach ( var siteUId in templateDto . DeployedSites )
150+ {
151+ core . CaseDelete ( templateDto . Id , siteUId . SiteUId ) ;
152+ }
153+ var result = core . TemplateDelete ( id ) ;
154+
100155 try
101156 {
102- return core . TemplateDelete ( id )
157+ return result
103158 ? new OperationResult ( true , $ "eForm \" { templateDto . Label } \" deleted successfully")
104159 : new OperationResult ( false , $ "eForm \" { templateDto . Label } \" could not be deleted!") ;
105160 }
@@ -112,7 +167,7 @@ public OperationResult Delete(int id)
112167 [ HttpPost ]
113168 public OperationDataResult < DeployToModel > DeployTo ( int id )
114169 {
115- Core core = _coreHelper . GetCore ( ) ;
170+ var core = _coreHelper . GetCore ( ) ;
116171 var templateDto = core . TemplateItemRead ( id ) ;
117172 var siteNamesDto = core . Advanced_SiteItemReadAll ( ) ;
118173
@@ -127,20 +182,20 @@ public OperationDataResult<DeployToModel> DeployTo(int id)
127182 [ HttpPost ]
128183 public OperationResult Deploy ( DeployModel deployModel )
129184 {
130- List < int > deployedSiteIds = new List < int > ( ) ;
185+ var deployedSiteIds = new List < int > ( ) ;
131186
132- List < int > sitesToBeRetractedFrom = new List < int > ( ) ;
133- List < int > sitesToBeDeployedTo = new List < int > ( ) ;
187+ var sitesToBeRetractedFrom = new List < int > ( ) ;
188+ var sitesToBeDeployedTo = new List < int > ( ) ;
134189
135- Core core = _coreHelper . GetCore ( ) ;
190+ var core = _coreHelper . GetCore ( ) ;
136191 var templateDto = core . TemplateItemRead ( deployModel . Id ) ;
137192
138193 foreach ( var site in templateDto . DeployedSites )
139194 {
140195 deployedSiteIds . Add ( site . SiteUId ) ;
141196 }
142197
143- List < int > requestedSiteIds = deployModel
198+ var requestedSiteIds = deployModel
144199 . DeployCheckboxes
145200 . Select ( deployCheckbox => deployCheckbox . Id )
146201 . ToList ( ) ;
@@ -183,22 +238,22 @@ public OperationResult Deploy(DeployModel deployModel)
183238 core . CaseDelete ( deployModel . Id , siteUId ) ;
184239 }
185240
186- return new OperationResult ( true , $ "eForm \" { templateDto . Label } \" deployed successfully! ") ;
241+ return new OperationResult ( true , $ "\" { templateDto . Label } \" paired successfully. ") ;
187242 }
188243
189244 [ HttpGet ]
190245 public HttpResponseMessage Csv ( int id )
191246 {
192- Core core = _coreHelper . GetCore ( ) ;
247+ var core = _coreHelper . GetCore ( ) ;
193248
194- string fileName = $ "{ id } _{ DateTime . Now . Ticks } .csv";
249+ var fileName = $ "{ id } _{ DateTime . Now . Ticks } .csv";
195250 System . IO . Directory . CreateDirectory ( System . Web . Hosting . HostingEnvironment . MapPath ( "~/bin/output/" ) ) ;
196- string filePath = System . Web . Hosting . HostingEnvironment . MapPath ( $ "~/bin/output/{ fileName } ") ;
251+ var filePath = System . Web . Hosting . HostingEnvironment . MapPath ( $ "~/bin/output/{ fileName } ") ;
197252 var fullPath = core . CasesToCsv ( id , null , null , filePath ,
198- $ "{ Request . RequestUri . Scheme } ://{ Request . RequestUri . DnsSafeHost } " +
199- "/api/templates/getimage?&filename= " ) ;
253+ $ "{ Request . RequestUri . Scheme } ://{ Request . RequestUri . Authority } { Url . Content ( "~" ) } " +
254+ "output/dataFolder/ " ) ;
200255
201- HttpResponseMessage result = new HttpResponseMessage ( HttpStatusCode . OK ) ;
256+ var result = new HttpResponseMessage ( HttpStatusCode . OK ) ;
202257 var fileStream = new FileStream ( fullPath , FileMode . Open , FileAccess . Read ) ;
203258
204259 result . Content = new StreamContent ( fileStream ) ;
@@ -211,24 +266,85 @@ public HttpResponseMessage Csv(int id)
211266
212267 [ HttpGet ]
213268 [ Authorize ]
214- public HttpResponseMessage GetImage ( string fileName )
269+ public HttpResponseMessage GetImage ( string fileName , string noCache = "noCache" )
215270 {
216- string filePath = HttpContext . Current . Server . MapPath ( $ "~/output/datafolder/picture/{ fileName } ") ;
271+ var filePath = HttpContext . Current . Server . MapPath ( $ "~/output/datafolder/picture/{ fileName } ") ;
217272 if ( ! File . Exists ( filePath ) )
218273 {
219274 return new HttpResponseMessage ( HttpStatusCode . NotFound ) ;
220275 }
221276 var extention = Path . GetExtension ( filePath ) . Replace ( "." , "" ) ;
222277
223- HttpResponseMessage result = new HttpResponseMessage ( HttpStatusCode . OK ) ;
278+ var result = new HttpResponseMessage ( HttpStatusCode . OK ) ;
224279 var fileStream = new FileStream ( filePath , FileMode . Open , FileAccess . Read ) ;
225280
226281 result . Content = new StreamContent ( fileStream ) ;
227- result . Content . Headers . ContentDisposition = new ContentDispositionHeaderValue ( "attachment" ) ;
228- result . Content . Headers . ContentDisposition . FileName = fileName ;
282+ result . Content . Headers . ContentDisposition =
283+ new ContentDispositionHeaderValue ( "attachment" ) { FileName = fileName } ;
229284 result . Content . Headers . ContentType =
230285 new MediaTypeHeaderValue ( $ "image/{ extention } ") ;
231286 return result ;
232287 }
288+
289+ [ HttpGet ]
290+ [ Authorize ]
291+ public OperationResult RotateImage ( string fileName )
292+ {
293+ var filePath = HttpContext . Current . Server . MapPath ( $ "~/output/datafolder/picture/{ fileName } ") ;
294+ if ( ! File . Exists ( filePath ) )
295+ {
296+ return new OperationResult ( false , "File not found" ) ;
297+ }
298+
299+ var img = Image . FromFile ( filePath ) ;
300+ img . RotateFlip ( RotateFlipType . Rotate90FlipNone ) ;
301+ img . Save ( filePath ) ;
302+ img . Dispose ( ) ;
303+
304+ return new OperationResult ( true , "Image rotated successfully." ) ;
305+ }
306+
307+ [ HttpGet ]
308+ [ Authorize ]
309+ public OperationResult DeleteImage ( string fileName , int fieldId , int uploadedObjId )
310+ {
311+ try
312+ {
313+ var core = _coreHelper . GetCore ( ) ;
314+ if ( ! core . Advanced_DeleteUploadedData ( fieldId , uploadedObjId ) )
315+ {
316+ return new OperationResult ( false , "Error: Image was not deleted" ) ;
317+ }
318+ }
319+
320+ catch ( Exception e )
321+ {
322+ Console . WriteLine ( e ) ;
323+ return new OperationResult ( false , "Error" ) ;
324+ }
325+ return new OperationResult ( true , "Image deleted successfully." ) ;
326+ }
327+
328+
329+ [ HttpGet ]
330+ [ Authorize ]
331+ public HttpResponseMessage GetPdfFile ( string fileName )
332+ {
333+ var filePath = HttpContext . Current . Server . MapPath ( $ "~/output/datafolder/pdf/{ fileName } .pdf") ;
334+ if ( ! File . Exists ( filePath ) )
335+ {
336+ return new HttpResponseMessage ( HttpStatusCode . NotFound ) ;
337+ }
338+
339+ var result = new HttpResponseMessage ( HttpStatusCode . OK ) ;
340+ var fileStream = new FileStream ( filePath , FileMode . Open , FileAccess . Read ) ;
341+
342+ result . Content = new StreamContent ( fileStream ) ;
343+ result . Content . Headers . ContentDisposition =
344+ new ContentDispositionHeaderValue ( "attachment" ) { FileName = fileName } ;
345+ result . Content . Headers . ContentType =
346+ new MediaTypeHeaderValue ( $ "application/pdf") ;
347+ return result ;
348+ }
233349 }
234350}
0 commit comments