public async Task <IActionResult> GenerateCode([FromBody] GenerateCodeRequestModel model) { _logger.Info("Generate code with data: {@request}", model); var result = await _backupService.CreateCode(model); return(File(Encoding.UTF8.GetBytes(result.Content), "application/text", result.FileName)); }
public async Task <GenerateCodeResponseModel> CreateCode(GenerateCodeRequestModel model) { var collectApp = _appServiceProvider.GetAppsByIds(model.Apps); var collectStandards = _standardServiceProvider.GetStandardComponentsByIds(model.Standards); var collectTree = _standardServiceProvider.GetStandardComponentsByIds(model.Tree); var collectArray = _standardServiceProvider.GetStandardComponentsByIds(model.Array); var collectCharts = _chartServiceProvider.GetChartsByIds(model.Charts); var collectDynamicLists = _dynamicListServiceProvider.GetDynamicListsByIds(model.DynamicLists); var collectDatabases = _databaseServiceProvider.GetDatabaseConnectionsByIds(model.Databases); var collectPages = _pageServiceProvider.GetPagesByIds(model.Pages); var collectCompositeControls = _compositeControlServiceProvider.GetByIds(model.CompositeControls); await Task.WhenAll( collectApp, collectStandards, collectCharts, collectDatabases, collectDynamicLists, collectPages, collectCompositeControls); var appCodes = new List <CodeGenerableResult>(); if (collectApp.Result != null && collectApp.Result.Any()) { foreach (var app in collectApp.Result) { try { appCodes.Add(app.GenerateCode()); } catch (Exception ex) { _logger.Error(ex, $"Cannot generate code for app {app.Name}"); } } } var standardCodes = new List <CodeGenerableResult>(); if (collectStandards.Result != null && collectStandards.Result.Any()) { foreach (var standard in collectStandards.Result) { try { standardCodes.Add(standard.GenerateCode()); } catch (Exception ex) { _logger.Error(ex, $"Cannot generate code for standard component {standard.Name}"); } } } var treeCodes = new List <CodeGenerableResult>(); if (collectTree.Result != null && collectTree.Result.Any()) { foreach (var tree in collectTree.Result) { try { treeCodes.Add(tree.GenerateCode()); } catch (Exception ex) { _logger.Error(ex, $"Cannot generate code for tree {tree.Name}"); } } } var arrayCodes = new List <CodeGenerableResult>(); if (collectArray.Result != null && collectArray.Result.Any()) { foreach (var array in collectArray.Result) { try { arrayCodes.Add(array.GenerateCode()); } catch (Exception ex) { _logger.Error(ex, $"Cannot generate code for array standard {array.Name}"); } } } var databaseCodes = new List <CodeGenerableResult>(); if (collectDatabases.Result != null && collectDatabases.Result.Any()) { foreach (var database in collectDatabases.Result) { try { databaseCodes.Add(database.GenerateCode()); } catch (Exception ex) { _logger.Error(ex, $"Cannot generate code for database {database.Name}"); } } } var chartCodes = new List <CodeGenerableResult>(); if (collectCharts.Result != null && collectCharts.Result.Any()) { foreach (var chart in collectCharts.Result) { try { chartCodes.Add(chart.GenerateCode()); } catch (Exception ex) { _logger.Error(ex, $"Cannot generate code for chart {chart.Name}"); } } } var pageCodes = new List <CodeGenerableResult>(); if (collectPages.Result != null && collectPages.Result.Any()) { foreach (var page in collectPages.Result) { try { pageCodes.Add(page.GenerateCode()); } catch (Exception ex) { _logger.Error(ex, $"Cannot generate code for page {page.Name}"); } } } var dynamicListCodes = new List <CodeGenerableResult>(); if (collectDynamicLists.Result != null && collectDynamicLists.Result.Any()) { foreach (var dynamicList in collectDynamicLists.Result) { try { dynamicListCodes.Add(dynamicList.GenerateCode()); } catch (Exception ex) { _logger.Error(ex, $"Cannot generate code for page {dynamicList.Name}"); } } } var compositeControlCodes = new List <CodeGenerableResult>(); if (collectCompositeControls.Result != null && collectCompositeControls.Result.Any()) { foreach (var control in collectCompositeControls.Result) { try { compositeControlCodes.Add(control.GenerateCode()); } catch (Exception ex) { _logger.Error(ex, $"Cannot generate code for composite code {control.Name}"); } } } var response = new GenerateCodeResponseModel(); var stringBuilder = new StringBuilder(); _ = stringBuilder.AppendLine("using System;"); _ = stringBuilder.AppendLine("using System.Threading.Tasks;"); _ = stringBuilder.AppendLine("using System.Collections.Generic;"); _ = stringBuilder.AppendLine("namespace CustomVersion"); _ = stringBuilder.AppendLine("{"); _ = stringBuilder.AppendLine($" public class {model.FileName} : LetPortal.Portal.IPortalVersion"); _ = stringBuilder.AppendLine($" {{"); _ = stringBuilder.AppendLine($" public string VersionNumber => \"{model.VersionNumber}\";"); _ = stringBuilder.AppendLine($" public Task Downgrade(LetPortal.Core.Versions.IVersionContext versionContext)"); _ = stringBuilder.AppendLine($" {{"); foreach (var appCode in appCodes) { _ = stringBuilder.AppendLine(appCode.DeletingCode); } foreach (var databaseCode in databaseCodes) { _ = stringBuilder.AppendLine(databaseCode.DeletingCode); } foreach (var standardCode in standardCodes) { _ = stringBuilder.AppendLine(standardCode.DeletingCode); } foreach (var treeCode in treeCodes) { _ = stringBuilder.AppendLine(treeCode.DeletingCode); } foreach (var arrayCode in arrayCodes) { _ = stringBuilder.AppendLine(arrayCode.DeletingCode); } foreach (var dynamicListCode in dynamicListCodes) { _ = stringBuilder.AppendLine(dynamicListCode.DeletingCode); } foreach (var chartCode in chartCodes) { _ = stringBuilder.AppendLine(chartCode.DeletingCode); } foreach (var pageCode in pageCodes) { _ = stringBuilder.AppendLine(pageCode.DeletingCode); } foreach (var controlCode in compositeControlCodes) { _ = stringBuilder.AppendLine(controlCode.DeletingCode); } _ = stringBuilder.AppendLine($" return System.Threading.Tasks.Task.CompletedTask;"); _ = stringBuilder.AppendLine($" }}"); _ = stringBuilder.AppendLine($" public Task Upgrade(LetPortal.Core.Versions.IVersionContext versionContext)"); _ = stringBuilder.AppendLine($" {{"); foreach (var appCode in appCodes) { _ = stringBuilder.AppendLine(appCode.InsertingCode); } foreach (var databaseCode in databaseCodes) { _ = stringBuilder.AppendLine(databaseCode.InsertingCode); } foreach (var standardCode in standardCodes) { _ = stringBuilder.AppendLine(standardCode.InsertingCode); } foreach (var treeCode in treeCodes) { _ = stringBuilder.AppendLine(treeCode.InsertingCode); } foreach (var arrayCode in arrayCodes) { _ = stringBuilder.AppendLine(arrayCode.InsertingCode); } foreach (var dynamicListCode in dynamicListCodes) { _ = stringBuilder.AppendLine(dynamicListCode.InsertingCode); } foreach (var chartCode in chartCodes) { _ = stringBuilder.AppendLine(chartCode.InsertingCode); } foreach (var pageCode in pageCodes) { _ = stringBuilder.AppendLine(pageCode.InsertingCode); } foreach (var controlCode in compositeControlCodes) { _ = stringBuilder.AppendLine(controlCode.InsertingCode); } _ = stringBuilder.AppendLine($" return System.Threading.Tasks.Task.CompletedTask;"); _ = stringBuilder.AppendLine($" }}"); _ = stringBuilder.AppendLine($" }}"); _ = stringBuilder.AppendLine("}"); response.Content = stringBuilder.ToString(); response.FileName = model.FileName + ".cs"; return(response); }