aboutsummaryrefslogtreecommitdiff
path: root/Timeline
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline')
-rw-r--r--Timeline/Controllers/UserDetailController.cs44
-rw-r--r--Timeline/Filters/User.cs42
-rw-r--r--Timeline/Resources/Filters.Designer.cs9
-rw-r--r--Timeline/Resources/Filters.resx3
-rw-r--r--Timeline/Resources/Filters.zh.resx3
5 files changed, 101 insertions, 0 deletions
diff --git a/Timeline/Controllers/UserDetailController.cs b/Timeline/Controllers/UserDetailController.cs
new file mode 100644
index 00000000..ef13b462
--- /dev/null
+++ b/Timeline/Controllers/UserDetailController.cs
@@ -0,0 +1,44 @@
+using Microsoft.AspNetCore.Mvc;
+using System.Threading.Tasks;
+using Timeline.Filters;
+using Timeline.Models.Validation;
+using Timeline.Services;
+using System.ComponentModel.DataAnnotations;
+
+namespace Timeline.Controllers
+{
+ [ApiController]
+ public class UserDetailController : Controller
+ {
+ private readonly IUserDetailService _service;
+
+ public UserDetailController(IUserDetailService service)
+ {
+ _service = service;
+ }
+
+ [HttpGet("users/{username}/nickname")]
+ [CatchUserNotExistException]
+ public async Task<ActionResult<string>> GetNickname([FromRoute][Username] string username)
+ {
+ return Ok(await _service.GetNickname(username));
+ }
+
+ [HttpPut("users/{username}/nickname")]
+ [CatchUserNotExistException]
+ public async Task<ActionResult> PutNickname([FromRoute][Username] string username,
+ [FromBody][StringLength(10, MinimumLength = 1)] string body)
+ {
+ await _service.SetNickname(username, body);
+ return Ok();
+ }
+
+ [HttpDelete("users/{username}/nickname")]
+ [CatchUserNotExistException]
+ public async Task<ActionResult> DeleteNickname([FromRoute][Username] string username)
+ {
+ await _service.SetNickname(username, null);
+ return Ok();
+ }
+ }
+}
diff --git a/Timeline/Filters/User.cs b/Timeline/Filters/User.cs
new file mode 100644
index 00000000..22fae938
--- /dev/null
+++ b/Timeline/Filters/User.cs
@@ -0,0 +1,42 @@
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Filters;
+using System;
+using Timeline.Models.Http;
+
+namespace Timeline
+{
+ public static partial class ErrorCodes
+ {
+ public static partial class Http
+ {
+ public static partial class Filter // bxx = 1xx
+ {
+ public static class User // bbb = 101
+ {
+ public const int NotExist = 11010001;
+ }
+
+ }
+ }
+ }
+}
+
+namespace Timeline.Filters
+{
+ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
+ public class CatchUserNotExistExceptionAttribute : ExceptionFilterAttribute
+ {
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "ASP.Net already checked.")]
+ public override void OnException(ExceptionContext context)
+ {
+ var body = new CommonResponse(
+ ErrorCodes.Http.Filter.User.NotExist,
+ Resources.Filters.MessageUserNotExist);
+
+ if (context.HttpContext.Request.Method == "GET")
+ context.Result = new NotFoundObjectResult(body);
+ else
+ context.Result = new BadRequestObjectResult(body);
+ }
+ }
+}
diff --git a/Timeline/Resources/Filters.Designer.cs b/Timeline/Resources/Filters.Designer.cs
index ae3565f7..e3c8be41 100644
--- a/Timeline/Resources/Filters.Designer.cs
+++ b/Timeline/Resources/Filters.Designer.cs
@@ -86,5 +86,14 @@ namespace Timeline.Resources {
return ResourceManager.GetString("MessageHeaderContentTypeMissing", resourceCulture);
}
}
+
+ /// <summary>
+ /// Looks up a localized string similar to The user does not exist..
+ /// </summary>
+ internal static string MessageUserNotExist {
+ get {
+ return ResourceManager.GetString("MessageUserNotExist", resourceCulture);
+ }
+ }
}
}
diff --git a/Timeline/Resources/Filters.resx b/Timeline/Resources/Filters.resx
index d2b7e68a..ba1fcee8 100644
--- a/Timeline/Resources/Filters.resx
+++ b/Timeline/Resources/Filters.resx
@@ -126,4 +126,7 @@
<data name="MessageHeaderContentTypeMissing" xml:space="preserve">
<value>Header Content-Type is required.</value>
</data>
+ <data name="MessageUserNotExist" xml:space="preserve">
+ <value>The user does not exist.</value>
+ </data>
</root> \ No newline at end of file
diff --git a/Timeline/Resources/Filters.zh.resx b/Timeline/Resources/Filters.zh.resx
index 90e97e49..690a3e39 100644
--- a/Timeline/Resources/Filters.zh.resx
+++ b/Timeline/Resources/Filters.zh.resx
@@ -126,4 +126,7 @@
<data name="MessageHeaderContentTypeMissing" xml:space="preserve">
<value>缺少必需的请求头Content-Type。</value>
</data>
+ <data name="MessageUserNotExist" xml:space="preserve">
+ <value>用户不存在。</value>
+ </data>
</root> \ No newline at end of file