Skip to content

Commit

Permalink
fix bug mix app
Browse files Browse the repository at this point in the history
  • Loading branch information
nhathoang989 committed Oct 14, 2023
1 parent d16c090 commit f56c110
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/applications/Mixcore/Controllers/AppController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async Task<IActionResult> Index(string baseRoute)
{
if (IsValid)
{
return await App(baseRoute);
return await App($"/app/{baseRoute}");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="page-wrapper d-flex flex-column pt-4"><div class="container-xl"><div class="page-header d-print-none"><div class="row align-items-center"><div class="col"><div class="page-pretitle">Listing</div><h3 class="page-title">All Applications</h3></div></div></div></div><div class="page-body d-flex flex-row-reverse"><aside class="bd-sidebar sub-sidebar"><nav class="bd-links p-3" aria-label="Sub-section navigation"><div class="card card-body"><filter-list class="" request="request" callback="getList(pageIndex)" create-url="'/admin/mix-application/create'" create-text="'Create Tenant'"></filter-list></div></nav></aside><div class="content-body py-3 pe-3"><div class="card mb-5"><div class="card-body"><table id="list-page" class="table table-striped-x table-hover" dnd-list="data.items" dnd-disable-if="canDrag" cellspacing="0" ng-init="getList()"><thead class="thead-light"><tr><th scope="col" width="15px"><div class="form-check m-0 p-0"><input type="checkbox" class="form-check-inputx" ng-model="selectedList.isSelectAll" ng-change="selectAll(selectedList.isSelectAll)" id="customCheck_all"></div></th><th class="col">Display Name</th><th class="col">System Name</th><th class="col">Primary Domain</th><th class="col">Created</th><th class="col">Author</th><th class="col"></th><th class="col text-end"></th></tr></thead><tbody class=""><tr ng-repeat="item in data.items" dnd-draggable="item" dnd-effect-allowed="move" dnd-selected="selected = item" dnd-moved="updateInfos($index)" ng-class="{'selected': selected === item}" class="" sort-model="category" sort-model-id="{{item.id}}"><td class="align-middle"><div class="form-check"><input type="checkbox" class="form-check-input" id="customCheck_{{item.id}}" ng-model="item.isSelected" ng-change="select(item.id, item.isSelected)"> <label class="custom-control-label" for="customCheck_{{item.id}}"></label></div></td><td class=""><a href="" ng-click="goToPath('/admin/mix-application/details/' + item.id)" class="btn-link" ng-bind="item.displayName"></a></td><td><a href="//{{item.detailUrl}}" target="_blank" ng-bind="item.detailUrl"></a></td><td><a href="{{item.primaryDomain}}" target="_blank" ng-bind="item.primaryDomain"></a></td><td class="text-secondary"><small>{{item.createdDateTime | utcToLocal:'dd.MM.yy hh:mm a'}}</small></td><td class="text-secondary"><small>{{item.createdBy}}</small></td><td class="text-black-50"><small ng-if="item.status == 'Published'">🟢</small> <small ng-if="item.status == 'Schedule'">🟡</small> <small ng-if="item.status == 'Draft'">🔴</small> <small ng-if="item.status == 'Deleted'">⚫️</small></td><td><div class="dropdown float-lg-end ms-auto pe-1"><a href="javascript:;" class="cursor-pointer" id="dropdownTable2" data-bs-toggle="dropdown" aria-expanded="false"><i class="fa fa-ellipsis-h text-secondary" aria-hidden="true"></i></a><ul class="dropdown-menu px-2 py-3 ms-sm-n4 ms-n5" aria-labelledby="dropdownTable2" style=""><li><a href="" ng-click="goToPath('/admin/mix-application/details/' + item.id)" class="dropdown-item border-radius-md"><span class="fas fa-pen"></span> Edit</a></li><li><a href="{{item.detailUrl}}" target="_blank" class="dropdown-item border-radius-md"><span class="fas fa-eye"></span> Preview</a></li><li><a href="" ng-click="remove(item.id);" class="dropdown-item border-radius-md text-danger"><i class="fas fa-trash-alt"></i> Delete</a></li></ul></div></td></tr></tbody></table></div><div class="card-body"><paging class="small" page="data.pagingData.page" page-size="data.pagingData.pageSize" total="data.pagingData.total" ul-class="pagination justify-content-end m-0" a-class="page-link" paging-action="getList(page-1)" scroll-top="true"></paging></div></div></div></div></div>
<div class="page-wrapper d-flex flex-column pt-4"><div class="container-xl"><div class="page-header d-print-none"><div class="row align-items-center"><div class="col"><div class="page-pretitle">Listing</div><h3 class="page-title">All Applications</h3></div></div></div></div><div class="page-body d-flex flex-row-reverse"><aside class="bd-sidebar sub-sidebar"><nav class="bd-links p-3" aria-label="Sub-section navigation"><div class="card card-body"><filter-list class="" request="request" callback="getList(pageIndex)" create-url="'/admin/mix-application/create'" create-text="'Create Tenant'"></filter-list></div></nav></aside><div class="content-body py-3 pe-3"><div class="card mb-5"><div class="card-body"><table id="list-page" class="table table-striped-x table-hover" dnd-list="data.items" dnd-disable-if="canDrag" cellspacing="0" ng-init="getList()"><thead class="thead-light"><tr><th scope="col" width="15px"><div class="form-check m-0 p-0"><input type="checkbox" class="form-check-inputx" ng-model="selectedList.isSelectAll" ng-change="selectAll(selectedList.isSelectAll)" id="customCheck_all"></div></th><th class="col">Display Name</th><th class="col">System Name</th><th class="col">Primary Domain</th><th class="col">Created</th><th class="col">Author</th><th class="col"></th><th class="col text-end"></th></tr></thead><tbody class=""><tr ng-repeat="item in data.items" dnd-draggable="item" dnd-effect-allowed="move" dnd-selected="selected = item" dnd-moved="updateInfos($index)" ng-class="{'selected': selected === item}" class="" sort-model="category" sort-model-id="{{item.id}}"><td class="align-middle"><div class="form-check"><input type="checkbox" class="form-check-input" id="customCheck_{{item.id}}" ng-model="item.isSelected" ng-change="select(item.id, item.isSelected)"> <label class="custom-control-label" for="customCheck_{{item.id}}"></label></div></td><td class=""><a href="" ng-click="goToPath('/admin/mix-application/details/' + item.id)" class="btn-link" ng-bind="item.displayName"></a></td><td><a href="//{{item.detailUrl}}" target="_blank" ng-bind="item.detailUrl"></a></td><td><a href="{{item.baseRoute}}" target="_blank" ng-bind="item.baseRoute"></a></td><td class="text-secondary"><small>{{item.createdDateTime | utcToLocal:'dd.MM.yy hh:mm a'}}</small></td><td class="text-secondary"><small>{{item.createdBy}}</small></td><td class="text-black-50"><small ng-if="item.status == 'Published'">🟢</small> <small ng-if="item.status == 'Schedule'">🟡</small> <small ng-if="item.status == 'Draft'">🔴</small> <small ng-if="item.status == 'Deleted'">⚫️</small></td><td><div class="dropdown float-lg-end ms-auto pe-1"><a href="javascript:;" class="cursor-pointer" id="dropdownTable2" data-bs-toggle="dropdown" aria-expanded="false"><i class="fa fa-ellipsis-h text-secondary" aria-hidden="true"></i></a><ul class="dropdown-menu px-2 py-3 ms-sm-n4 ms-n5" aria-labelledby="dropdownTable2" style=""><li><a href="" ng-click="goToPath('/admin/mix-application/details/' + item.id)" class="dropdown-item border-radius-md"><span class="fas fa-pen"></span> Edit</a></li><li><a href="{{item.detailUrl}}" target="_blank" class="dropdown-item border-radius-md"><span class="fas fa-eye"></span> Preview</a></li><li><a href="" ng-click="remove(item.id);" class="dropdown-item border-radius-md text-danger"><i class="fas fa-trash-alt"></i> Delete</a></li></ul></div></td></tr></tbody></table></div><div class="card-body"><paging class="small" page="data.pagingData.page" page-size="data.pagingData.pageSize" total="data.pagingData.total" ul-class="pagination justify-content-end m-0" a-class="page-link" paging-action="getList(page-1)" scroll-top="true"></paging></div></div></div></div></div>
10 changes: 9 additions & 1 deletion src/modules/mix.portal/Controllers/MixApplicationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public async Task<ActionResult<MixApplicationViewModel>> Install([FromBody] MixA
await _applicationService.Install(app, cancellationToken);
return base.Ok(app);
}

[HttpPost]
[Route("restore-package")]
public async Task<ActionResult<MixApplicationViewModel>> Restore([FromBody] RestoreMixApplicationPackageDto dto, CancellationToken cancellationToken = default)
{
MixApplicationViewModel app = await _applicationService.RestorePackage(dto, cancellationToken);
return base.Ok(app);
}

#endregion

Expand All @@ -64,7 +72,7 @@ protected override async Task<PagingResponseModel<MixApplicationViewModel>> Sear
var result = await base.SearchHandler(req, cancellationToken);
foreach (var item in result.Items)
{
item.DetailUrl = $"{CurrentTenant.PrimaryDomain}/app/{item.BaseRoute}";
item.DetailUrl = $"{CurrentTenant.PrimaryDomain}{item.BaseRoute}";
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Mix.Portal.Domain.Dtos
{
public sealed class RestoreMixApplicationPackageDto
{
public int AppId { get; set; }
public string PackageFilePath { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface IMixApplicationService
public Task<MixApplicationViewModel> UpdatePackage(MixApplicationViewModel app, string pakageFilePath, CancellationToken cancellationToken = default);

public Task AlertAsync<T>(IClientProxy clients, string action, int status, T message);
Task<MixApplicationViewModel> RestorePackage(RestoreMixApplicationPackageDto dto, CancellationToken cancellationToken);
}
}
60 changes: 49 additions & 11 deletions src/modules/mix.portal/Domain/Services/MixApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Mix.Shared.Services;
using Mix.SignalR.Constants;
using Mix.SignalR.Hubs;
using System.IO.Packaging;
using System.Text.RegularExpressions;

namespace Mix.Portal.Domain.Services
Expand Down Expand Up @@ -45,7 +46,7 @@ public async Task<MixApplicationViewModel> Install(MixApplicationViewModel app,
string filePath = await DownloadPackage(name, app.PackageFilePath, appFolder);
MixFileHelper.UnZipFile(filePath, appFolder);

var template = await CreateTemplate(name, appFolder, app.BaseRoute);
var template = await SaveTemplate(app.TemplateId, name, appFolder, app.BaseRoute);
if (template != null)
{
app.SetUowInfo(_cmsUow, CacheService);
Expand All @@ -60,7 +61,7 @@ public async Task<MixApplicationViewModel> Install(MixApplicationViewModel app,
return null;
}

private async Task<MixTemplateViewModel> CreateTemplate(string name, string appFolder, string baseRoute)
private async Task<MixTemplateViewModel> SaveTemplate(int? templateId, string name, string appFolder, string baseRoute)
{
try
{
Expand All @@ -79,19 +80,20 @@ private async Task<MixTemplateViewModel> CreateTemplate(string name, string appF
.Replace("options['baseHref']", $"'{appFolder}'");

var activeTheme = await _themeService.GetActiveTheme();
MixTemplateViewModel template = new(_cmsUow)
MixTemplateViewModel template = await MixTemplateViewModel.GetRepository(_cmsUow, CacheService).GetSingleAsync(m => m.Id == templateId);
template ??= new(_cmsUow)
{
MixThemeId = activeTheme.Id,
FileName = name,
FileFolder = $"{MixFolders.TemplatesFolder}/{CurrentTenant.SystemName}/{activeTheme.SystemName}/{MixTemplateFolderType.Pages}",
FolderType = MixTemplateFolderType.Pages,
Extension = MixFileExtensions.CsHtml,
Content = indexFile.Content.Replace("@", "@@"),
MixTenantId = CurrentTenant.Id,
Scripts = string.Empty,
Styles = string.Empty,
CreatedBy = _mixIdentityService.GetClaim(HttpContextAccessor.HttpContext.User, MixClaims.Username)
};
template.Content = indexFile.Content.Replace("@", "@@");
await template.SaveAsync();
_queueService.PushQueue(CurrentTenant.Id, MixQueueTopics.MixViewModelChanged, MixRestAction.Post.ToString(), template);
MixFileHelper.SaveFile(indexFile);
Expand Down Expand Up @@ -122,10 +124,11 @@ private async Task<string> DownloadPackage(string name, string packageUrl, strin
}
};
var cancellationToken = new CancellationToken();

string filePath = $"{appFolder}/{name}-{DateTime.UtcNow.ToString("dd-MM-yyyy-hh:mm:ss")}{MixFileExtensions.Zip}";
await _httpService.DownloadAsync(packageUrl, appFolder, name, MixFileExtensions.Zip, progress, cancellationToken);


string fileName = $"{name}-{DateTime.UtcNow.ToString("dd-MM-yyyy-hh-mm-ss")}";
string filePath = $"{appFolder}/{fileName}{MixFileExtensions.Zip}";
await _httpService.DownloadAsync(packageUrl, appFolder, fileName, MixFileExtensions.Zip, progress, cancellationToken);

return filePath;
}
catch (Exception ex)
Expand All @@ -144,11 +147,11 @@ public async Task<MixApplicationViewModel> UpdatePackage(MixApplicationViewModel
}

var packages = app.AppSettings.Value<JArray>("packages") ?? new();

string appFolder = $"{MixFolders.StaticFiles}/{MixFolders.MixApplications}/{app.DisplayName}";
string package = await DownloadPackage(app.DisplayName, app.PackageFilePath, appFolder);
MixFileHelper.UnZipFile(package, appFolder);

var template = await SaveTemplate(app.TemplateId, app.DisplayName, appFolder, app.BaseRoute);
packages.Add(package);
app.AppSettings["activePackage"] = package;
app.AppSettings["packages"] = packages;
Expand All @@ -158,7 +161,40 @@ public async Task<MixApplicationViewModel> UpdatePackage(MixApplicationViewModel
{
throw;
}
catch(Exception ex)
catch (Exception ex)
{
throw new MixException(MixErrorStatus.Badrequest, ex);
}
}

public async Task<MixApplicationViewModel> RestorePackage(RestoreMixApplicationPackageDto dto, CancellationToken cancellationToken)
{
try
{
var app = await MixApplicationViewModel.GetRepository(_cmsUow, CacheService).GetSingleAsync(m => m.Id == dto.AppId);
if (app == null)
{
throw new MixException(MixErrorStatus.NotFound, "App Not Found");
}
if (!File.Exists(dto.PackageFilePath))
{
throw new MixException(MixErrorStatus.NotFound, $"Package {dto.PackageFilePath} Not Found");
}

string appFolder = $"{MixFolders.StaticFiles}/{MixFolders.MixApplications}/{app.DisplayName}";
MixFileHelper.UnZipFile(dto.PackageFilePath, appFolder);
var template = await SaveTemplate(app.TemplateId, app.DisplayName, appFolder, app.BaseRoute);
app.AppSettings["activePackage"] = dto.PackageFilePath;

await app.SaveAsync(cancellationToken);

return app;
}
catch (MixException)
{
throw;
}
catch (Exception ex)
{
throw new MixException(MixErrorStatus.Badrequest, ex);
}
Expand Down Expand Up @@ -190,6 +226,8 @@ public async Task AlertAsync<T>(IClientProxy clients, string action, int status,
await clients.SendAsync(
HubMethods.ReceiveMethod, logMsg.ToString(Formatting.None));
}


#endregion
}
}

0 comments on commit f56c110

Please sign in to comment.