diff options
author | crupest <crupest@outlook.com> | 2021-04-25 21:20:04 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-25 21:20:04 +0800 |
commit | 657fb589137099794e58fbd35beb7d942b376965 (patch) | |
tree | 7ab03d970f4c556b0a005f94da2e0752e9d7ce99 /BackEnd/Timeline/Models | |
parent | b64806226723df9a9deb64e80defc93860896f50 (diff) | |
download | timeline-657fb589137099794e58fbd35beb7d942b376965.tar.gz timeline-657fb589137099794e58fbd35beb7d942b376965.tar.bz2 timeline-657fb589137099794e58fbd35beb7d942b376965.zip |
...
Diffstat (limited to 'BackEnd/Timeline/Models')
-rw-r--r-- | BackEnd/Timeline/Models/ByteData.cs | 4 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Http/HttpAutoMapperProfile.cs | 3 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Mapper/MapperServiceCollectionExtensions.cs | 13 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Mapper/TimelineMapper.cs | 157 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Mapper/UserMapper.cs | 47 |
5 files changed, 4 insertions, 220 deletions
diff --git a/BackEnd/Timeline/Models/ByteData.cs b/BackEnd/Timeline/Models/ByteData.cs index a1a0c238..b10771b0 100644 --- a/BackEnd/Timeline/Models/ByteData.cs +++ b/BackEnd/Timeline/Models/ByteData.cs @@ -1,5 +1,5 @@ -using System;
-using NSwag.Annotations;
+using NSwag.Annotations;
+using System;
namespace Timeline.Models
{
diff --git a/BackEnd/Timeline/Models/Http/HttpAutoMapperProfile.cs b/BackEnd/Timeline/Models/Http/HttpAutoMapperProfile.cs index 426379b8..50c20862 100644 --- a/BackEnd/Timeline/Models/Http/HttpAutoMapperProfile.cs +++ b/BackEnd/Timeline/Models/Http/HttpAutoMapperProfile.cs @@ -1,5 +1,6 @@ using AutoMapper;
-using Timeline.Services;
+using Timeline.Services.Timeline;
+using Timeline.Services.User;
namespace Timeline.Models.Http
{
diff --git a/BackEnd/Timeline/Models/Mapper/MapperServiceCollectionExtensions.cs b/BackEnd/Timeline/Models/Mapper/MapperServiceCollectionExtensions.cs deleted file mode 100644 index c87586d2..00000000 --- a/BackEnd/Timeline/Models/Mapper/MapperServiceCollectionExtensions.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Microsoft.Extensions.DependencyInjection;
-
-namespace Timeline.Models.Mapper
-{
- public static class MapperServiceCollectionExtensions
- {
- public static void AddMappers(this IServiceCollection services)
- {
- services.AddScoped<UserMapper, UserMapper>();
- services.AddScoped<TimelineMapper, TimelineMapper>();
- }
- }
-}
diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs deleted file mode 100644 index e4304311..00000000 --- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs +++ /dev/null @@ -1,157 +0,0 @@ -using Microsoft.AspNetCore.Mvc;
-using Microsoft.EntityFrameworkCore;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
-using Timeline.Controllers;
-using Timeline.Entities;
-using Timeline.Models.Http;
-using Timeline.Services;
-
-namespace Timeline.Models.Mapper
-{
- public class TimelineMapper
- {
- private readonly DatabaseContext _database;
- private readonly UserMapper _userMapper;
- private readonly IHighlightTimelineService _highlightTimelineService;
- private readonly IBookmarkTimelineService _bookmarkTimelineService;
- private readonly ITimelineService _timelineService;
- private readonly ITimelinePostService _timelinePostService;
-
- public TimelineMapper(DatabaseContext database, UserMapper userMapper, IHighlightTimelineService highlightTimelineService, IBookmarkTimelineService bookmarkTimelineService, ITimelineService timelineService, ITimelinePostService timelinePostService)
- {
- _database = database;
- _userMapper = userMapper;
- _highlightTimelineService = highlightTimelineService;
- _bookmarkTimelineService = bookmarkTimelineService;
- _timelineService = timelineService;
- _timelinePostService = timelinePostService;
- }
-
- public async Task<HttpTimeline> MapToHttp(TimelineEntity entity, IUrlHelper urlHelper, long? userId, bool isAdministrator)
- {
- await _database.Entry(entity).Reference(e => e.Owner).LoadAsync();
- await _database.Entry(entity).Collection(e => e.Members).Query().Include(m => m.User).LoadAsync();
-
- var timelineName = entity.Name is null ? "@" + entity.Owner.Username : entity.Name;
-
- bool manageable;
-
- if (userId is null)
- {
- manageable = false;
- }
- else if (isAdministrator)
- {
- manageable = true;
- }
- else
- {
- manageable = await _timelineService.HasManagePermission(entity.Id, userId.Value);
- }
-
- bool postable;
- if (userId is null)
- {
- postable = false;
- }
- else
- {
- postable = await _timelineService.IsMemberOf(entity.Id, userId.Value);
- }
-
- return new HttpTimeline(
- uniqueId: entity.UniqueId,
- title: string.IsNullOrEmpty(entity.Title) ? timelineName : entity.Title,
- name: timelineName,
- nameLastModifed: entity.NameLastModified,
- description: entity.Description ?? "",
- owner: await _userMapper.MapToHttp(entity.Owner, urlHelper),
- visibility: entity.Visibility,
- members: await _userMapper.MapToHttp(entity.Members.Select(m => m.User).ToList(), urlHelper),
- color: entity.Color,
- createTime: entity.CreateTime,
- lastModified: entity.LastModified,
- isHighlight: await _highlightTimelineService.IsHighlightTimeline(entity.Id),
- isBookmark: userId is not null && await _bookmarkTimelineService.IsBookmark(userId.Value, entity.Id, false, false),
- manageable: manageable,
- postable: postable,
- links: new HttpTimelineLinks(
- self: urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { timeline = timelineName }),
- posts: urlHelper.ActionLink(nameof(TimelinePostController.List), nameof(TimelinePostController)[0..^nameof(Controller).Length], new { timeline = timelineName })
- )
- );
- }
-
- public async Task<List<HttpTimeline>> MapToHttp(List<TimelineEntity> entities, IUrlHelper urlHelper, long? userId, bool isAdministrator)
- {
- var result = new List<HttpTimeline>();
- foreach (var entity in entities)
- {
- result.Add(await MapToHttp(entity, urlHelper, userId, isAdministrator));
- }
- return result;
- }
-
-
- public async Task<HttpTimelinePost> MapToHttp(TimelinePostEntity entity, string timelineName, IUrlHelper urlHelper, long? userId, bool isAdministrator)
- {
- _ = timelineName;
-
- await _database.Entry(entity).Collection(p => p.DataList).LoadAsync();
- await _database.Entry(entity).Reference(e => e.Author).LoadAsync();
-
- List<HttpTimelinePostDataDigest> dataDigestList = entity.DataList.OrderBy(d => d.Index).Select(d => new HttpTimelinePostDataDigest(d.Kind, $"\"{d.DataTag}\"", d.LastUpdated)).ToList();
-
- HttpUser? author = null;
- if (entity.Author is not null)
- {
- author = await _userMapper.MapToHttp(entity.Author, urlHelper);
- }
-
- bool editable;
-
- if (userId is null)
- {
- editable = false;
- }
- else if (isAdministrator)
- {
- editable = true;
- }
- else
- {
- editable = await _timelinePostService.HasPostModifyPermission(entity.TimelineId, entity.LocalId, userId.Value);
- }
-
-
- return new HttpTimelinePost(
- id: entity.LocalId,
- dataList: dataDigestList,
- time: entity.Time,
- author: author,
- color: entity.Color,
- deleted: entity.Deleted,
- lastUpdated: entity.LastUpdated,
- timelineName: timelineName,
- editable: editable
- );
- }
-
- public async Task<List<HttpTimelinePost>> MapToHttp(List<TimelinePostEntity> entities, string timelineName, IUrlHelper urlHelper, long? userId, bool isAdministrator)
- {
- var result = new List<HttpTimelinePost>();
- foreach (var entity in entities)
- {
- result.Add(await MapToHttp(entity, timelineName, urlHelper, userId, isAdministrator));
- }
- return result;
- }
-
- internal Task MapToHttp(TimelinePostEntity post, string timeline, IUrlHelper url)
- {
- throw new System.NotImplementedException();
- }
- }
-}
diff --git a/BackEnd/Timeline/Models/Mapper/UserMapper.cs b/BackEnd/Timeline/Models/Mapper/UserMapper.cs deleted file mode 100644 index e6db4225..00000000 --- a/BackEnd/Timeline/Models/Mapper/UserMapper.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Microsoft.AspNetCore.Mvc;
-using System.Collections.Generic;
-using System.Threading.Tasks;
-using Timeline.Controllers;
-using Timeline.Entities;
-using Timeline.Models.Http;
-using Timeline.Services;
-
-namespace Timeline.Models.Mapper
-{
- public class UserMapper
- {
- private readonly DatabaseContext _database;
- private readonly IUserPermissionService _userPermissionService;
-
- public UserMapper(DatabaseContext database, IUserPermissionService userPermissionService)
- {
- _database = database;
- _userPermissionService = userPermissionService;
- }
-
- public async Task<HttpUser> MapToHttp(UserEntity entity, IUrlHelper urlHelper)
- {
- return new HttpUser(
- uniqueId: entity.UniqueId,
- username: entity.Username,
- nickname: string.IsNullOrEmpty(entity.Nickname) ? entity.Username : entity.Nickname,
- permissions: (await _userPermissionService.GetPermissionsOfUserAsync(entity.Id, false)).ToStringList(),
- links: new HttpUserLinks(
- self: urlHelper.ActionLink(nameof(UserController.Get), nameof(UserController)[0..^nameof(Controller).Length], new { entity.Username }),
- avatar: urlHelper.ActionLink(nameof(UserAvatarController.Get), nameof(UserAvatarController)[0..^nameof(Controller).Length], new { entity.Username }),
- timeline: urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { timeline = "@" + entity.Username })
- )
- );
- }
-
- public async Task<List<HttpUser>> MapToHttp(List<UserEntity> entities, IUrlHelper urlHelper)
- {
- var result = new List<HttpUser>();
- foreach (var entity in entities)
- {
- result.Add(await MapToHttp(entity, urlHelper));
- }
- return result;
- }
- }
-}
|