aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Controllers
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-01-07 20:14:55 +0800
committerGitHub <noreply@github.com>2021-01-07 20:14:55 +0800
commite02871ff8bba7fccebdaaeea29141ed5e3289c09 (patch)
tree67128b91d6cfd08c54c9e745e7bf2abbdf1f20f4 /BackEnd/Timeline/Controllers
parent5ad1b1f0191ee1131e7808c8fcb0484ba29c0d4d (diff)
parentffe44ba70c9e5c6a01179c7e2f4185543cbc441c (diff)
downloadtimeline-e02871ff8bba7fccebdaaeea29141ed5e3289c09.tar.gz
timeline-e02871ff8bba7fccebdaaeea29141ed5e3289c09.tar.bz2
timeline-e02871ff8bba7fccebdaaeea29141ed5e3289c09.zip
Merge pull request #203 from crupest/back-dev
refactor: Make mapper a service. Fix #202.
Diffstat (limited to 'BackEnd/Timeline/Controllers')
-rw-r--r--BackEnd/Timeline/Controllers/BookmarkTimelineController.cs6
-rw-r--r--BackEnd/Timeline/Controllers/HighlightTimelineController.cs9
-rw-r--r--BackEnd/Timeline/Controllers/TimelineController.cs18
-rw-r--r--BackEnd/Timeline/Controllers/TokenController.cs17
-rw-r--r--BackEnd/Timeline/Controllers/UserController.cs19
5 files changed, 39 insertions, 30 deletions
diff --git a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs
index 7412232d..64cb8afa 100644
--- a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs
+++ b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs
@@ -19,11 +19,13 @@ namespace Timeline.Controllers
{
private readonly IBookmarkTimelineService _service;
private readonly ITimelineService _timelineService;
+ private readonly TimelineMapper _timelineMapper;
- public BookmarkTimelineController(IBookmarkTimelineService service, ITimelineService timelineService)
+ public BookmarkTimelineController(IBookmarkTimelineService service, ITimelineService timelineService, TimelineMapper timelineMapper)
{
_service = service;
_timelineService = timelineService;
+ _timelineMapper = timelineMapper;
}
/// <summary>
@@ -38,7 +40,7 @@ namespace Timeline.Controllers
{
var ids = await _service.GetBookmarks(this.GetUserId());
var timelines = await _timelineService.GetTimelineList(ids);
- return Ok(timelines.MapToHttp(Url));
+ return await _timelineMapper.MapToHttp(timelines, Url);
}
/// <summary>
diff --git a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs
index 76650b00..685ec16f 100644
--- a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs
+++ b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs
@@ -1,5 +1,4 @@
-using AutoMapper;
-using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Timeline.Auth;
@@ -20,11 +19,13 @@ namespace Timeline.Controllers
{
private readonly IHighlightTimelineService _service;
private readonly ITimelineService _timelineService;
+ private readonly TimelineMapper _timelineMapper;
- public HighlightTimelineController(IHighlightTimelineService service, ITimelineService timelineService)
+ public HighlightTimelineController(IHighlightTimelineService service, ITimelineService timelineService, TimelineMapper timelineMapper)
{
_service = service;
_timelineService = timelineService;
+ _timelineMapper = timelineMapper;
}
/// <summary>
@@ -37,7 +38,7 @@ namespace Timeline.Controllers
{
var ids = await _service.GetHighlightTimelines();
var timelines = await _timelineService.GetTimelineList(ids);
- return timelines.MapToHttp(Url);
+ return await _timelineMapper.MapToHttp(timelines, Url);
}
/// <summary>
diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs
index b1401a03..efc49952 100644
--- a/BackEnd/Timeline/Controllers/TimelineController.cs
+++ b/BackEnd/Timeline/Controllers/TimelineController.cs
@@ -30,16 +30,18 @@ namespace Timeline.Controllers
private readonly ITimelineService _service;
private readonly ITimelinePostService _postService;
+ private readonly TimelineMapper _timelineMapper;
private readonly IMapper _mapper;
/// <summary>
///
/// </summary>
- public TimelineController(IUserService userService, ITimelineService service, ITimelinePostService timelinePostService, IMapper mapper)
+ public TimelineController(IUserService userService, ITimelineService service, ITimelinePostService timelinePostService, TimelineMapper timelineMapper, IMapper mapper)
{
_userService = userService;
_service = service;
_postService = timelinePostService;
+ _timelineMapper = timelineMapper;
_mapper = mapper;
}
@@ -107,7 +109,7 @@ namespace Timeline.Controllers
}
var timelines = await _service.GetTimelines(relationship, visibilityFilter);
- var result = timelines.MapToHttp(Url);
+ var result = await _timelineMapper.MapToHttp(timelines, Url);
return result;
}
@@ -166,7 +168,7 @@ namespace Timeline.Controllers
else
{
var t = await _service.GetTimeline(timelineId);
- var result = t.MapToHttp(Url);
+ var result = await _timelineMapper.MapToHttp(t, Url);
return result;
}
}
@@ -193,7 +195,7 @@ namespace Timeline.Controllers
var posts = await _postService.GetPosts(timelineId, modifiedSince, includeDeleted ?? false);
- var result = posts.MapToHttp(timeline, Url);
+ var result = await _timelineMapper.MapToHttp(posts, timeline, Url);
return result;
}
@@ -304,7 +306,7 @@ namespace Timeline.Controllers
return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ContentUnknownType));
}
- var result = post.MapToHttp(timeline, Url);
+ var result = await _timelineMapper.MapToHttp(post, timeline, Url);
return result;
}
@@ -361,7 +363,7 @@ namespace Timeline.Controllers
}
await _service.ChangeProperty(timelineId, _mapper.Map<TimelineChangePropertyParams>(body));
var t = await _service.GetTimeline(timelineId);
- var result = t.MapToHttp(Url);
+ var result = await _timelineMapper.MapToHttp(t, Url);
return result;
}
@@ -446,7 +448,7 @@ namespace Timeline.Controllers
try
{
var timeline = await _service.CreateTimeline(body.Name, userId);
- var result = timeline.MapToHttp(Url);
+ var result = await _timelineMapper.MapToHttp(timeline, Url);
return result;
}
catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.Timeline)
@@ -505,7 +507,7 @@ namespace Timeline.Controllers
{
await _service.ChangeTimelineName(timelineId, body.NewName);
var timeline = await _service.GetTimeline(timelineId);
- return Ok(timeline.MapToHttp(Url));
+ return await _timelineMapper.MapToHttp(timeline, Url);
}
catch (EntityAlreadyExistException)
{
diff --git a/BackEnd/Timeline/Controllers/TokenController.cs b/BackEnd/Timeline/Controllers/TokenController.cs
index e695a10e..b3675aad 100644
--- a/BackEnd/Timeline/Controllers/TokenController.cs
+++ b/BackEnd/Timeline/Controllers/TokenController.cs
@@ -1,4 +1,3 @@
-using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@@ -26,14 +25,16 @@ namespace Timeline.Controllers
private readonly IUserCredentialService _userCredentialService;
private readonly IUserTokenManager _userTokenManager;
private readonly ILogger<TokenController> _logger;
+ private readonly UserMapper _userMapper;
private readonly IClock _clock;
/// <summary></summary>
- public TokenController(IUserCredentialService userCredentialService, IUserTokenManager userTokenManager, ILogger<TokenController> logger, IClock clock)
+ public TokenController(IUserCredentialService userCredentialService, IUserTokenManager userTokenManager, ILogger<TokenController> logger, UserMapper userMapper, IClock clock)
{
_userCredentialService = userCredentialService;
_userTokenManager = userTokenManager;
_logger = logger;
+ _userMapper = userMapper;
_clock = clock;
}
@@ -69,11 +70,11 @@ namespace Timeline.Controllers
("Username", request.Username),
("Expire At", expireTime?.ToString(CultureInfo.CurrentCulture.DateTimeFormat) ?? "default")
));
- return Ok(new HttpCreateTokenResponse
+ return new HttpCreateTokenResponse
{
Token = result.Token,
- User = result.User.MapToHttp(Url)
- });
+ User = await _userMapper.MapToHttp(result.User, Url)
+ };
}
catch (UserNotExistException e)
{
@@ -111,10 +112,10 @@ namespace Timeline.Controllers
var result = await _userTokenManager.VerifyToken(request.Token);
_logger.LogInformation(Log.Format(LogVerifySuccess,
("Username", result.Username), ("Token", request.Token)));
- return Ok(new HttpVerifyTokenResponse
+ return new HttpVerifyTokenResponse
{
- User = result.MapToHttp(Url)
- });
+ User = await _userMapper.MapToHttp(result, Url)
+ };
}
catch (UserTokenTimeExpireException e)
{
diff --git a/BackEnd/Timeline/Controllers/UserController.cs b/BackEnd/Timeline/Controllers/UserController.cs
index 93b17b2e..e1a9d454 100644
--- a/BackEnd/Timeline/Controllers/UserController.cs
+++ b/BackEnd/Timeline/Controllers/UserController.cs
@@ -3,6 +3,7 @@ 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.Auth;
using Timeline.Helpers;
@@ -28,16 +29,18 @@ namespace Timeline.Controllers
private readonly IUserCredentialService _userCredentialService;
private readonly IUserPermissionService _userPermissionService;
private readonly IUserDeleteService _userDeleteService;
+ private readonly UserMapper _userMapper;
private readonly IMapper _mapper;
/// <summary></summary>
- public UserController(ILogger<UserController> logger, IUserService userService, IUserCredentialService userCredentialService, IUserPermissionService userPermissionService, IUserDeleteService userDeleteService, IMapper mapper)
+ public UserController(ILogger<UserController> logger, IUserService userService, IUserCredentialService userCredentialService, IUserPermissionService userPermissionService, IUserDeleteService userDeleteService, UserMapper userMapper, IMapper mapper)
{
_logger = logger;
_userService = userService;
_userCredentialService = userCredentialService;
_userPermissionService = userPermissionService;
_userDeleteService = userDeleteService;
+ _userMapper = userMapper;
_mapper = mapper;
}
@@ -49,11 +52,11 @@ namespace Timeline.Controllers
/// <returns>All user list.</returns>
[HttpGet("users")]
[ProducesResponseType(StatusCodes.Status200OK)]
- public async Task<ActionResult<HttpUser[]>> List()
+ public async Task<ActionResult<List<HttpUser>>> List()
{
var users = await _userService.GetUsers();
- var result = users.MapToHttp(Url);
- return Ok(result);
+ var result = await _userMapper.MapToHttp(users, Url);
+ return result;
}
/// <summary>
@@ -70,7 +73,7 @@ namespace Timeline.Controllers
{
var id = await _userService.GetUserIdByUsername(username);
var user = await _userService.GetUser(id);
- return Ok(user.MapToHttp(Url));
+ return await _userMapper.MapToHttp(user, Url);
}
catch (UserNotExistException e)
{
@@ -99,7 +102,7 @@ namespace Timeline.Controllers
{
var id = await _userService.GetUserIdByUsername(username);
var user = await _userService.ModifyUser(id, _mapper.Map<ModifyUserParams>(body));
- return Ok(user.MapToHttp(Url));
+ return await _userMapper.MapToHttp(user, Url);
}
catch (UserNotExistException e)
{
@@ -126,7 +129,7 @@ namespace Timeline.Controllers
ErrorResponse.Common.CustomMessage_Forbid(UserController_Patch_Forbid_Password));
var user = await _userService.ModifyUser(this.GetUserId(), _mapper.Map<ModifyUserParams>(body));
- return Ok(user.MapToHttp(Url));
+ return await _userMapper.MapToHttp(user, Url);
}
}
@@ -170,7 +173,7 @@ namespace Timeline.Controllers
try
{
var user = await _userService.CreateUser(body.Username, body.Password);
- return Ok(user.MapToHttp(Url));
+ return await _userMapper.MapToHttp(user, Url);
}
catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User)
{