aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-03-10 16:01:09 +0800
committercrupest <crupest@outlook.com>2020-03-10 16:01:09 +0800
commit843da00b55c2296422596ec0539afbcadb3de3b5 (patch)
treef71188192238770e03ea2bf7d0b1e3485e4d219c
parent52c59d7cd949ee6ccc89fbb25231666e1e589fe2 (diff)
downloadtimeline-843da00b55c2296422596ec0539afbcadb3de3b5.tar.gz
timeline-843da00b55c2296422596ec0539afbcadb3de3b5.tar.bz2
timeline-843da00b55c2296422596ec0539afbcadb3de3b5.zip
...
-rw-r--r--Timeline/Controllers/PersonalTimelineController.cs131
-rw-r--r--Timeline/Controllers/TimelineController.cs14
-rw-r--r--Timeline/Models/Timeline.cs2
-rw-r--r--Timeline/Models/Validation/GeneralTimelineNameValidator.cs33
4 files changed, 42 insertions, 138 deletions
diff --git a/Timeline/Controllers/PersonalTimelineController.cs b/Timeline/Controllers/PersonalTimelineController.cs
deleted file mode 100644
index cef04a97..00000000
--- a/Timeline/Controllers/PersonalTimelineController.cs
+++ /dev/null
@@ -1,131 +0,0 @@
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.Extensions.Logging;
-using System.Collections.Generic;
-using System.Threading.Tasks;
-using Timeline.Filters;
-using Timeline.Models.Http;
-using Timeline.Models.Validation;
-using Timeline.Services;
-
-namespace Timeline.Controllers
-{
- [ApiController]
- [CatchTimelineNotExistException]
- public class PersonalTimelineController : Controller
- {
- private readonly ILogger<PersonalTimelineController> _logger;
-
- private readonly IPersonalTimelineService _service;
-
- public PersonalTimelineController(ILogger<PersonalTimelineController> logger, IPersonalTimelineService service)
- {
- _logger = logger;
- _service = service;
- }
-
- [HttpGet("users/{username}/timeline")]
- public async Task<ActionResult<TimelineInfo>> TimelineGet([FromRoute][Username] string username)
- {
- return (await _service.GetTimeline(username)).FillLinks(Url);
- }
-
- [HttpGet("users/{username}/timeline/posts")]
- public async Task<ActionResult<IList<TimelinePostInfo>>> PostListGet([FromRoute][Username] string username)
- {
- if (!this.IsAdministrator() && !await _service.HasReadPermission(username, this.GetOptionalUserId()))
- {
- return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
- }
-
- return await _service.GetPosts(username);
- }
-
- [HttpPost("users/{username}/timeline/posts")]
- [Authorize]
- public async Task<ActionResult<TimelinePostInfo>> PostPost([FromRoute][Username] string username, [FromBody] TimelinePostCreateRequest body)
- {
- var id = this.GetUserId();
- if (!this.IsAdministrator() && !await _service.IsMemberOf(username, id))
- {
- return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
- }
-
- var res = await _service.CreatePost(username, id, body.Content, body.Time);
- return res;
- }
-
- [HttpDelete("users/{username}/timeline/posts/{id}")]
- [Authorize]
- public async Task<ActionResult> PostDelete([FromRoute][Username] string username, [FromRoute] long id)
- {
- try
- {
- if (!this.IsAdministrator() && !await _service.HasPostModifyPermission(username, id, this.GetUserId()))
- {
- return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
- }
- await _service.DeletePost(username, id);
- return Ok(CommonDeleteResponse.Delete());
- }
- catch (TimelinePostNotExistException)
- {
- return Ok(CommonDeleteResponse.NotExist());
- }
- }
-
- [HttpPatch("users/{username}/timeline")]
- [Authorize]
- public async Task<ActionResult<TimelineInfo>> TimelinePatch([FromRoute][Username] string username, [FromBody] TimelinePatchRequest body)
- {
- if (!this.IsAdministrator() && !(await _service.HasManagePermission(username, this.GetUserId())))
- {
- return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
- }
- await _service.ChangeProperty(username, body);
- var timeline = (await _service.GetTimeline(username)).FillLinks(Url);
- return Ok(timeline);
- }
-
- [HttpPut("users/{username}/timeline/members/{member}")]
- [Authorize]
- public async Task<ActionResult> TimelineMemberPut([FromRoute][Username] string username, [FromRoute][Username] string member)
- {
- if (!this.IsAdministrator() && !(await _service.HasManagePermission(username, this.GetUserId())))
- {
- return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
- }
-
- try
- {
- await _service.ChangeMember(username, new List<string> { member }, null);
- return Ok();
- }
- catch (UserNotExistException)
- {
- return BadRequest(ErrorResponse.TimelineCommon.MemberPut_NotExist());
- }
- }
-
- [HttpDelete("users/{username}/timeline/members/{member}")]
- [Authorize]
- public async Task<ActionResult> TimelineMemberDelete([FromRoute][Username] string username, [FromRoute][Username] string member)
- {
- if (!this.IsAdministrator() && !(await _service.HasManagePermission(username, this.GetUserId())))
- {
- return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
- }
-
- try
- {
- await _service.ChangeMember(username, null, new List<string> { member });
- return Ok(CommonDeleteResponse.Delete());
- }
- catch (UserNotExistException)
- {
- return Ok(CommonDeleteResponse.NotExist());
- }
- }
- }
-}
diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs
index 25290ca5..0e5483fa 100644
--- a/Timeline/Controllers/TimelineController.cs
+++ b/Timeline/Controllers/TimelineController.cs
@@ -92,7 +92,7 @@ namespace Timeline.Controllers
}
[HttpGet("timelines/{name}")]
- public async Task<ActionResult<TimelineInfo>> TimelineGet([FromRoute][TimelineName] string name)
+ public async Task<ActionResult<TimelineInfo>> TimelineGet([FromRoute][GeneralTimelineName] string name)
{
var timeline = await _service.GetTimeline(name);
var result = _mapper.Map<TimelineInfo>(timeline);
@@ -100,7 +100,7 @@ namespace Timeline.Controllers
}
[HttpGet("timelines/{name}/posts")]
- public async Task<ActionResult<List<TimelinePostInfo>>> PostListGet([FromRoute][TimelineName] string name)
+ public async Task<ActionResult<List<TimelinePostInfo>>> PostListGet([FromRoute][GeneralTimelineName] string name)
{
if (!this.IsAdministrator() && !await _service.HasReadPermission(name, this.GetOptionalUserId()))
{
@@ -115,7 +115,7 @@ namespace Timeline.Controllers
[HttpPost("timelines/{name}/posts")]
[Authorize]
- public async Task<ActionResult<TimelinePostInfo>> PostPost([FromRoute][TimelineName] string name, [FromBody] TimelinePostCreateRequest body)
+ public async Task<ActionResult<TimelinePostInfo>> PostPost([FromRoute][GeneralTimelineName] string name, [FromBody] TimelinePostCreateRequest body)
{
var id = this.GetUserId();
if (!this.IsAdministrator() && !await _service.IsMemberOf(name, id))
@@ -129,7 +129,7 @@ namespace Timeline.Controllers
[HttpDelete("timelines/{name}/posts/{id}")]
[Authorize]
- public async Task<ActionResult<CommonDeleteResponse>> PostDelete([FromRoute][TimelineName] string name, [FromRoute] long id)
+ public async Task<ActionResult<CommonDeleteResponse>> PostDelete([FromRoute][GeneralTimelineName] string name, [FromRoute] long id)
{
try
{
@@ -148,7 +148,7 @@ namespace Timeline.Controllers
[HttpPatch("timelines/{name}")]
[Authorize]
- public async Task<ActionResult<TimelineInfo>> TimelinePatch([FromRoute][TimelineName] string name, [FromBody] TimelinePatchRequest body)
+ public async Task<ActionResult<TimelineInfo>> TimelinePatch([FromRoute][GeneralTimelineName] string name, [FromBody] TimelinePatchRequest body)
{
if (!this.IsAdministrator() && !(await _service.HasManagePermission(name, this.GetUserId())))
{
@@ -162,7 +162,7 @@ namespace Timeline.Controllers
[HttpPut("timelines/{name}/members/{member}")]
[Authorize]
- public async Task<ActionResult> TimelineMemberPut([FromRoute][TimelineName] string name, [FromRoute][Username] string member)
+ public async Task<ActionResult> TimelineMemberPut([FromRoute][GeneralTimelineName] string name, [FromRoute][Username] string member)
{
if (!this.IsAdministrator() && !(await _service.HasManagePermission(name, this.GetUserId())))
{
@@ -182,7 +182,7 @@ namespace Timeline.Controllers
[HttpDelete("timelines/{name}/members/{member}")]
[Authorize]
- public async Task<ActionResult> TimelineMemberDelete([FromRoute][TimelineName] string name, [FromRoute][Username] string member)
+ public async Task<ActionResult> TimelineMemberDelete([FromRoute][GeneralTimelineName] string name, [FromRoute][Username] string member)
{
if (!this.IsAdministrator() && !(await _service.HasManagePermission(name, this.GetUserId())))
{
diff --git a/Timeline/Models/Timeline.cs b/Timeline/Models/Timeline.cs
index e2ff525e..6d4c924d 100644
--- a/Timeline/Models/Timeline.cs
+++ b/Timeline/Models/Timeline.cs
@@ -55,7 +55,9 @@ namespace Timeline.Models
public DateTime LastUpdated { get; set; } = default!;
}
+#pragma warning disable CA1724 // Type names should not match namespaces
public class Timeline
+#pragma warning restore CA1724 // Type names should not match namespaces
{
public string Name { get; set; } = default!;
public string Description { get; set; } = default!;
diff --git a/Timeline/Models/Validation/GeneralTimelineNameValidator.cs b/Timeline/Models/Validation/GeneralTimelineNameValidator.cs
new file mode 100644
index 00000000..e1c96fbd
--- /dev/null
+++ b/Timeline/Models/Validation/GeneralTimelineNameValidator.cs
@@ -0,0 +1,33 @@
+using System;
+
+namespace Timeline.Models.Validation
+{
+ public class GeneralTimelineNameValidator : Validator<string>
+ {
+ private readonly UsernameValidator _usernameValidator = new UsernameValidator();
+ private readonly TimelineNameValidator _timelineNameValidator = new TimelineNameValidator();
+
+ protected override (bool, string) DoValidate(string value)
+ {
+ if (value.StartsWith('@'))
+ {
+ return _usernameValidator.Validate(value.Substring(1));
+ }
+ else
+ {
+ return _timelineNameValidator.Validate(value);
+ }
+ }
+ }
+
+ [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,
+ AllowMultiple = false)]
+ public class GeneralTimelineNameAttribute : ValidateWithAttribute
+ {
+ public GeneralTimelineNameAttribute()
+ : base(typeof(GeneralTimelineNameValidator))
+ {
+
+ }
+ }
+}