Skip to content

Commit

Permalink
improve code style
Browse files Browse the repository at this point in the history
  • Loading branch information
siegrainwong committed Jun 21, 2019
1 parent fa534ca commit e5cbf14
Show file tree
Hide file tree
Showing 23 changed files with 54 additions and 153 deletions.
10 changes: 2 additions & 8 deletions Ancorazor.API.Common/UrlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@

namespace Ancorazor.API.Common
{
public class UrlHelper
public static class UrlHelper
{
private readonly SEOConfiguration _seoConfiguration;
public UrlHelper(IOptions<SEOConfiguration> seoConfiguration)
{
_seoConfiguration = seoConfiguration.Value;
}

public static string ToUrlSafeString(string source, bool convertToPinyin = true)
{
var str = convertToPinyin ? CHNToPinyin.ConvertToPinYin(source) : source;
Expand All @@ -29,7 +23,7 @@ public static string UrlStringEncode(string source)
return WebUtility.HtmlEncode(source.Replace(" ", "-"));
}

public string GetArticleRoutePath(int id, DateTime date, string alias, string category, string template)
public static string GetArticleRoutePath(int id, DateTime date, string alias, string category, string template)
{
category = category ?? Constants.Constants.Article.DefaultCategoryName;
var path = template
Expand Down
4 changes: 2 additions & 2 deletions Ancorazor.API/Authentication/SGCookieAuthenticationEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public override async Task RedirectToLogin(RedirectContext<CookieAuthenticationO
{
context.Response.Headers.Remove("Location");
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
await Task.CompletedTask;
await Task.CompletedTask.ConfigureAwait(false);
}

public override async Task RedirectToAccessDenied(RedirectContext<CookieAuthenticationOptions> context)
{
context.Response.Headers.Remove("Location");
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
await Task.CompletedTask;
await Task.CompletedTask.ConfigureAwait(false);
}

public override Task RedirectToLogout(RedirectContext<CookieAuthenticationOptions> context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ export class SGFadeAnimation extends SGAnimation {
* === Transition Commands ===
*/

/**
* 打个ClassName的标记进去
*/
export function ClassName(name: string): ClassDecorator {
return function(target: any) {
Object.defineProperty(target.prototype, "className", {
value: () => name
});
};
}

/** 过渡指令 */
@ClassName("SGTransitionCommands")
export class SGTransitionCommands {
Expand Down Expand Up @@ -123,11 +134,3 @@ export class SGCustomizeTransitionCommands extends SGTransitionCommands {
Object.assign(this, obj);
}
}

export function ClassName(name: string): ClassDecorator {
return function(target: any) {
Object.defineProperty(target.prototype, "className", {
value: () => name
});
};
}
36 changes: 17 additions & 19 deletions Ancorazor.API/ClientApp/src/app/shared/utils/pbkdf2.cryptography.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
function stringToArrayBuffer(byteString: string) {
var byteArray = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
byteArray[i] = byteString.codePointAt(i);
}
return byteArray;
}

function arrayBufferToString(buffer: ArrayBuffer) {
var byteArray = new Uint8Array(buffer);
var byteString = "";
for (var i = 0; i < byteArray.byteLength; i++) {
byteString += String.fromCodePoint(byteArray[i]);
}
return byteString;
}

/**
* MARK: Angular PBKDF2
* https://stackoverflow.com/questions/40459020/angular-js-cryptography-pbkdf2-and-iteration/40468218#40468218
Expand Down Expand Up @@ -47,22 +64,3 @@ export function deriveAKey(
});
});
}

//Utility functions

function stringToArrayBuffer(byteString: string) {
var byteArray = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
byteArray[i] = byteString.codePointAt(i);
}
return byteArray;
}

function arrayBufferToString(buffer: ArrayBuffer) {
var byteArray = new Uint8Array(buffer);
var byteString = "";
for (var i = 0; i < byteArray.byteLength; i++) {
byteString += String.fromCodePoint(byteArray[i]);
}
return byteString;
}
86 changes: 0 additions & 86 deletions Ancorazor.API/ClientApp/src/app/shared/utils/time-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,3 @@ export function timeFormat(time: Date) {
return `${time.getFullYear()}-${time.getMonth() +
1}-${time.getDate()} ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
}

export function pastStringForTime(time: Date) {
function zeroize(num: number) {
return (String(num).length == 1 ? "0" : "") + num;
}

let timestamp = time.valueOf() / 1000;

let curTimestamp = new Date().getTime() / 1000; //当前时间戳
let timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数

let curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
let tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象

let Y = tmDate.getFullYear(),
m = tmDate.getMonth() + 1,
d = tmDate.getDate();
let H = tmDate.getHours(),
i = tmDate.getMinutes(),
s = tmDate.getSeconds();

if (timestampDiff < 60) {
// 一分钟以内
return "刚刚";
} else if (timestampDiff < 3600) {
// 一小时前之内
return Math.floor(timestampDiff / 60) + "分钟前";
} else if (
curDate.getFullYear() == Y &&
curDate.getMonth() + 1 == m &&
curDate.getDate() == d
) {
return zeroize(H) + ":" + zeroize(i);
} else {
let newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
if (
newDate.getFullYear() == Y &&
newDate.getMonth() + 1 == m &&
newDate.getDate() == d
) {
return zeroize(H) + ":" + zeroize(i);
} else if (curDate.getFullYear() == Y) {
return zeroize(H) + ":" + zeroize(i);
} else {
return zeroize(H) + ":" + zeroize(i);
}
}
}

export function pastStringForDate(date: Date) {
function zeroize(num: number) {
return (String(num).length == 1 ? "0" : "") + num;
}

let timestamp = date.valueOf() / 1000;

let curTimestamp = new Date().getTime() / 1000; //当前时间戳

let curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
let tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象

let Y = tmDate.getFullYear(),
m = tmDate.getMonth() + 1,
d = tmDate.getDate();

if (
curDate.getFullYear() == Y &&
curDate.getMonth() + 1 == m &&
curDate.getDate() == d
) {
return "今天";
} else {
let newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
if (
newDate.getFullYear() == Y &&
newDate.getMonth() + 1 == m &&
newDate.getDate() == d
) {
return "昨天";
} else if (curDate.getFullYear() == Y) {
return zeroize(m) + "." + zeroize(d);
} else {
return Y + "\n" + zeroize(m) + "." + zeroize(d);
}
}
}
2 changes: 1 addition & 1 deletion Ancorazor.API/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task<IActionResult> SignOut()
public IActionResult GetXSRFToken()
{
var tokens = _antiforgery.GetAndStoreTokens(HttpContext);
Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions() { HttpOnly = false });
Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions { HttpOnly = false });
return Ok();
}

Expand Down
5 changes: 2 additions & 3 deletions Ancorazor.API/Filters/GlobalValidateModelFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ namespace Ancorazor.API.Filters
{
public class GlobalValidateModelFilter : IActionFilter
{
private readonly IHostingEnvironment _env;
private readonly ILogger<GlobalValidateModelFilter> _logger;

public GlobalValidateModelFilter(IHostingEnvironment env, ILogger<GlobalValidateModelFilter> logger)
public GlobalValidateModelFilter(ILogger<GlobalValidateModelFilter> logger)
{
_env = env;
_logger = logger;
}

public void OnActionExecuted(ActionExecutedContext context)
{
// do nothing
}

public void OnActionExecuting(ActionExecutingContext context)
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.API/Services/SpaPrerenderingServiceLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class SpaPrerenderingServiceLocator
{
public static Action<HttpContext, IDictionary<string, object>> GetProcessor(IApplicationBuilder app)
{
return (HttpContext httpContext, IDictionary<string, object> supplyData) =>
return (httpContext, supplyData) =>
{
var service = app.ApplicationServices.CreateScope().ServiceProvider.GetService<ISpaPrerenderingService>();
service.Process(httpContext, supplyData);
Expand Down
1 change: 0 additions & 1 deletion Ancorazor.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ private void RegisterCaching(IServiceCollection services)

private void RegisterHelper(IServiceCollection services)
{
services.AddSingleton<UrlHelper>();
services.AddSingleton<IFileSystem>(new LocalDiskFileSystem(Path.Combine(_hostingEnvironment.ContentRootPath, "Upload")));
services.AddScoped<ISpaPrerenderingService, SpaPrerenderingService>();
}
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.Entity/Article.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Ancorazor.Entity
{
public partial class Article: BaseEntity
public class Article: BaseEntity
{
public Article()
{
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.Entity/ArticleTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Ancorazor.Entity
{
public partial class ArticleTags: BaseEntity
public class ArticleTags: BaseEntity
{
public int Article { get; set; }
public int Tag { get; set; }
Expand Down
6 changes: 1 addition & 5 deletions Ancorazor.Entity/BlogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Ancorazor.Entity
{
public partial class BlogContext : DbContext
public class BlogContext : DbContext
{
public BlogContext() { }

Expand Down Expand Up @@ -497,10 +497,6 @@ 然后直接把 markdown 粘贴进来即可。
#endregion

#endregion

OnModelCreatingPartial(builder);
}

partial void OnModelCreatingPartial(ModelBuilder builder);
}
}
2 changes: 1 addition & 1 deletion Ancorazor.Entity/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Ancorazor.Entity
{
public partial class Category: BaseEntity
public class Category: BaseEntity
{
public Category()
{
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.Entity/Migrations/BlogContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Ancorazor.Entity.Migrations
{
[DbContext(typeof(BlogContext))]
partial class BlogContextModelSnapshot : ModelSnapshot
class BlogContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.Entity/OperationLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Ancorazor.Entity
{
public partial class OperationLog: BaseEntity
public class OperationLog: BaseEntity
{
public int? UserId { get; set; }
[StringLength(200)]
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.Entity/Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Ancorazor.Entity
{
public partial class Role: BaseEntity
public class Role: BaseEntity
{
public Role()
{
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.Entity/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Ancorazor.Entity
{
public partial class Tag: BaseEntity
public class Tag: BaseEntity
{
public Tag()
{
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.Entity/UserRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Ancorazor.Entity
{
public partial class UserRole: BaseEntity
public class UserRole: BaseEntity
{
public int UserId { get; set; }
public int RoleId { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.Entity/Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Ancorazor.Entity
{
public partial class Users: BaseEntity
public class Users: BaseEntity
{
public Users()
{
Expand Down
2 changes: 1 addition & 1 deletion Ancorazor.Messages/QueryParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Ancorazor.API.Messages
{
public class QueryParameter
{
public bool IsDeleted { get; set; } = false;
public bool IsDeleted { get; set; }
}
}
6 changes: 2 additions & 4 deletions Ancorazor.Service/ArticleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ namespace Ancorazor.Service
{
public partial class ArticleService
{
private readonly UrlHelper _urlHelper;
private readonly BlogContext _context;
private readonly IMapper _mapper;
private readonly SiteSettingService _settingService;

public ArticleService(UrlHelper urlHelper, BlogContext context, IMapper mapper, SiteSettingService settingService)
public ArticleService(BlogContext context, IMapper mapper, SiteSettingService settingService)
{
_urlHelper = urlHelper;
_context = context;
_mapper = mapper;
_settingService = settingService;
Expand Down Expand Up @@ -202,7 +200,7 @@ private string GetArticleRoutePath(ArticleViewModel viewModel)
{
if (viewModel == null) return null;
var setting = _settingService.GetSetting();
return _urlHelper.GetArticleRoutePath(viewModel.Id, viewModel.CreatedAt, viewModel.Alias, viewModel.CategoryAlias, setting.RouteMapping);
return UrlHelper.GetArticleRoutePath(viewModel.Id, viewModel.CreatedAt, viewModel.Alias, viewModel.CategoryAlias, setting.RouteMapping);
}
}
}
2 changes: 1 addition & 1 deletion Siegrain.Common/CHNToPinyin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Siegrain.Common
{
public class CHNToPinyin
public static class CHNToPinyin
{
/// <summary>
/// 将字符串转换成拼音
Expand Down
Loading

0 comments on commit e5cbf14

Please sign in to comment.