aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/TimelineService.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-03-07 22:02:31 +0800
committercrupest <crupest@outlook.com>2020-03-07 22:02:31 +0800
commitb686b383022e6d9db2bd61ad441be753cae3dbcf (patch)
tree391e42cf9106161bd6e9c84f5c73ef5d07ae84d7 /Timeline/Services/TimelineService.cs
parent968140e8aaba398e10585e978aff33d7b32e824a (diff)
downloadtimeline-b686b383022e6d9db2bd61ad441be753cae3dbcf.tar.gz
timeline-b686b383022e6d9db2bd61ad441be753cae3dbcf.tar.bz2
timeline-b686b383022e6d9db2bd61ad441be753cae3dbcf.zip
...
Diffstat (limited to 'Timeline/Services/TimelineService.cs')
-rw-r--r--Timeline/Services/TimelineService.cs86
1 files changed, 80 insertions, 6 deletions
diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs
index d999587a..edd0a0ab 100644
--- a/Timeline/Services/TimelineService.cs
+++ b/Timeline/Services/TimelineService.cs
@@ -32,6 +32,14 @@ namespace Timeline.Services
public long UserId { get; set; }
}
+ public class DataWithType
+ {
+#pragma warning disable CA1819 // Properties should not return arrays
+ public byte[] Data { get; set; } = default!;
+#pragma warning restore CA1819 // Properties should not return arrays
+ public string Type { get; set; } = default!;
+ }
+
/// <summary>
/// This define the common interface of both personal timeline
/// and normal timeline.
@@ -90,6 +98,21 @@ namespace Timeline.Services
Task<List<TimelinePostInfo>> GetPosts(string name);
/// <summary>
+ /// Get the data of a post.
+ /// </summary>
+ /// <param name="name">Username or the timeline name. See remarks of <see cref="IBaseTimelineService"/>.</param>
+ /// <param name="postId">The id of the post.</param>
+ /// <returns>The data and its type.</returns>
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="name"/> is null.</exception>
+ /// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service).</exception>
+ /// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist.</exception>
+ /// <exception cref="InvalidOperationException">Thrown when post has no data. See remarks.</exception>
+ /// <remarks>
+ /// Use this method to retrieve the image of image post.
+ /// </remarks>
+ Task<DataWithType> GetPostData(string name, long postId);
+
+ /// <summary>
/// Create a new text post in timeline.
/// </summary>
/// <param name="name">Username or the timeline name. See remarks of <ssee cref="IBaseTimelineService"/>.</param>
@@ -307,10 +330,12 @@ namespace Timeline.Services
public abstract class BaseTimelineService : IBaseTimelineService
{
- protected BaseTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IUserService userService, IMapper mapper, IClock clock)
+ protected BaseTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IImageValidator imageValidator, IDataManager dataManager, IUserService userService, IMapper mapper, IClock clock)
{
Clock = clock;
Database = database;
+ ImageValidator = imageValidator;
+ DataManager = dataManager;
UserService = userService;
Mapper = mapper;
}
@@ -321,6 +346,8 @@ namespace Timeline.Services
protected DatabaseContext Database { get; }
+ protected IImageValidator ImageValidator { get; }
+ protected IDataManager DataManager { get; }
protected IUserService UserService { get; }
protected IMapper Mapper { get; }
@@ -451,7 +478,54 @@ namespace Timeline.Services
return new TimelinePostInfo
{
Id = postEntity.LocalId,
- Content = text,
+ Content = new TextTimelinePostContent(text),
+ Author = author,
+ Time = finalTime,
+ LastUpdated = currentTime
+ };
+ }
+
+ public async Task<TimelinePostInfo> CreateImagePost(string name, long authorId, byte[] data, DateTime? time)
+ {
+ if (name == null)
+ throw new ArgumentNullException(nameof(name));
+ if (data == null)
+ throw new ArgumentNullException(nameof(data));
+
+ var timelineId = await FindTimelineId(name);
+ var timelineEntity = await Database.Timelines.Where(t => t.Id == timelineId).SingleAsync();
+
+ var author = Mapper.Map<UserInfo>(await UserService.GetUserById(authorId));
+
+ var imageFormat = await ImageValidator.Validate(data);
+
+ var imageFormatText = imageFormat.DefaultMimeType;
+
+ var tag = await DataManager.RetainEntry(data);
+
+ var currentTime = Clock.GetCurrentTime();
+ var finalTime = time ?? currentTime;
+
+ timelineEntity.CurrentPostLocalId += 1;
+
+ var postEntity = new TimelinePostEntity
+ {
+ LocalId = timelineEntity.CurrentPostLocalId,
+ ContentType = TimelinePostContentTypes.Image,
+ Content = tag,
+ ExtraContent = imageFormatText,
+ AuthorId = authorId,
+ TimelineId = timelineId,
+ Time = finalTime,
+ LastUpdated = currentTime
+ };
+ Database.TimelinePosts.Add(postEntity);
+ await Database.SaveChangesAsync();
+
+ return new TimelinePostInfo
+ {
+ Id = postEntity.LocalId,
+ Content = new ImageTimelinePostContent(tag),
Author = author,
Time = finalTime,
LastUpdated = currentTime
@@ -658,8 +732,8 @@ namespace Timeline.Services
}
}
- public TimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IUserService userService, IMapper mapper, IClock clock)
- : base(loggerFactory, database, userService, mapper, clock)
+ public TimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IImageValidator imageValidator, IDataManager dataManager, IUserService userService, IMapper mapper, IClock clock)
+ : base(loggerFactory, database, imageValidator, dataManager, userService, mapper, clock)
{
}
@@ -788,8 +862,8 @@ namespace Timeline.Services
public class PersonalTimelineService : BaseTimelineService, IPersonalTimelineService
{
- public PersonalTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IUserService userService, IMapper mapper, IClock clock)
- : base(loggerFactory, database, userService, mapper, clock)
+ public PersonalTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IImageValidator imageValidator, IDataManager dataManager, IUserService userService, IMapper mapper, IClock clock)
+ : base(loggerFactory, database, imageValidator, dataManager, userService, mapper, clock)
{
}