Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
nhathoang989 committed Dec 17, 2023
1 parent 2bcc51e commit d796f2e
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 123 deletions.
10 changes: 0 additions & 10 deletions src/applications/mixcore.gateway/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@
app.UseOutputCache();
app.MapDefaultEndpoints();
app.Run();
static void ConfigureServices(WebApplicationBuilder builder)
{
builder.Services.AddOutputCache();
builder.Services.AddControllers();
builder.Services.AddOcelot(builder.Configuration);
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
static void Configure(WebApplication app, IWebHostEnvironment env)
Expand Down
43 changes: 18 additions & 25 deletions src/applications/mixcore/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,21 @@

namespace Mixcore.Controllers
{
public class HomeController : MvcBaseController
public class HomeController(
IHttpContextAccessor httpContextAccessor,
IPSecurityConfigService ipSecurityConfigService,
IMixCmsService mixCmsService,
TranslatorService translator,
DatabaseService databaseService,
UnitOfWorkInfo<MixCmsContext> uow,
MixRepoDbRepository repoDbRepository,
IMixMetadataService metadataService,
MixCacheService cacheService,
IMixTenantService tenantService,
IConfiguration configuration) : MvcBaseController(httpContextAccessor, ipSecurityConfigService, mixCmsService, translator, databaseService, uow, cacheService, tenantService, configuration)
{
private readonly IMixMetadataService _metadataService;
private readonly MixRepoDbRepository _repoDbRepository;

public HomeController(
IHttpContextAccessor httpContextAccessor,
IPSecurityConfigService ipSecurityConfigService,
IMixCmsService mixCmsService,
TranslatorService translator,
DatabaseService databaseService,
UnitOfWorkInfo<MixCmsContext> uow,
MixRepoDbRepository repoDbRepository,
IMixMetadataService metadataService,
MixCacheService cacheService,
IMixTenantService tenantService,
IConfiguration configuration)
: base(httpContextAccessor, ipSecurityConfigService, mixCmsService, translator, databaseService, uow, cacheService, tenantService, configuration)
{
_repoDbRepository = repoDbRepository;
_metadataService = metadataService;
}
private readonly IMixMetadataService _metadataService = metadataService;
private readonly MixRepoDbRepository _repoDbRepository = repoDbRepository;

protected override void ValidateRequest()
{
Expand All @@ -54,10 +47,10 @@ protected override void ValidateRequest()
}
}

[Route("{seoName?}")]
[Route("")]
public async Task<IActionResult> Index()
{
string? seoName = Request.RouteValues["seoName"]?.ToString();
string seoName = Request.RouteValues["seoName"]?.ToString();
if (!IsValid)
{
return Redirect(RedirectUrl);
Expand All @@ -71,7 +64,7 @@ public async Task<IActionResult> Index()
}


private async Task<PageContentViewModel?> LoadPage(string? seoName = null)
private async Task<PageContentViewModel> LoadPage(string seoName = null)
{
var pageRepo = PageContentViewModel.GetRepository(Uow, CacheService);
Expression<Func<MixPageContent, bool>> predicate = p => p.MixTenantId == CurrentTenant.Id
Expand Down Expand Up @@ -101,7 +94,7 @@ public async Task<IActionResult> Index()
}
return page;
}
private async Task<IActionResult> LoadAlias(string? seoName)
private async Task<IActionResult> LoadAlias(string seoName = null)
{
var alias = await MixUrlAliasViewModel.GetRepository(Uow, CacheService).GetSingleAsync(m => m.MixTenantId == CurrentTenant.Id && m.Alias == seoName);
if (alias != null)
Expand Down
3 changes: 1 addition & 2 deletions src/applications/mixcore/Controllers/PageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<IActionResult> Index(int id, string keyword)
#endregion Routes

#region Helper
protected async Task<IActionResult> Page(int pageId, string? keyword = null)
protected async Task<IActionResult> Page(int pageId, string keyword = null)
{
// Home Page
var pageRepo = PageContentViewModel.GetRepository(Uow, _cacheService);
Expand All @@ -78,7 +78,6 @@ protected async Task<IActionResult> Page(int pageId, string? keyword = null)
{
SortBy = MixQueryColumnName.Priority
}, _cacheService);
page.Posts.Items.Take(2);


ViewData["Title"] = page.SeoTitle;
Expand Down
2 changes: 1 addition & 1 deletion src/applications/mixcore/Controllers/PostController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task<IActionResult> Index(int id, string seoName)
#endregion Routes

#region Helper
protected async Task<IActionResult> Post(int postId, string? seoName = null)
protected async Task<IActionResult> Post(int postId, string seoName = null)
{
// Home Post
var postRepo = PostContentViewModel.GetRepository(Uow, _cacheService);
Expand Down
55 changes: 25 additions & 30 deletions src/applications/mixcore/Controllers/SecurityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,33 @@

namespace Mixcore.Controllers
{
public class SecurityController : MixControllerBase
public class SecurityController(
IHttpContextAccessor httpContextAccessor,
IMixCmsService mixCmsService,
IPSecurityConfigService ipSecurityConfigService,
SignInManager<MixUser> signInManager,
ILogger<ExternalLoginModel> logger,
MixIdentityService idService,
TenantUserManager userManager,
MixEndpointService mixEndpointService,
IMixTenantService tenantService,
IConfiguration configuration) : MixControllerBase(httpContextAccessor, mixCmsService, ipSecurityConfigService, tenantService, configuration)
{
private readonly SignInManager<MixUser> _signInManager;
private readonly TenantUserManager _userManager;
private readonly ILogger<ExternalLoginModel> _logger;
private readonly MixIdentityService _idService;
private readonly MixEndpointService _mixEndpointService;

public SecurityController(
IHttpContextAccessor httpContextAccessor,
IMixCmsService mixCmsService,
IPSecurityConfigService ipSecurityConfigService,
SignInManager<MixUser> signInManager,
ILogger<ExternalLoginModel> logger,
MixIdentityService idService,
TenantUserManager userManager,
MixEndpointService mixEndpointService,
IMixTenantService tenantService,
IConfiguration configuration)
: base(httpContextAccessor, mixCmsService, ipSecurityConfigService, tenantService, configuration)
{
_signInManager = signInManager;
_logger = logger;
_idService = idService;
_userManager = userManager;
_mixEndpointService = mixEndpointService;
}
private readonly SignInManager<MixUser> _signInManager = signInManager;
private readonly TenantUserManager _userManager = userManager;
private readonly ILogger<ExternalLoginModel> _logger = logger;
private readonly MixIdentityService _idService = idService;
private readonly MixEndpointService _mixEndpointService = mixEndpointService;

[HttpGet]
[Route("security/{page}")]
public IActionResult Index(string page)
{
if (page is null)
{
throw new MixException(MixErrorStatus.Badrequest, nameof(page));
}

if (IsValid)
{
return View();
Expand Down Expand Up @@ -72,9 +67,9 @@ public ActionResult<JObject> ExternalLogin([FromForm] string returnUrl, [FromFor
[Route("security/external-login-result")]
[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<JObject>> ExternalLoginResultAsync(string? returnUrl = null, string? remoteError = null)
public async Task<ActionResult<JObject>> ExternalLoginResultAsync(string returnUrl = null, string remoteError = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
returnUrl ??= Url.Content("~/");
if (remoteError != null)
{
return RedirectToPage("./Login", new { ReturnUrl = returnUrl });
Expand All @@ -89,7 +84,7 @@ public async Task<ActionResult<JObject>> ExternalLoginResultAsync(string? return
var siginResult = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
if (siginResult.Succeeded)
{
string? email = info.Principal.FindFirstValue(ClaimTypes.Email);
string email = info.Principal.FindFirstValue(ClaimTypes.Email);
if (string.IsNullOrEmpty(email))
{
throw new MixException(MixErrorStatus.Badrequest, "Email not exist");
Expand All @@ -109,7 +104,7 @@ public async Task<ActionResult<JObject>> ExternalLoginResultAsync(string? return
else
{
// If the user does not have an account, then ask the user to create an account.
string? email = info.Principal.FindFirstValue(ClaimTypes.Email);
string email = info.Principal.FindFirstValue(ClaimTypes.Email);
if (string.IsNullOrEmpty(email))
{
throw new MixException(MixErrorStatus.Badrequest, "Email not exist");
Expand Down
38 changes: 15 additions & 23 deletions src/applications/mixcore/Domain/Bases/MvcBaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,21 @@
namespace Mixcore.Domain.Bases
{
[ResponseCache(CacheProfileName = "Default")]
public class MvcBaseController : MixControllerBase
public class MvcBaseController(
IHttpContextAccessor httpContextAccessor,
IPSecurityConfigService ipSecurityConfigService,
IMixCmsService mixCmsService,
TranslatorService translator,
DatabaseService databaseService,
UnitOfWorkInfo<MixCmsContext> uow,
MixCacheService cacheService,
IMixTenantService tenantService,
IConfiguration configuration) : MixControllerBase(httpContextAccessor, mixCmsService, ipSecurityConfigService, tenantService, configuration)
{
protected UnitOfWorkInfo<MixCmsContext> Uow;
protected readonly MixCacheService CacheService;
protected readonly TranslatorService Translator;
protected readonly DatabaseService DatabaseService;
public MvcBaseController(
IHttpContextAccessor httpContextAccessor,
IPSecurityConfigService ipSecurityConfigService,
IMixCmsService mixCmsService,
TranslatorService translator,
DatabaseService databaseService,
UnitOfWorkInfo<MixCmsContext> uow,
MixCacheService cacheService,
IMixTenantService tenantService,
IConfiguration configuration) :
base(httpContextAccessor, mixCmsService, ipSecurityConfigService, tenantService, configuration)
{
Translator = translator;
DatabaseService = databaseService;
Uow = uow;
CacheService = cacheService;
}
protected UnitOfWorkInfo<MixCmsContext> Uow = uow;
protected readonly MixCacheService CacheService = cacheService;
protected readonly TranslatorService Translator = translator;
protected readonly DatabaseService DatabaseService = databaseService;

protected override void ValidateRequest()
{
Expand All @@ -51,7 +43,7 @@ protected override void ValidateRequest()
}

#region Helper
protected async Task<IActionResult> Page(int pageId, string? keyword = null)
protected async Task<IActionResult> Page(int pageId, string keyword = null)
{
// Home Page
var pageRepo = PageContentViewModel.GetRepository(Uow, CacheService);
Expand Down
2 changes: 1 addition & 1 deletion src/applications/mixcore/Domain/Models/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public string Href

public List<MenuItem> MenuItems { get; set; }

public T? Property<T>(string fieldName)
public T Property<T>(string fieldName)
{
if (Obj != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ApplicationViewModel()

public ApplicationViewModel(MixApplication entity,

UnitOfWorkInfo? uowInfo = null) : base(entity, uowInfo)
UnitOfWorkInfo uowInfo = null) : base(entity, uowInfo)
{
}

Expand All @@ -35,7 +35,7 @@ public ApplicationViewModel(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkInfo
public string MixDatabaseName { get; set; }
public int? MixDbId { get; set; }

public JObject? ExtraData { get; set; }
public JObject ExtraData { get; set; }
public TemplateViewModel Template { get; set; }
#endregion

Expand All @@ -54,7 +54,7 @@ public override async Task ExpandView(CancellationToken cancellationToken = defa

#region Public Method

public T? Property<T>(string fieldName)
public T Property<T>(string fieldName)
{
return ExtraData != null
? ExtraData.Value<T>(fieldName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ModuleContentViewModel()
}

public ModuleContentViewModel(MixModuleContent entity,
UnitOfWorkInfo? uowInfo = null) : base(entity, uowInfo)
UnitOfWorkInfo uowInfo = null) : base(entity, uowInfo)
{
}

Expand All @@ -34,7 +34,7 @@ public ModuleContentViewModel(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkIn

public string DetailUrl => $"/Module/{Id}/{SeoName}";

public JObject? AdditionalData { get; set; }
public JObject AdditionalData { get; set; }
public PagingResponseModel<JObject> Data { get; set; }
public PagingResponseModel<ModulePostAssociationViewModel> Posts { get; set; }
#endregion
Expand All @@ -53,7 +53,7 @@ public override async Task ExpandView(CancellationToken cancellationToken = defa

#region Public Methods

public T? Property<T>(string fieldName)
public T Property<T>(string fieldName)
{
return AdditionalData != null
? AdditionalData.Value<T>(fieldName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public ModuleDataViewModel()

public ModuleDataViewModel(MixModuleData entity,

UnitOfWorkInfo? uowInfo = null) : base(entity, uowInfo)
UnitOfWorkInfo uowInfo = null) : base(entity, uowInfo)
{
}

Expand All @@ -33,7 +33,7 @@ public override Task ExpandView(CancellationToken cancellationToken = default)
{
if (!string.IsNullOrEmpty(Value) && Data == null)
{
Data = new JObject();
Data = [];
var tmp = JObject.Parse(Value);
foreach (var prop in tmp.Properties())
{
Expand All @@ -46,7 +46,7 @@ public override Task ExpandView(CancellationToken cancellationToken = default)
#endregion

#region Helper
public string? Property(string name)
public string Property(string name)
{
return Data.Property(name)?.Value.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ModulePostAssociationViewModel()

public ModulePostAssociationViewModel(MixModulePostAssociation entity,

UnitOfWorkInfo? uowInfo = null)
UnitOfWorkInfo uowInfo = null)
: base(entity, uowInfo)
{
}
Expand All @@ -29,7 +29,7 @@ public ModulePostAssociationViewModel(UnitOfWorkInfo unitOfWorkInfo) : base(unit
#region Overrides
public override async Task ExpandView(CancellationToken cancellationToken = default)
{
Post = await PostContentViewModel.GetRepository(UowInfo, CacheService).GetSingleAsync(ChildId);
Post = await PostContentViewModel.GetRepository(UowInfo, CacheService).GetSingleAsync(ChildId, cancellationToken);
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public PageContentViewModel()

public PageContentViewModel(MixPageContent entity,

UnitOfWorkInfo? uowInfo = null) : base(entity, uowInfo)
UnitOfWorkInfo uowInfo = null) : base(entity, uowInfo)
{
}

Expand All @@ -39,7 +39,7 @@ public PageContentViewModel(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkInfo

public List<ModuleContentViewModel> Modules { get; set; }
public PagingResponseModel<PagePostAssociationViewModel> Posts { get; set; }
public JObject? AdditionalData { get; set; }
public JObject AdditionalData { get; set; }
#endregion

#region Overrides
Expand All @@ -62,11 +62,11 @@ public async Task LoadDataAsync(MixRepoDbRepository mixRepoDbRepository,
}


public ModuleContentViewModel? GetModule(string moduleName)
public ModuleContentViewModel GetModule(string moduleName)
{
return Modules?.FirstOrDefault(m => m.SystemName == moduleName);
}
public T? Property<T>(string fieldName)
public T Property<T>(string fieldName)
{
return AdditionalData != null
? AdditionalData.Value<T>(fieldName)
Expand Down
Loading

0 comments on commit d796f2e

Please sign in to comment.