From df1ef1e21d8d889a2c9abd440039533c6a43818f Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Jan 2021 16:23:20 +0800 Subject: 史诗级重构! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/BookmarkTimelineController.cs | 16 +- .../Controllers/HighlightTimelineController.cs | 12 +- BackEnd/Timeline/Controllers/TimelineController.cs | 185 ++++++++++++--------- BackEnd/Timeline/Controllers/TokenController.cs | 10 +- BackEnd/Timeline/Controllers/UserController.cs | 15 +- 5 files changed, 128 insertions(+), 110 deletions(-) (limited to 'BackEnd/Timeline/Controllers') diff --git a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs index 9dff95f3..7412232d 100644 --- a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs +++ b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs @@ -1,9 +1,9 @@ -using AutoMapper; -using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading.Tasks; using Timeline.Models.Http; +using Timeline.Models.Mapper; using Timeline.Models.Validation; using Timeline.Services; using Timeline.Services.Exceptions; @@ -18,13 +18,12 @@ namespace Timeline.Controllers public class BookmarkTimelineController : Controller { private readonly IBookmarkTimelineService _service; + private readonly ITimelineService _timelineService; - private readonly IMapper _mapper; - - public BookmarkTimelineController(IBookmarkTimelineService service, IMapper mapper) + public BookmarkTimelineController(IBookmarkTimelineService service, ITimelineService timelineService) { _service = service; - _mapper = mapper; + _timelineService = timelineService; } /// @@ -37,8 +36,9 @@ namespace Timeline.Controllers [ProducesResponseType(401)] public async Task>> List() { - var bookmarks = await _service.GetBookmarks(this.GetUserId()); - return Ok(_mapper.Map>(bookmarks)); + var ids = await _service.GetBookmarks(this.GetUserId()); + var timelines = await _timelineService.GetTimelineList(ids); + return Ok(timelines.MapToHttp(Url)); } /// diff --git a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs index 519d6161..76650b00 100644 --- a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs +++ b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Timeline.Auth; using Timeline.Models.Http; +using Timeline.Models.Mapper; using Timeline.Models.Validation; using Timeline.Services; using Timeline.Services.Exceptions; @@ -18,12 +19,12 @@ namespace Timeline.Controllers public class HighlightTimelineController : Controller { private readonly IHighlightTimelineService _service; - private readonly IMapper _mapper; + private readonly ITimelineService _timelineService; - public HighlightTimelineController(IHighlightTimelineService service, IMapper mapper) + public HighlightTimelineController(IHighlightTimelineService service, ITimelineService timelineService) { _service = service; - _mapper = mapper; + _timelineService = timelineService; } /// @@ -34,8 +35,9 @@ namespace Timeline.Controllers [ProducesResponseType(200)] public async Task>> List() { - var t = await _service.GetHighlightTimelines(); - return _mapper.Map>(t); + var ids = await _service.GetHighlightTimelines(); + var timelines = await _timelineService.GetTimelineList(ids); + return timelines.MapToHttp(Url); } /// diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs index 27b4b7a7..b1401a03 100644 --- a/BackEnd/Timeline/Controllers/TimelineController.cs +++ b/BackEnd/Timeline/Controllers/TimelineController.cs @@ -2,15 +2,16 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; +using Timeline.Entities; using Timeline.Filters; using Timeline.Helpers; using Timeline.Models; using Timeline.Models.Http; +using Timeline.Models.Mapper; using Timeline.Models.Validation; using Timeline.Services; using Timeline.Services.Exceptions; @@ -25,8 +26,6 @@ namespace Timeline.Controllers [ProducesErrorResponseType(typeof(CommonResponse))] public class TimelineController : Controller { - private readonly ILogger _logger; - private readonly IUserService _userService; private readonly ITimelineService _service; private readonly ITimelinePostService _postService; @@ -36,9 +35,8 @@ namespace Timeline.Controllers /// /// /// - public TimelineController(ILogger logger, IUserService userService, ITimelineService service, ITimelinePostService timelinePostService, IMapper mapper) + public TimelineController(IUserService userService, ITimelineService service, ITimelinePostService timelinePostService, IMapper mapper) { - _logger = logger; _userService = userService; _service = service; _postService = timelinePostService; @@ -109,44 +107,46 @@ namespace Timeline.Controllers } var timelines = await _service.GetTimelines(relationship, visibilityFilter); - var result = _mapper.Map>(timelines); + var result = timelines.MapToHttp(Url); return result; } /// /// Get info of a timeline. /// - /// The timeline name. + /// The timeline name. /// A unique id. If specified and if-modified-since is also specified, the timeline info will return when unique id is not the specified one even if it is not modified. /// Same effect as If-Modified-Since header and take precedence than it. /// If specified, will return 304 if not modified. /// The timeline info. - [HttpGet("timelines/{name}")] + [HttpGet("timelines/{timeline}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status304NotModified)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task> TimelineGet([FromRoute][GeneralTimelineName] string name, [FromQuery] string? checkUniqueId, [FromQuery(Name = "ifModifiedSince")] DateTime? queryIfModifiedSince, [FromHeader(Name = "If-Modified-Since")] DateTime? headerIfModifiedSince) + public async Task> TimelineGet([FromRoute][GeneralTimelineName] string timeline, [FromQuery] string? checkUniqueId, [FromQuery(Name = "ifModifiedSince")] DateTime? queryIfModifiedSince, [FromHeader(Name = "If-Modified-Since")] DateTime? headerIfModifiedSince) { DateTime? ifModifiedSince = null; if (queryIfModifiedSince.HasValue) { ifModifiedSince = queryIfModifiedSince.Value; } - else if (headerIfModifiedSince != null) + else if (headerIfModifiedSince is not null) { ifModifiedSince = headerIfModifiedSince.Value; } + var timelineId = await _service.GetTimelineIdByName(timeline); + bool returnNotModified = false; if (ifModifiedSince.HasValue) { - var lastModified = await _service.GetTimelineLastModifiedTime(name); + var lastModified = await _service.GetTimelineLastModifiedTime(timelineId); if (lastModified < ifModifiedSince.Value) { if (checkUniqueId != null) { - var uniqueId = await _service.GetTimelineUniqueId(name); + var uniqueId = await _service.GetTimelineUniqueId(timelineId); if (uniqueId == checkUniqueId) { returnNotModified = true; @@ -165,8 +165,8 @@ namespace Timeline.Controllers } else { - var timeline = await _service.GetTimeline(name); - var result = _mapper.Map(timeline); + var t = await _service.GetTimeline(timelineId); + var result = t.MapToHttp(Url); return result; } } @@ -174,56 +174,59 @@ namespace Timeline.Controllers /// /// Get posts of a timeline. /// - /// The name of the timeline. + /// The name of the timeline. /// If set, only posts modified since the time will return. /// If set to true, deleted post will also return. /// The post list. - [HttpGet("timelines/{name}/posts")] + [HttpGet("timelines/{timeline}/posts")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task>> PostListGet([FromRoute][GeneralTimelineName] string name, [FromQuery] DateTime? modifiedSince, [FromQuery] bool? includeDeleted) + public async Task>> PostListGet([FromRoute][GeneralTimelineName] string timeline, [FromQuery] DateTime? modifiedSince, [FromQuery] bool? includeDeleted) { - if (!UserHasAllTimelineManagementPermission && !await _service.HasReadPermission(name, this.GetOptionalUserId())) + var timelineId = await _service.GetTimelineIdByName(timeline); + + if (!UserHasAllTimelineManagementPermission && !await _service.HasReadPermission(timelineId, this.GetOptionalUserId())) { return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } - List posts = await _postService.GetPosts(name, modifiedSince, includeDeleted ?? false); + var posts = await _postService.GetPosts(timelineId, modifiedSince, includeDeleted ?? false); - var result = _mapper.Map>(posts); + var result = posts.MapToHttp(timeline, Url); return result; } /// /// Get the data of a post. Usually a image post. /// - /// Timeline name. - /// The id of the post. + /// Timeline name. + /// The id of the post. /// If-None-Match header. /// The data. - [HttpGet("timelines/{name}/posts/{id}/data")] + [HttpGet("timelines/{timeline}/posts/{post}/data")] [Produces("image/png", "image/jpeg", "image/gif", "image/webp", "application/json", "text/json")] [ProducesResponseType(typeof(byte[]), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status304NotModified)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public async Task PostDataGet([FromRoute][GeneralTimelineName] string name, [FromRoute] long id, [FromHeader(Name = "If-None-Match")] string? ifNoneMatch) + public async Task PostDataGet([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post, [FromHeader(Name = "If-None-Match")] string? ifNoneMatch) { _ = ifNoneMatch; - if (!UserHasAllTimelineManagementPermission && !await _service.HasReadPermission(name, this.GetOptionalUserId())) + + var timelineId = await _service.GetTimelineIdByName(timeline); + + if (!UserHasAllTimelineManagementPermission && !await _service.HasReadPermission(timelineId, this.GetOptionalUserId())) { return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } try { - return await DataCacheHelper.GenerateActionResult(this, () => _postService.GetPostDataETag(name, id), async () => - { - var data = await _postService.GetPostData(name, id); - return data; - }); + return await DataCacheHelper.GenerateActionResult(this, + () => _postService.GetPostDataETag(timelineId, post), + async () => await _postService.GetPostData(timelineId, post)); } catch (TimelinePostNotExistException) { @@ -238,26 +241,28 @@ namespace Timeline.Controllers /// /// Create a new post. /// - /// Timeline name. + /// Timeline name. /// /// Info of new post. - [HttpPost("timelines/{name}/posts")] + [HttpPost("timelines/{timeline}/posts")] [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] - public async Task> PostPost([FromRoute][GeneralTimelineName] string name, [FromBody] HttpTimelinePostCreateRequest body) + public async Task> PostPost([FromRoute][GeneralTimelineName] string timeline, [FromBody] HttpTimelinePostCreateRequest body) { - var id = this.GetUserId(); - if (!UserHasAllTimelineManagementPermission && !await _service.IsMemberOf(name, id)) + var timelineId = await _service.GetTimelineIdByName(timeline); + var userId = this.GetUserId(); + + if (!UserHasAllTimelineManagementPermission && !await _service.IsMemberOf(timelineId, userId)) { return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } var content = body.Content; - TimelinePostInfo post; + TimelinePostEntity post; if (content.Type == TimelinePostContentTypes.Text) { @@ -266,7 +271,7 @@ namespace Timeline.Controllers { return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_TextContentTextRequired)); } - post = await _postService.CreateTextPost(name, id, text, body.Time); + post = await _postService.CreateTextPost(timelineId, userId, text, body.Time); } else if (content.Type == TimelinePostContentTypes.Image) { @@ -287,7 +292,7 @@ namespace Timeline.Controllers try { - post = await _postService.CreateImagePost(name, id, data, body.Time); + post = await _postService.CreateImagePost(timelineId, userId, data, body.Time); } catch (ImageException) { @@ -299,117 +304,128 @@ namespace Timeline.Controllers return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ContentUnknownType)); } - var result = _mapper.Map(post); + var result = post.MapToHttp(timeline, Url); return result; } /// /// Delete a post. /// - /// Timeline name. - /// Post id. + /// Timeline name. + /// Post id. /// Info of deletion. - [HttpDelete("timelines/{name}/posts/{id}")] + [HttpDelete("timelines/{timeline}/posts/{post}")] [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] - public async Task> PostDelete([FromRoute][GeneralTimelineName] string name, [FromRoute] long id) + public async Task PostDelete([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post) { - if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermission(name, id, this.GetUserId())) - { - return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); - } + var timelineId = await _service.GetTimelineIdByName(timeline); + try { - await _postService.DeletePost(name, id); - return CommonDeleteResponse.Delete(); + if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermission(timelineId, post, this.GetUserId(), true)) + { + return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + } + await _postService.DeletePost(timelineId, post); + return Ok(); } catch (TimelinePostNotExistException) { - return CommonDeleteResponse.NotExist(); + return BadRequest(ErrorResponse.TimelineController.PostNotExist()); } } /// /// Change properties of a timeline. /// - /// Timeline name. + /// Timeline name. /// /// The new info. - [HttpPatch("timelines/{name}")] + [HttpPatch("timelines/{timeline}")] [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] - public async Task> TimelinePatch([FromRoute][GeneralTimelineName] string name, [FromBody] HttpTimelinePatchRequest body) + public async Task> TimelinePatch([FromRoute][GeneralTimelineName] string timeline, [FromBody] HttpTimelinePatchRequest body) { - if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermission(name, this.GetUserId()))) + var timelineId = await _service.GetTimelineIdByName(timeline); + + if (!UserHasAllTimelineManagementPermission && !await _service.HasManagePermission(timelineId, this.GetUserId())) { return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } - await _service.ChangeProperty(name, _mapper.Map(body)); - var timeline = await _service.GetTimeline(name); - var result = _mapper.Map(timeline); + await _service.ChangeProperty(timelineId, _mapper.Map(body)); + var t = await _service.GetTimeline(timelineId); + var result = t.MapToHttp(Url); return result; } /// /// Add a member to timeline. /// - /// Timeline name. + /// Timeline name. /// The new member's username. - [HttpPut("timelines/{name}/members/{member}")] + [HttpPut("timelines/{timeline}/members/{member}")] [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] - public async Task TimelineMemberPut([FromRoute][GeneralTimelineName] string name, [FromRoute][Username] string member) + public async Task> TimelineMemberPut([FromRoute][GeneralTimelineName] string timeline, [FromRoute][Username] string member) { - if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermission(name, this.GetUserId()))) + var timelineId = await _service.GetTimelineIdByName(timeline); + + if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermission(timelineId, this.GetUserId()))) { return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } try { - await _service.ChangeMember(name, new List { member }, null); - return Ok(); + var userId = await _userService.GetUserIdByUsername(member); + var create = await _service.AddMember(timelineId, userId); + return Ok(CommonPutResponse.Create(create)); } catch (UserNotExistException) { - return BadRequest(ErrorResponse.TimelineController.MemberPut_NotExist()); + return BadRequest(ErrorResponse.UserCommon.NotExist()); } } /// /// Remove a member from timeline. /// - /// Timeline name. + /// Timeline name. /// The member's username. - [HttpDelete("timelines/{name}/members/{member}")] + [HttpDelete("timelines/{timeline}/members/{member}")] [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] - public async Task TimelineMemberDelete([FromRoute][GeneralTimelineName] string name, [FromRoute][Username] string member) + public async Task TimelineMemberDelete([FromRoute][GeneralTimelineName] string timeline, [FromRoute][Username] string member) { - if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermission(name, this.GetUserId()))) + var timelineId = await _service.GetTimelineIdByName(timeline); + + if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermission(timelineId, this.GetUserId()))) { return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } try { - await _service.ChangeMember(name, null, new List { member }); - return Ok(CommonDeleteResponse.Delete()); + var userId = await _userService.GetUserIdByUsername(member); + var delete = await _service.RemoveMember(timelineId, userId); + return Ok(CommonDeleteResponse.Create(delete)); } catch (UserNotExistException) { - return Ok(CommonDeleteResponse.NotExist()); + return BadRequest(ErrorResponse.UserCommon.NotExist()); } } @@ -430,7 +446,7 @@ namespace Timeline.Controllers try { var timeline = await _service.CreateTimeline(body.Name, userId); - var result = _mapper.Map(timeline); + var result = timeline.MapToHttp(Url); return result; } catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.Timeline) @@ -442,29 +458,31 @@ namespace Timeline.Controllers /// /// Delete a timeline. /// - /// Timeline name. + /// Timeline name. /// Info of deletion. - [HttpDelete("timelines/{name}")] + [HttpDelete("timelines/{timeline}")] [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] - public async Task> TimelineDelete([FromRoute][TimelineName] string name) + public async Task TimelineDelete([FromRoute][TimelineName] string timeline) { - if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermission(name, this.GetUserId()))) + var timelineId = await _service.GetTimelineIdByName(timeline); + + if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermission(timelineId, this.GetUserId()))) { return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } try { - await _service.DeleteTimeline(name); - return CommonDeleteResponse.Delete(); + await _service.DeleteTimeline(timelineId); + return Ok(); } catch (TimelineNotExistException) { - return CommonDeleteResponse.NotExist(); + return BadRequest(ErrorResponse.TimelineController.NotExist()); } } @@ -476,15 +494,18 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status403Forbidden)] public async Task> TimelineOpChangeName([FromBody] HttpTimelineChangeNameRequest body) { - if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermission(body.OldName, this.GetUserId()))) + var timelineId = await _service.GetTimelineIdByName(body.OldName); + + if (!UserHasAllTimelineManagementPermission && !(await _service.HasManagePermission(timelineId, this.GetUserId()))) { return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } try { - var timeline = await _service.ChangeTimelineName(body.OldName, body.NewName); - return Ok(_mapper.Map(timeline)); + await _service.ChangeTimelineName(timelineId, body.NewName); + var timeline = await _service.GetTimeline(timelineId); + return Ok(timeline.MapToHttp(Url)); } catch (EntityAlreadyExistException) { diff --git a/BackEnd/Timeline/Controllers/TokenController.cs b/BackEnd/Timeline/Controllers/TokenController.cs index c801b8cc..e695a10e 100644 --- a/BackEnd/Timeline/Controllers/TokenController.cs +++ b/BackEnd/Timeline/Controllers/TokenController.cs @@ -8,6 +8,7 @@ using System.Globalization; using System.Threading.Tasks; using Timeline.Helpers; using Timeline.Models.Http; +using Timeline.Models.Mapper; using Timeline.Services; using Timeline.Services.Exceptions; using static Timeline.Resources.Controllers.TokenController; @@ -27,16 +28,13 @@ namespace Timeline.Controllers private readonly ILogger _logger; private readonly IClock _clock; - private readonly IMapper _mapper; - /// - public TokenController(IUserCredentialService userCredentialService, IUserTokenManager userTokenManager, ILogger logger, IClock clock, IMapper mapper) + public TokenController(IUserCredentialService userCredentialService, IUserTokenManager userTokenManager, ILogger logger, IClock clock) { _userCredentialService = userCredentialService; _userTokenManager = userTokenManager; _logger = logger; _clock = clock; - _mapper = mapper; } /// @@ -74,7 +72,7 @@ namespace Timeline.Controllers return Ok(new HttpCreateTokenResponse { Token = result.Token, - User = _mapper.Map(result.User) + User = result.User.MapToHttp(Url) }); } catch (UserNotExistException e) @@ -115,7 +113,7 @@ namespace Timeline.Controllers ("Username", result.Username), ("Token", request.Token))); return Ok(new HttpVerifyTokenResponse { - User = _mapper.Map(result) + User = result.MapToHttp(Url) }); } catch (UserTokenTimeExpireException e) diff --git a/BackEnd/Timeline/Controllers/UserController.cs b/BackEnd/Timeline/Controllers/UserController.cs index 3727da36..93b17b2e 100644 --- a/BackEnd/Timeline/Controllers/UserController.cs +++ b/BackEnd/Timeline/Controllers/UserController.cs @@ -3,12 +3,11 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using System.Linq; using System.Threading.Tasks; using Timeline.Auth; using Timeline.Helpers; -using Timeline.Models; using Timeline.Models.Http; +using Timeline.Models.Mapper; using Timeline.Models.Validation; using Timeline.Services; using Timeline.Services.Exceptions; @@ -42,8 +41,6 @@ namespace Timeline.Controllers _mapper = mapper; } - private HttpUser ConvertToUserInfo(UserInfo user) => _mapper.Map(user); - private bool UserHasUserManagementPermission => this.UserHasPermission(UserPermission.UserManagement); /// @@ -55,7 +52,7 @@ namespace Timeline.Controllers public async Task> List() { var users = await _userService.GetUsers(); - var result = users.Select(u => ConvertToUserInfo(u)).ToArray(); + var result = users.MapToHttp(Url); return Ok(result); } @@ -73,7 +70,7 @@ namespace Timeline.Controllers { var id = await _userService.GetUserIdByUsername(username); var user = await _userService.GetUser(id); - return Ok(ConvertToUserInfo(user)); + return Ok(user.MapToHttp(Url)); } catch (UserNotExistException e) { @@ -102,7 +99,7 @@ namespace Timeline.Controllers { var id = await _userService.GetUserIdByUsername(username); var user = await _userService.ModifyUser(id, _mapper.Map(body)); - return Ok(ConvertToUserInfo(user)); + return Ok(user.MapToHttp(Url)); } catch (UserNotExistException e) { @@ -129,7 +126,7 @@ namespace Timeline.Controllers ErrorResponse.Common.CustomMessage_Forbid(UserController_Patch_Forbid_Password)); var user = await _userService.ModifyUser(this.GetUserId(), _mapper.Map(body)); - return Ok(ConvertToUserInfo(user)); + return Ok(user.MapToHttp(Url)); } } @@ -173,7 +170,7 @@ namespace Timeline.Controllers try { var user = await _userService.CreateUser(body.Username, body.Password); - return Ok(ConvertToUserInfo(user)); + return Ok(user.MapToHttp(Url)); } catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User) { -- cgit v1.2.3