diff options
author | crupest <crupest@outlook.com> | 2019-04-21 22:39:42 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-04-21 22:39:42 +0800 |
commit | edac531b7c0bfc40bc9b6f5c6f1abe71f71cd9e0 (patch) | |
tree | 02aceff5fe6a540a1f4b0d4f7253f75cafeb160a /Timeline | |
parent | a9f248ad817683e911348cd168c570db3d07757f (diff) | |
download | timeline-edac531b7c0bfc40bc9b6f5c6f1abe71f71cd9e0.tar.gz timeline-edac531b7c0bfc40bc9b6f5c6f1abe71f71cd9e0.tar.bz2 timeline-edac531b7c0bfc40bc9b6f5c6f1abe71f71cd9e0.zip |
Remove unnecessary columns in database query.
Diffstat (limited to 'Timeline')
-rw-r--r-- | Timeline/Entities/UserInfo.cs | 23 | ||||
-rw-r--r-- | Timeline/Services/UserService.cs | 18 | ||||
-rw-r--r-- | Timeline/Startup.cs | 32 |
3 files changed, 41 insertions, 32 deletions
diff --git a/Timeline/Entities/UserInfo.cs b/Timeline/Entities/UserInfo.cs index a1860552..c9bcde5b 100644 --- a/Timeline/Entities/UserInfo.cs +++ b/Timeline/Entities/UserInfo.cs @@ -17,25 +17,32 @@ namespace Timeline.Entities Roles = roles; } - public UserInfo(User user) + public static UserInfo Create(User user) { if (user == null) throw new ArgumentNullException(nameof(user)); - - Username = user.Name; - - if (user.RoleString == null) - Roles = null; - else - Roles = user.RoleString.Split(',').Select(r => r.Trim()).ToArray(); + return Create(user.Name, user.RoleString); } + public static UserInfo Create(string username, string roleString) => new UserInfo + { + Username = username, + Roles = RolesFromString(roleString) + }; + public string Username { get; set; } public string[] Roles { get; set; } public static IEqualityComparer<UserInfo> EqualityComparer { get; } = new EqualityComparerImpl(); public static IComparer<UserInfo> Comparer { get; } = Comparer<UserInfo>.Create(Compare); + private static string[] RolesFromString(string roleString) + { + if (roleString == null) + return null; + return roleString.Split(',').Select(r => r.Trim()).ToArray(); + } + private class EqualityComparerImpl : IEqualityComparer<UserInfo> { bool IEqualityComparer<UserInfo>.Equals(UserInfo x, UserInfo y) diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index caeb4efe..34eeb1ad 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -132,8 +132,6 @@ namespace Timeline.Services public async Task<CreateTokenResult> CreateToken(string username, string password) { - var users = _databaseContext.Users.ToList(); - var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync(); if (user == null) @@ -146,7 +144,7 @@ namespace Timeline.Services if (verifyResult) { - var userInfo = new UserInfo(user); + var userInfo = UserInfo.Create(user); return new CreateTokenResult { @@ -171,7 +169,10 @@ namespace Timeline.Services return null; } - var user = await _databaseContext.Users.Where(u => u.Id == userId.Value).SingleOrDefaultAsync(); + var user = await _databaseContext.Users + .Where(u => u.Id == userId.Value) + .Select(u => UserInfo.Create(u.Name, u.RoleString)) + .SingleOrDefaultAsync(); if (user == null) { @@ -179,19 +180,22 @@ namespace Timeline.Services return null; } - return new UserInfo(user); + return user; } public async Task<UserInfo> GetUser(string username) { return await _databaseContext.Users .Where(user => user.Name == username) - .Select(user => new UserInfo(user)).SingleOrDefaultAsync(); + .Select(user => UserInfo.Create(user.Name, user.RoleString)) + .SingleOrDefaultAsync(); } public async Task<UserInfo[]> ListUsers() { - return await _databaseContext.Users.Select(user => new UserInfo(user)).ToArrayAsync(); + return await _databaseContext.Users + .Select(user => UserInfo.Create(user.Name, user.RoleString)) + .ToArrayAsync(); } public async Task<PutUserResult> PutUser(string username, string password, string[] roles) diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 0c8d7052..285dfcfa 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; @@ -36,26 +37,16 @@ namespace Timeline options.InputFormatters.Add(new StringInputFormatter()); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); - if (Environment.IsDevelopment()) - { - services.AddCors(options => - { - options.AddPolicy(corsPolicyName, builder => - { - builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials(); - }); - }); - } - else + services.AddCors(options => { - services.AddCors(options => + options.AddPolicy(corsPolicyName, builder => { - options.AddPolicy(corsPolicyName, builder => - { + if (Environment.IsProduction()) builder.WithOrigins("https://www.crupest.xyz", "https://crupest.xyz").AllowAnyMethod().AllowAnyHeader().AllowCredentials(); - }); + else + builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials(); }); - } + }); services.Configure<JwtConfig>(Configuration.GetSection(nameof(JwtConfig))); var jwtConfig = Configuration.GetSection(nameof(JwtConfig)).Get<JwtConfig>(); @@ -80,7 +71,14 @@ namespace Timeline services.AddDbContext<DatabaseContext>(options => { - options.UseMySql(databaseConfig.ConnectionString); + options.UseMySql(databaseConfig.ConnectionString) + .ConfigureWarnings(warnings => + { + if (Environment.IsProduction()) + warnings.Log(RelationalEventId.QueryClientEvaluationWarning); + else + warnings.Throw(RelationalEventId.QueryClientEvaluationWarning); + }); }); } |