From dc1ab11cea249f4ca967f86b115147a63f7c93a5 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Mon, 5 Aug 2019 18:58:56 +0800 Subject: 3 things. 1. Exchange Models and Entities namespace. 2. Fix the bug that input with missing field leads to 500. 3. Write unit tests. --- Timeline/Startup.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'Timeline/Startup.cs') diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 242e816d..e6a8f96f 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -8,7 +8,8 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Timeline.Authenticate; using Timeline.Configs; -using Timeline.Models; +using Timeline.Entities; +using Timeline.Helpers; using Timeline.Services; namespace Timeline @@ -29,7 +30,11 @@ 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().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + services.AddMvc() + .ConfigureApiBehaviorOptions(options =>{ + options.InvalidModelStateResponseFactory = InvalidModelResponseFactory.Factory; + }) + .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddCors(options => { -- cgit v1.2.3 From 063321c90b8509249e65b49f39cf7d4f375305f6 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 8 Aug 2019 17:13:14 +0800 Subject: 2 things. 1. Make Administrator in UserPutRequest nullable. 2. Remove default route. --- Timeline/Controllers/UserController.cs | 10 +--------- Timeline/Models/Http/User.cs | 2 +- Timeline/Startup.cs | 2 +- 3 files changed, 3 insertions(+), 11 deletions(-) (limited to 'Timeline/Startup.cs') diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 0992946c..28d9523a 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -18,8 +18,6 @@ namespace Timeline.Controllers { public const int Get_NotExists = -1001; - public const int Put_NoPassword = -2001; - public const int Patch_NotExists = -3001; public const int ChangePassword_BadOldPassword = -4001; @@ -55,13 +53,7 @@ namespace Timeline.Controllers [HttpPut("user/{username}"), AdminAuthorize] public async Task Put([FromBody] UserPutRequest request, [FromRoute] string username) { - if (request.Password == null) // This place will be refactored. - { - _logger.LogInformation("Attempt to put a user without a password. Username: {} .", username); - return BadRequest(); - } - - var result = await _userService.PutUser(username, request.Password, request.Administrator); + var result = await _userService.PutUser(username, request.Password, request.Administrator.Value); switch (result) { case PutResult.Created: diff --git a/Timeline/Models/Http/User.cs b/Timeline/Models/Http/User.cs index 3259a448..d45543fb 100644 --- a/Timeline/Models/Http/User.cs +++ b/Timeline/Models/Http/User.cs @@ -7,7 +7,7 @@ namespace Timeline.Models.Http [Required] public string Password { get; set; } [Required] - public bool Administrator { get; set; } + public bool? Administrator { get; set; } } public class UserPatchRequest diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index a28899f4..414bc705 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -89,7 +89,7 @@ namespace Timeline app.UseAuthentication(); - app.UseMvcWithDefaultRoute(); + app.UseMvc(); } } } -- cgit v1.2.3