From 2984abc9aa0429380459e5b5b6fda2d20058041b Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sat, 19 Oct 2019 17:33:38 +0800 Subject: ... --- Timeline/GlobalSuppressions.cs | 9 ++++++ Timeline/Models/Http/Common.cs | 2 +- Timeline/Program.cs | 2 +- Timeline/Services/UserService.cs | 61 +++++++++++++++++++++++++++++++--------- Timeline/Startup.cs | 5 ++-- Timeline/Timeline.csproj | 7 ++++- 6 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 Timeline/GlobalSuppressions.cs (limited to 'Timeline') diff --git a/Timeline/GlobalSuppressions.cs b/Timeline/GlobalSuppressions.cs new file mode 100644 index 00000000..3c9c8341 --- /dev/null +++ b/Timeline/GlobalSuppressions.cs @@ -0,0 +1,9 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "This is not a UI application.")] +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "This is not bad.")] +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "No need to check the null because it's ASP.Net's duty.", Scope = "namespaceanddescendants", Target = "Timeline.Controllers")] +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Error code constant identifiers use nested names.", Scope = "type", Target = "Timeline.ErrorCodes")] diff --git a/Timeline/Models/Http/Common.cs b/Timeline/Models/Http/Common.cs index af185e85..83e6a072 100644 --- a/Timeline/Models/Http/Common.cs +++ b/Timeline/Models/Http/Common.cs @@ -38,7 +38,7 @@ namespace Timeline.Models.Http Message = message; } - public int Code { get; set; } + public int? Code { get; set; } public string Message { get; set; } } diff --git a/Timeline/Program.cs b/Timeline/Program.cs index dfc93b9e..7474fe2f 100644 --- a/Timeline/Program.cs +++ b/Timeline/Program.cs @@ -6,7 +6,7 @@ using Microsoft.Extensions.Hosting; namespace Timeline { - public class Program + public static class Program { public static void Main(string[] args) { diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 347b8cbb..9564b34b 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -5,6 +5,7 @@ using System; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; +using Timeline.Helpers; using Timeline.Models; using Timeline.Models.Validation; using static Timeline.Helpers.MyLogHelper; @@ -23,14 +24,20 @@ namespace Timeline.Services { private const string message = "The user does not exist."; + public UserNotExistException() + : base(message) + { + + } + public UserNotExistException(string username) - : base(FormatLogMessage(message, Pair("Username", username))) + : base(Log.Format(message, ("Username", username))) { Username = username; } public UserNotExistException(long id) - : base(FormatLogMessage(message, Pair("Id", id))) + : base(Log.Format(message, ("Id", id))) { Id = id; } @@ -42,21 +49,29 @@ namespace Timeline.Services System.Runtime.Serialization.StreamingContext context) : base(info, context) { } /// - /// The username that does not exist. May be null then is not null. + /// The username that does not exist. /// - public string Username { get; private set; } + public string Username { get; set; } /// - /// The id that does not exist. May be null then is not null. + /// The id that does not exist. /// - public long? Id { get; private set; } + public long? Id { get; set; } } [Serializable] public class BadPasswordException : Exception { + private const string message = "Password is wrong."; + + public BadPasswordException() + : base(message) + { + + } + public BadPasswordException(string badPassword) - : base(FormatLogMessage("Password is wrong.", Pair("Bad Password", badPassword))) + : base(Log.Format(message, ("Bad Password", badPassword))) { Password = badPassword; } @@ -70,22 +85,31 @@ namespace Timeline.Services /// /// The wrong password. /// - public string Password { get; private set; } + public string Password { get; set; } } [Serializable] public class BadTokenVersionException : Exception { + private const string message = "Token version is expired."; + + public BadTokenVersionException() + : base(message) + { + + } + public BadTokenVersionException(long tokenVersion, long requiredVersion) - : base(FormatLogMessage("Token version is expired.", - Pair("Token Version", tokenVersion), - Pair("Required Version", requiredVersion))) + : base(Log.Format(message, + ("Token Version", tokenVersion), + ("Required Version", requiredVersion))) { TokenVersion = tokenVersion; RequiredVersion = requiredVersion; } + public BadTokenVersionException(string message) : base(message) { } public BadTokenVersionException(string message, Exception inner) : base(message, inner) { } protected BadTokenVersionException( @@ -95,12 +119,12 @@ namespace Timeline.Services /// /// The version in the token. /// - public long TokenVersion { get; private set; } + public long? TokenVersion { get; set; } /// /// The version required. /// - public long RequiredVersion { get; private set; } + public long? RequiredVersion { get; set; } } /// @@ -109,6 +133,12 @@ namespace Timeline.Services [Serializable] public class UsernameBadFormatException : Exception { + private const string message = "Username is of bad format."; + + public UsernameBadFormatException() : base(message) { } + public UsernameBadFormatException(string message) : base(message) { } + public UsernameBadFormatException(string message, Exception inner) : base(message, inner) { } + public UsernameBadFormatException(string username, string message) : base(message) { Username = username; } public UsernameBadFormatException(string username, string message, Exception inner) : base(message, inner) { Username = username; } protected UsernameBadFormatException( @@ -128,7 +158,10 @@ namespace Timeline.Services [Serializable] public class UserAlreadyExistException : Exception { - public UserAlreadyExistException(string username) : base($"User {username} already exists.") { Username = username; } + private const string message = "User already exists."; + + public UserAlreadyExistException() : base(message) { } + public UserAlreadyExistException(string username) : base(Log.Format(message, ("Username", username))) { Username = username; } public UserAlreadyExistException(string username, string message) : base(message) { Username = username; } public UserAlreadyExistException(string message, Exception inner) : base(message, inner) { } protected UserAlreadyExistException( diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 8e8a6393..fc570fdd 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -27,11 +27,12 @@ namespace Timeline // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc() + services.AddControllers() .ConfigureApiBehaviorOptions(options => { options.InvalidModelStateResponseFactory = InvalidModelResponseFactory.Factory; - }); + }) + .AddNewtonsoftJson(); services.Configure(Configuration.GetSection(nameof(JwtConfig))); var jwtConfig = Configuration.GetSection(nameof(JwtConfig)).Get(); diff --git a/Timeline/Timeline.csproj b/Timeline/Timeline.csproj index f01b8e31..836dfb47 100644 --- a/Timeline/Timeline.csproj +++ b/Timeline/Timeline.csproj @@ -13,9 +13,14 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + -- cgit v1.2.3