From 9df5a86786ac2dcb8bc0f34f69501abfffd0dc9c Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Mon, 4 Nov 2019 22:58:24 +0800 Subject: Add controller primarily and of course redesign the service accordingly. --- Timeline/Controllers/PersonalTimelineController.cs | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 Timeline/Controllers/PersonalTimelineController.cs (limited to 'Timeline/Controllers') diff --git a/Timeline/Controllers/PersonalTimelineController.cs b/Timeline/Controllers/PersonalTimelineController.cs new file mode 100644 index 00000000..1535a0b2 --- /dev/null +++ b/Timeline/Controllers/PersonalTimelineController.cs @@ -0,0 +1,141 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Threading.Tasks; +using Timeline.Auth; +using Timeline.Entities; +using Timeline.Filters; +using Timeline.Models; +using Timeline.Models.Http; +using Timeline.Models.Validation; +using Timeline.Services; +using static Timeline.Resources.Controllers.TimelineController; + +namespace Timeline +{ + public static partial class ErrorCodes + { + public static partial class Http + { + public static class Timeline // ccc = 004 + { + public const int PostsGetForbid = 10040101; + public const int PostsCreateForbid = 10040102; + } + } + } +} + +namespace Timeline.Controllers +{ + [ApiController] + public class PersonalTimelineController : Controller + { + private readonly IPersonalTimelineService _service; + + private bool IsAdmin() + { + if (User != null) + { + return User.IsAdministrator(); + } + return false; + } + + private string? GetAuthUsername() + { + if (User == null) + { + return null; + } + else + { + return User.Identity.Name; + } + } + + public PersonalTimelineController(IPersonalTimelineService service) + { + _service = service; + } + + [HttpGet("users/{username}/timeline")] + public async Task> TimelineGet([FromRoute][Username] string username) + { + return await _service.GetTimeline(username); + } + + [HttpGet("users/{username}/timeline/posts")] + public async Task>> PostsGet([FromRoute][Username] string username) + { + if (!IsAdmin() && !await _service.HasReadPermission(username, GetAuthUsername())) + { + return StatusCode(StatusCodes.Status403Forbidden, + new CommonResponse(ErrorCodes.Http.Timeline.PostsGetForbid, MessagePostsGetForbid)); + } + + return await _service.GetPosts(username); + } + + [HttpPost("user/{username}/timeline/posts/create")] + [Authorize] + public async Task PostsCreate([FromRoute][Username] string username, [FromBody] TimelinePostCreateRequest body) + { + if (!IsAdmin() && !await _service.IsMemberOf(username, GetAuthUsername()!)) + { + return StatusCode(StatusCodes.Status403Forbidden, + new CommonResponse(ErrorCodes.Http.Timeline.PostsCreateForbid, MessagePostsCreateForbid)); + } + + await _service.CreatePost(username, User.Identity.Name!, body.Content, body.Time); + return Ok(); + } + + [HttpPut("user/{username}/timeline/description")] + [Authorize] + [SelfOrAdmin] + public async Task TimelinePutDescription([FromRoute][Username] string username, [FromBody] string body) + { + await _service.SetDescription(username, body); + return Ok(); + } + + private static TimelineVisibility StringToVisibility(string s) + { + if ("public".Equals(s, StringComparison.InvariantCultureIgnoreCase)) + { + return TimelineVisibility.Public; + } + else if ("register".Equals(s, StringComparison.InvariantCultureIgnoreCase)) + { + return TimelineVisibility.Register; + } + else if ("private".Equals(s, StringComparison.InvariantCultureIgnoreCase)) + { + return TimelineVisibility.Private; + } + throw new ArgumentException(ExceptionStringToVisibility); + } + + [HttpPut("user/{username}/timeline/visibility")] + [Authorize] + [SelfOrAdmin] + public async Task TimelinePutVisibility([FromRoute][Username] string username, [FromBody][RegularExpression("public|register|private")] string body) + { + await _service.SetVisibility(username, StringToVisibility(body)); + return Ok(); + } + + [HttpPost("user/{username}/timeline/members/change")] + [Authorize] + [SelfOrAdmin] + public async Task TimelineMembersChange([FromRoute][Username] string username, [FromBody] TimelineMemberChangeRequest body) + { + //TODO! + } + } +} -- cgit v1.2.3