Skip to content

Commit

Permalink
Add FE BE Info ViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
nhathoang989 committed Dec 28, 2017
1 parent 1e378e7 commit be6f573
Show file tree
Hide file tree
Showing 16 changed files with 2,209 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Swastika.Cms.Lib/Readme.txt
Expand Up @@ -15,7 +15,7 @@ dotnet add package System.IdentityModel.Tokens.Jwt

Add-Migration AddFirstName -Context ApplicationDbContext

Update-database -Context ApplicationDbContext
Update-database -Context SiocCmsContext

Scaffold-DbContext "Server=115.77.190.113,4444;Database=Stag_swastika_io;UID=sa;Pwd=sqlP@ssw0rd;MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force

Expand Down
104 changes: 104 additions & 0 deletions src/Swastika.Cms.Lib/TagHelpers/ActiveMenuTagHelper.cs
@@ -0,0 +1,104 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Swastika.Web.Start.TagHelpers
{
[HtmlTargetElement(Attributes = "is-active-menu")]
public class ActiveMenuTagHelper : TagHelper
{
private IDictionary<string, string> _routeValues;

/// <summary>The name of the controller.</summary>
/// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
[HtmlAttributeName("asp-controllers")]
public string Controllers { get; set; }
[HtmlAttributeName("asp-action")]
public string Actions { get; set; }
[HtmlAttributeName("asp-route-pagenames")]
public string PageNames { get; set; }

[HtmlAttributeName("asp-route-id")]
public string Id { get; set; }

/// <summary>
/// Gets or sets the <see cref="T:Microsoft.AspNetCore.Mvc.Rendering.ViewContext" /> for the current request.
/// </summary>
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
public IDictionary<string, string> RouteValues { get => _routeValues; set => _routeValues = value; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);

if (ShouldBeActive())
{
MakeActive(output);
}

output.Attributes.RemoveAll("is-active-menu");
}

private bool ShouldBeActive()
{
bool rtn = false;
string currentController = ViewContext.RouteData.Values["Controller"].ToString();

string currentAction = ViewContext.RouteData.Values["Action"].ToString();
string id = ViewContext.RouteData.Values["Id"] != null ? ViewContext.RouteData.Values["Id"].ToString() : string.Empty;
string currentPagename = ViewContext.RouteData.Values["pageName"] != null ? ViewContext.RouteData.Values["pageName"].ToString() : string.Empty;

List<string> activeControllers = string.IsNullOrEmpty(Controllers) ? new List<string>() : Controllers.ToLower().Split(',').ToList();
List<string> activeActions = string.IsNullOrEmpty(Actions) ? new List<string>() : Actions.ToLower().Split(',').ToList();
List<string> activePageNames = string.IsNullOrEmpty(PageNames) ? new List<string>() : PageNames.ToLower().Split(',').ToList();
if (!string.IsNullOrWhiteSpace(Controllers))
{
rtn = (string.IsNullOrEmpty(Actions) && string.IsNullOrEmpty(PageNames) && activeControllers.Contains(currentController.ToLower()))
|| (
activeControllers.Contains(currentController.ToLower()) // Current Controller
&& activeActions.Contains(currentAction.ToLower()) // Current Action
&& (string.IsNullOrEmpty(Id) || id==Id) // Current Details
&& (string.IsNullOrEmpty(PageNames) || activePageNames.Contains(currentPagename.ToLower()))
);
}

//if (!string.IsNullOrWhiteSpace(Controllers))
//{
// foreach (string controller in Controllers.Split(','))
// {
// if (currentController.ToLower() == controller.ToLower())
// {
// rtn = true;
// break;
// }
// }

//}

return rtn;
}

private void MakeActive(TagHelperOutput output)
{
var classAttr = output.Attributes.FirstOrDefault(a => a.Name == "class");
if (classAttr == null)
{
classAttr = new TagHelperAttribute("class", "active");
output.Attributes.Add(classAttr);
}
else if (classAttr.Value == null || classAttr.Value.ToString().IndexOf("active") < 0)
{
output.Attributes.SetAttribute("class", classAttr.Value == null
? "active"
: classAttr.Value.ToString() + " active");
}
}
}
}
105 changes: 105 additions & 0 deletions src/Swastika.Cms.Lib/TagHelpers/ActiveRouteTagHelper.cs
@@ -0,0 +1,105 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Swastika.Cms.Mvc.TagHelpers
{
[HtmlTargetElement(Attributes = "is-active-route")]
public class ActiveRouteTagHelper : TagHelper
{
private IDictionary<string, string> _routeValues;

/// <summary>The name of the action method.</summary>
/// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
[HtmlAttributeName("asp-action")]
public string Action { get; set; }

/// <summary>The name of the controller.</summary>
/// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
[HtmlAttributeName("asp-controller")]
public string Controller { get; set; }

/// <summary>Additional parameters for the route.</summary>
[HtmlAttributeName("asp-all-route-data", DictionaryAttributePrefix = "asp-route-")]
public IDictionary<string, string> RouteValues
{
get
{
if (this._routeValues == null)
this._routeValues = (IDictionary<string, string>)new Dictionary<string, string>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
return this._routeValues;
}
set
{
this._routeValues = value;
}
}

/// <summary>
/// Gets or sets the <see cref="T:Microsoft.AspNetCore.Mvc.Rendering.ViewContext" /> for the current request.
/// </summary>
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);

if (ShouldBeActive())
{
MakeActive(output);
}

output.Attributes.RemoveAll("is-active-route");
}

