aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Controllers
diff options
context:
space:
mode:
Diffstat (limited to 'BackEnd/Timeline/Controllers')
-rw-r--r--BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs14
-rw-r--r--BackEnd/Timeline/Controllers/V2/TimelineV2Controller.cs19
-rw-r--r--BackEnd/Timeline/Controllers/V2/UserV2Controller.cs19
-rw-r--r--BackEnd/Timeline/Controllers/V2/V2ControllerBase.cs26
4 files changed, 44 insertions, 34 deletions
diff --git a/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs b/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs
index 8a4fa7ed..4d486041 100644
--- a/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs
+++ b/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs
@@ -9,7 +9,6 @@ using Timeline.Helpers.Cache;
using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Models.Validation;
-using Timeline.Services.Mapper;
using Timeline.Services.Timeline;
using Timeline.Services.User;
using Timeline.SignalRHub;
@@ -23,17 +22,14 @@ namespace Timeline.Controllers.V2
private readonly ITimelineService _timelineService;
private readonly ITimelinePostService _postService;
- private readonly IGenericMapper _mapper;
-
private readonly MarkdownProcessor _markdownProcessor;
private readonly IHubContext<TimelineHub> _timelineHubContext;
- public TimelinePostV2Controller(ITimelineService timelineService, ITimelinePostService timelinePostService, IGenericMapper mapper, MarkdownProcessor markdownProcessor, IHubContext<TimelineHub> timelineHubContext)
+ public TimelinePostV2Controller(ITimelineService timelineService, ITimelinePostService timelinePostService, MarkdownProcessor markdownProcessor, IHubContext<TimelineHub> timelineHubContext)
{
_timelineService = timelineService;
_postService = timelinePostService;
- _mapper = mapper;
_markdownProcessor = markdownProcessor;
_timelineHubContext = timelineHubContext;
}
@@ -52,7 +48,7 @@ namespace Timeline.Controllers.V2
return Forbid();
}
var postPage = await _postService.GetPostsV2Async(timelineId, modifiedSince, page, pageSize);
- var items = await _mapper.MapListAsync<HttpTimelinePost>(postPage.Items, Url, User);
+ var items = await MapListAsync<HttpTimelinePost>(postPage.Items);
return postPage.WithItems(items);
}
@@ -70,7 +66,7 @@ namespace Timeline.Controllers.V2
return Forbid();
}
var post = await _postService.GetPostV2Async(timelineId, postId);
- var result = await _mapper.MapAsync<HttpTimelinePost>(post, Url, User);
+ var result = await MapAsync<HttpTimelinePost>(post);
return result;
}
@@ -164,7 +160,7 @@ namespace Timeline.Controllers.V2
var group = TimelineHub.GenerateTimelinePostChangeListeningGroupName(timeline);
await _timelineHubContext.Clients.Group(group).SendAsync(nameof(ITimelineClient.OnTimelinePostChanged), timeline);
- var result = await _mapper.MapAsync<HttpTimelinePost>(post, Url, User);
+ var result = await MapAsync<HttpTimelinePost>(post);
return CreatedAtAction("Get", new { owner = owner, timeline = timeline, post = post.LocalId }, result);
}
catch (TimelinePostCreateDataException e)
@@ -189,7 +185,7 @@ namespace Timeline.Controllers.V2
}
var entity = await _postService.PatchPostAsync(timelineId, post, new TimelinePostPatchRequest { Time = body.Time, Color = body.Color });
- var result = await _mapper.MapAsync<HttpTimelinePost>(entity, Url, User);
+ var result = await MapAsync<HttpTimelinePost>(entity);
return Ok(result);
}
diff --git a/BackEnd/Timeline/Controllers/V2/TimelineV2Controller.cs b/BackEnd/Timeline/Controllers/V2/TimelineV2Controller.cs
index 7f620928..7bc02dc2 100644
--- a/BackEnd/Timeline/Controllers/V2/TimelineV2Controller.cs
+++ b/BackEnd/Timeline/Controllers/V2/TimelineV2Controller.cs
@@ -2,11 +2,9 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
-using Timeline.Entities;
using Timeline.Models.Http;
using Timeline.Models.Validation;
using Timeline.Services;
-using Timeline.Services.Mapper;
using Timeline.Services.Timeline;
using Timeline.Services.User;
@@ -17,27 +15,20 @@ namespace Timeline.Controllers.V2
public class TimelineV2Controller : V2ControllerBase
{
private ITimelineService _timelineService;
- private IGenericMapper _mapper;
private IUserService _userService;
- public TimelineV2Controller(ITimelineService timelineService, IGenericMapper mapper, IUserService userService)
+ public TimelineV2Controller(ITimelineService timelineService, IUserService userService)
{
_timelineService = timelineService;
- _mapper = mapper;
_userService = userService;
}
- private Task<HttpTimeline> MapAsync(TimelineEntity entity)
- {
- return _mapper.MapAsync<HttpTimeline>(entity, Url, User);
- }
-
[HttpGet("{owner}/{timeline}")]
public async Task<ActionResult<HttpTimeline>> GetAsync([FromRoute][Username] string owner, [FromRoute][TimelineName] string timeline)
{
var timelineId = await _timelineService.GetTimelineIdAsync(owner, timeline);
var t = await _timelineService.GetTimelineAsync(timelineId);
- return await MapAsync(t);
+ return await MapAsync<HttpTimeline>(t);
}
[HttpPatch("{owner}/{timeline}")]
@@ -54,9 +45,9 @@ namespace Timeline.Controllers.V2
{
return Forbid();
}
- await _timelineService.ChangePropertyAsync(timelineId, _mapper.AutoMapperMap<TimelineChangePropertyParams>(body));
+ await _timelineService.ChangePropertyAsync(timelineId, AutoMapperMap<TimelineChangePropertyParams>(body));
var t = await _timelineService.GetTimelineAsync(timelineId);
- return await MapAsync(t);
+ return await MapAsync<HttpTimeline>(t);
}
[HttpDelete("{owner}/{timeline}")]
@@ -144,7 +135,7 @@ namespace Timeline.Controllers.V2
var authUserId = GetAuthUserId();
var authUser = await _userService.GetUserAsync(authUserId);
var timeline = await _timelineService.CreateTimelineAsync(authUserId, body.Name);
- var result = await MapAsync(timeline);
+ var result = await MapAsync<HttpTimeline>(timeline);
return CreatedAtAction("Get", new { owner = authUser.Username, timeline = body.Name }, result);
}
}
diff --git a/BackEnd/Timeline/Controllers/V2/UserV2Controller.cs b/BackEnd/Timeline/Controllers/V2/UserV2Controller.cs
index c84bab80..40657ad1 100644
--- a/BackEnd/Timeline/Controllers/V2/UserV2Controller.cs
+++ b/BackEnd/Timeline/Controllers/V2/UserV2Controller.cs
@@ -7,7 +7,6 @@ using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Models.Validation;
using Timeline.Services;
-using Timeline.Services.Mapper;
using Timeline.Services.User;
namespace Timeline.Controllers.V2
@@ -22,14 +21,12 @@ namespace Timeline.Controllers.V2
private readonly IUserService _userService;
private readonly IUserPermissionService _userPermissionService;
private readonly IUserDeleteService _userDeleteService;
- private readonly IGenericMapper _mapper;
- public UserV2Controller(IUserService userService, IUserPermissionService userPermissionService, IUserDeleteService userDeleteService, IGenericMapper mapper)
+ public UserV2Controller(IUserService userService, IUserPermissionService userPermissionService, IUserDeleteService userDeleteService)
{
_userService = userService;
_userPermissionService = userPermissionService;
_userDeleteService = userDeleteService;
- _mapper = mapper;
}
/// <summary>
@@ -41,7 +38,7 @@ namespace Timeline.Controllers.V2
public async Task<ActionResult<Page<HttpUser>>> ListAsync([FromQuery][PositiveInteger] int? page, [FromQuery][PositiveInteger] int? pageSize)
{
var p = await _userService.GetUsersV2Async(page ?? 1, pageSize ?? 20);
- var items = await _mapper.MapListAsync<HttpUser>(p.Items, Url, User);
+ var items = await MapListAsync<HttpUser>(p.Items);
return p.WithItems(items);
}
@@ -59,7 +56,7 @@ namespace Timeline.Controllers.V2
{
var user = await _userService.CreateUserAsync(
new CreateUserParams(body.Username, body.Password) { Nickname = body.Nickname });
- return CreatedAtAction("Get", new { username = body.Username }, await _mapper.MapAsync<HttpUser>(user, Url, User));
+ return CreatedAtAction("Get", new { username = body.Username }, await MapAsync<HttpUser>(user));
}
/// <summary>
@@ -75,7 +72,7 @@ namespace Timeline.Controllers.V2
{
var id = await _userService.GetUserIdByUsernameAsync(username);
var user = await _userService.GetUserAsync(id);
- return await _mapper.MapAsync<HttpUser>(user, Url, User);
+ return await MapAsync<HttpUser>(user);
}
/// <summary>
@@ -96,8 +93,8 @@ namespace Timeline.Controllers.V2
var userId = await _userService.GetUserIdByUsernameAsync(username);
if (UserHasPermission(UserPermission.UserManagement))
{
- var user = await _userService.ModifyUserAsync(userId, _mapper.AutoMapperMap<ModifyUserParams>(body));
- return await _mapper.MapAsync<HttpUser>(user, Url, User);
+ var user = await _userService.ModifyUserAsync(userId, AutoMapperMap<ModifyUserParams>(body));
+ return await MapAsync<HttpUser>(user);
}
else
{
@@ -110,8 +107,8 @@ namespace Timeline.Controllers.V2
if (body.Password is not null)
return Forbid();
- var user = await _userService.ModifyUserAsync(GetAuthUserId(), _mapper.AutoMapperMap<ModifyUserParams>(body));
- return await _mapper.MapAsync<HttpUser>(user, Url, User);
+ var user = await _userService.ModifyUserAsync(GetAuthUserId(), AutoMapperMap<ModifyUserParams>(body));
+ return await MapAsync<HttpUser>(user);
}
}
diff --git a/BackEnd/Timeline/Controllers/V2/V2ControllerBase.cs b/BackEnd/Timeline/Controllers/V2/V2ControllerBase.cs
index 54b9c7c9..d6fa0c84 100644
--- a/BackEnd/Timeline/Controllers/V2/V2ControllerBase.cs
+++ b/BackEnd/Timeline/Controllers/V2/V2ControllerBase.cs
@@ -1,6 +1,10 @@
using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.DependencyInjection;
using Timeline.Auth;
+using Timeline.Services.Mapper;
using Timeline.Services.User;
namespace Timeline.Controllers.V2
@@ -23,6 +27,28 @@ namespace Timeline.Controllers.V2
return GetOptionalAuthUserId() ?? throw new InvalidOperationException(Resource.ExceptionNoUserId);
}
#endregion
+
+ #region mapper
+ protected IGenericMapper GetMapper()
+ {
+ return HttpContext.RequestServices.GetRequiredService<IGenericMapper>();
+ }
+
+ protected async Task<T> MapAsync<T>(object o)
+ {
+ return await GetMapper().MapAsync<T>(o, Url, User);
+ }
+
+ protected async Task<List<T>> MapListAsync<T>(IEnumerable<object> o)
+ {
+ return await GetMapper().MapListAsync<T>(o, Url, User);
+ }
+
+ protected T AutoMapperMap<T>(object o)
+ {
+ return GetMapper().AutoMapperMap<T>(o);
+ }
+ #endregion
}
}