private bool ShouldBeActive()
{
string currentController = ViewContext.RouteData.Values["Controller"].ToString();
string currentAction = ViewContext.RouteData.Values["Action"].ToString();

if (!string.IsNullOrWhiteSpace(Controller) && Controller.ToLower() != currentController.ToLower())
{
return false;
}

if (!string.IsNullOrWhiteSpace(Action) && Action.ToLower() != currentAction.ToLower())
{
return false;
}

foreach (KeyValuePair<string, string> routeValue in RouteValues)
{
if (!ViewContext.RouteData.Values.ContainsKey(routeValue.Key) ||
ViewContext.RouteData.Values[routeValue.Key].ToString() != routeValue.Value)
{
return false;
}
}

return true;
}

private void MakeActive(TagHelperOutput output)
{
var classAttr = output.Attributes.FirstOrDefault(a => a.Name == "class");
if (classAttr == null)
{
classAttr = new TagHelperAttribute("class", "active");
output.Attributes.Add(classAttr);
}
else if (classAttr.Value == null || classAttr.Value.ToString().IndexOf("active") < 0)
{
output.Attributes.SetAttribute("class", classAttr.Value == null
? "active"
: classAttr.Value.ToString() + " active");
}
}
}
}
117 changes: 117 additions & 0 deletions src/Swastika.Cms.Lib/ViewModels/BackEnd/BEArticleModuleViewModel.cs
@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Swastika.Cms.Lib.Models;
using Swastika.Domain.Data.ViewModels;
using Microsoft.EntityFrameworkCore.Storage;
using Newtonsoft.Json;
using Swastika.IO.Common.Helper;
using Swastika.IO.Domain.Core.ViewModels;
using Swastika.Cms.Lib.ViewModels.Info;
using System.Threading.Tasks;
using Swastika.Cms.Lib.ViewModels.FrontEnd;

namespace Swastika.Cms.Lib.ViewModels.BackEnd
{
public class BEArticleModuleViewModel
: ViewModelBase<SiocCmsContext, SiocArticleModule, BEArticleModuleViewModel>
{
#region Properties

#region Models
[JsonProperty("articleId")]
public string ArticleId { get; set; }
[JsonProperty("moduleId")]
public int ModuleId { get; set; }
[JsonProperty("isActived")]
public bool IsActived { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
#endregion

#region Views
[JsonProperty("module")]
public FEModuleViewModel Module { get; set; }
#endregion

#endregion

#region Contructors

public BEArticleModuleViewModel() : base()
{
}

public BEArticleModuleViewModel(SiocArticleModule model, SiocCmsContext _context = null, IDbContextTransaction _transaction = null) : base(model, _context, _transaction)
{
}

#endregion

#region Overrides
public override void ExpandView(SiocCmsContext _context = null, IDbContextTransaction _transaction = null)
{
var getModuleResult = FEModuleViewModel.Repository.GetSingleModel(m => m.Id == ModuleId && m.Specificulture == Specificulture);
if (getModuleResult.IsSucceed)
{
this.Module = getModuleResult.Data;
}
}
public override RepositoryResponse<bool> RemoveRelatedModels(BEArticleModuleViewModel view, SiocCmsContext _context = null, IDbContextTransaction _transaction = null)
{
return InfoModuleDataViewModel.Repository.RemoveListModel(d => d.ArticleId == view.ArticleId
&& d.ModuleId == view.ModuleId && d.Specificulture == view.Specificulture, _context, _transaction);
}



public override RepositoryResponse<bool> SaveSubModels(SiocArticleModule parent, SiocCmsContext _context = null, IDbContextTransaction _transaction = null)
{
RepositoryResponse<bool> result = new RepositoryResponse<bool>() { IsSucceed = true };
foreach (var data in Module.Data.Items)
{
data.ArticleId = parent.ArticleId;
data.ModuleId = parent.ModuleId;
var saveResult = data.SaveModel(false, _context, _transaction);
if (!saveResult.IsSucceed)
{
result.Errors.AddRange(saveResult.Errors);
result.Exception = saveResult.Exception;
}
result.Data = result.IsSucceed = result.IsSucceed && saveResult.IsSucceed;
}
return result;
}

#region Async
public override Task<RepositoryResponse<bool>> RemoveRelatedModelsAsync(BEArticleModuleViewModel view, SiocCmsContext _context = null, IDbContextTransaction _transaction = null)
{
return InfoModuleDataViewModel.Repository.RemoveListModelAsync(d => d.ArticleId == view.ArticleId
&& d.ModuleId == view.ModuleId && d.Specificulture == view.Specificulture, _context, _transaction);
}
public override async Task<RepositoryResponse<bool>> SaveSubModelsAsync(SiocArticleModule parent, SiocCmsContext _context = null, IDbContextTransaction _transaction = null)
{
RepositoryResponse<bool> result = new RepositoryResponse<bool>() { IsSucceed = true };
foreach (var data in Module.Data.Items)
{
data.ArticleId = parent.ArticleId;
data.ModuleId = parent.ModuleId;
var saveResult = await data.SaveModelAsync(false, _context, _transaction);
if (!saveResult.IsSucceed)
{
result.Errors.AddRange(saveResult.Errors);
result.Exception = saveResult.Exception;
}
result.Data = result.IsSucceed = result.IsSucceed && saveResult.IsSucceed;
}
return result;
}
#endregion
#endregion

#region Expands

#endregion

}
}

0 comments on commit be6f573

Please sign in to comment.