diff options
author | crupest <crupest@outlook.com> | 2021-04-29 19:17:03 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-29 19:17:03 +0800 |
commit | af5bab651a56a04468654c06057ab6d5ee28f18b (patch) | |
tree | 9d5a169be5aff26a5224c1997b4d47ba486a550a /BackEnd/Timeline/Services | |
parent | 2075e4098025001073b5eca025632cbb3199c316 (diff) | |
download | timeline-af5bab651a56a04468654c06057ab6d5ee28f18b.tar.gz timeline-af5bab651a56a04468654c06057ab6d5ee28f18b.tar.bz2 timeline-af5bab651a56a04468654c06057ab6d5ee28f18b.zip |
refactor: ...
Diffstat (limited to 'BackEnd/Timeline/Services')
7 files changed, 222 insertions, 20 deletions
diff --git a/BackEnd/Timeline/Services/Timeline/Resource.Designer.cs b/BackEnd/Timeline/Services/Timeline/Resource.Designer.cs index 6faea295..5ad03011 100644 --- a/BackEnd/Timeline/Services/Timeline/Resource.Designer.cs +++ b/BackEnd/Timeline/Services/Timeline/Resource.Designer.cs @@ -61,6 +61,42 @@ namespace Timeline.Services.Timeline { }
/// <summary>
+ /// Looks up a localized string similar to Color is not valid. {0}.
+ /// </summary>
+ internal static string ExceptionColorInvalid {
+ get {
+ return ResourceManager.GetString("ExceptionColorInvalid", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Data list can't be empty..
+ /// </summary>
+ internal static string ExceptionDataListEmpty {
+ get {
+ return ResourceManager.GetString("ExceptionDataListEmpty", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Data list can't be null..
+ /// </summary>
+ internal static string ExceptionDataListNull {
+ get {
+ return ResourceManager.GetString("ExceptionDataListNull", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Data list count can't be bigger than 100..
+ /// </summary>
+ internal static string ExceptionDataListTooLarge {
+ get {
+ return ResourceManager.GetString("ExceptionDataListTooLarge", resourceCulture);
+ }
+ }
+
+ /// <summary>
/// Looks up a localized string similar to This timeline name is neither a valid personal timeline name nor a valid ordinary timeline name. {0}.
/// </summary>
internal static string ExceptionGeneralTimelineNameBadFormat {
@@ -70,6 +106,33 @@ namespace Timeline.Services.Timeline { }
/// <summary>
+ /// Looks up a localized string similar to Image validation failed..
+ /// </summary>
+ internal static string ExceptionPostDataImageInvalid {
+ get {
+ return ResourceManager.GetString("ExceptionPostDataImageInvalid", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to It is not a valid utf-8 sequence..
+ /// </summary>
+ internal static string ExceptionPostDataNotValidUtf8 {
+ get {
+ return ResourceManager.GetString("ExceptionPostDataNotValidUtf8", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Unsupported content type..
+ /// </summary>
+ internal static string ExceptionPostDataUnsupportedType {
+ get {
+ return ResourceManager.GetString("ExceptionPostDataUnsupportedType", resourceCulture);
+ }
+ }
+
+ /// <summary>
/// Looks up a localized string similar to Timeline with given constraints already exist..
/// </summary>
internal static string ExceptionTimelineAlreadyExist {
@@ -131,5 +194,77 @@ namespace Timeline.Services.Timeline { return ResourceManager.GetString("LogPersonalTimelineAutoCreate", resourceCulture);
}
}
+
+ /// <summary>
+ /// Looks up a localized string similar to A member(user id={0}) is added to timeline(id={1})..
+ /// </summary>
+ internal static string LogTimelineAddMember {
+ get {
+ return ResourceManager.GetString("LogTimelineAddMember", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to A timeline is created with name={0}, id={1}..
+ /// </summary>
+ internal static string LogTimelineCreate {
+ get {
+ return ResourceManager.GetString("LogTimelineCreate", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to A timeline(id={0}) is deleted..
+ /// </summary>
+ internal static string LogTimelineDelete {
+ get {
+ return ResourceManager.GetString("LogTimelineDelete", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to A timeline(id={0}) post(id={1}) is created..
+ /// </summary>
+ internal static string LogTimelinePostCreated {
+ get {
+ return ResourceManager.GetString("LogTimelinePostCreated", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to A timeline(id={0}) post(id={1}) is deleted..
+ /// </summary>
+ internal static string LogTimelinePostDeleted {
+ get {
+ return ResourceManager.GetString("LogTimelinePostDeleted", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to A timeline(id={0}) post(id={1}) is updated..
+ /// </summary>
+ internal static string LogTimelinePostUpdated {
+ get {
+ return ResourceManager.GetString("LogTimelinePostUpdated", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to A member(user id={0}) is removed from timeline(id={1})..
+ /// </summary>
+ internal static string LogTimelineRemoveMember {
+ get {
+ return ResourceManager.GetString("LogTimelineRemoveMember", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Timeline with id={0} is updated..
+ /// </summary>
+ internal static string LogTimelineUpdated {
+ get {
+ return ResourceManager.GetString("LogTimelineUpdated", resourceCulture);
+ }
+ }
}
}
diff --git a/BackEnd/Timeline/Services/Timeline/Resource.resx b/BackEnd/Timeline/Services/Timeline/Resource.resx index 3a233e55..cc293d05 100644 --- a/BackEnd/Timeline/Services/Timeline/Resource.resx +++ b/BackEnd/Timeline/Services/Timeline/Resource.resx @@ -117,9 +117,30 @@ <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
+ <data name="ExceptionColorInvalid" xml:space="preserve">
+ <value>Color is not valid. {0}</value>
+ </data>
+ <data name="ExceptionDataListEmpty" xml:space="preserve">
+ <value>Data list can't be empty.</value>
+ </data>
+ <data name="ExceptionDataListNull" xml:space="preserve">
+ <value>Data list can't be null.</value>
+ </data>
+ <data name="ExceptionDataListTooLarge" xml:space="preserve">
+ <value>Data list count can't be bigger than 100.</value>
+ </data>
<data name="ExceptionGeneralTimelineNameBadFormat" xml:space="preserve">
<value>This timeline name is neither a valid personal timeline name nor a valid ordinary timeline name. {0}</value>
</data>
+ <data name="ExceptionPostDataImageInvalid" xml:space="preserve">
+ <value>Image validation failed.</value>
+ </data>
+ <data name="ExceptionPostDataNotValidUtf8" xml:space="preserve">
+ <value>It is not a valid utf-8 sequence.</value>
+ </data>
+ <data name="ExceptionPostDataUnsupportedType" xml:space="preserve">
+ <value>Unsupported content type.</value>
+ </data>
<data name="ExceptionTimelineAlreadyExist" xml:space="preserve">
<value>Timeline with given constraints already exist.</value>
</data>
@@ -141,4 +162,28 @@ <data name="LogPersonalTimelineAutoCreate" xml:space="preserve">
<value>A personal timeline for user with username={0} is created automatically.</value>
</data>
+ <data name="LogTimelineAddMember" xml:space="preserve">
+ <value>A member(user id={0}) is added to timeline(id={1}).</value>
+ </data>
+ <data name="LogTimelineCreate" xml:space="preserve">
+ <value>A timeline is created with name={0}, id={1}.</value>
+ </data>
+ <data name="LogTimelineDelete" xml:space="preserve">
+ <value>A timeline(id={0}) is deleted.</value>
+ </data>
+ <data name="LogTimelinePostCreated" xml:space="preserve">
+ <value>A timeline(id={0}) post(id={1}) is created.</value>
+ </data>
+ <data name="LogTimelinePostDeleted" xml:space="preserve">
+ <value>A timeline(id={0}) post(id={1}) is deleted.</value>
+ </data>
+ <data name="LogTimelinePostUpdated" xml:space="preserve">
+ <value>A timeline(id={0}) post(id={1}) is updated.</value>
+ </data>
+ <data name="LogTimelineRemoveMember" xml:space="preserve">
+ <value>A member(user id={0}) is removed from timeline(id={1}).</value>
+ </data>
+ <data name="LogTimelineUpdated" xml:space="preserve">
+ <value>Timeline with id={0} is updated.</value>
+ </data>
</root>
\ No newline at end of file diff --git a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs index 65a01b37..a9bb7bd1 100644 --- a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs @@ -50,6 +50,12 @@ namespace Timeline.Services.Timeline throw new UserNotExistException(userId);
}
+ private void CheckColor(string color, string paramName)
+ {
+ if (!_colorValidator.Validate(color, out var message))
+ throw new ArgumentException(string.Format(Resource.ExceptionColorInvalid, message), paramName);
+ }
+
public async Task<List<TimelinePostEntity>> GetPostsAsync(long timelineId, DateTime? modifiedSince = null, bool includeDeleted = false)
{
await CheckTimelineExistence(timelineId);
@@ -139,19 +145,17 @@ namespace Timeline.Services.Timeline if (request is null)
throw new ArgumentNullException(nameof(request));
- {
- if (!_colorValidator.Validate(request.Color, out var message))
- throw new ArgumentException("Color is not valid.", nameof(request));
- }
+ if (request.Color is not null)
+ CheckColor(request.Color, nameof(request));
if (request.DataList is null)
- throw new ArgumentException("Data list can't be null.", nameof(request));
+ throw new ArgumentException(Resource.ExceptionDataListNull, nameof(request));
if (request.DataList.Count == 0)
- throw new ArgumentException("Data list can't be empty.", nameof(request));
+ throw new ArgumentException(Resource.ExceptionDataListEmpty, nameof(request));
if (request.DataList.Count > 100)
- throw new ArgumentException("Data list count can't be bigger than 100.", nameof(request));
+ throw new ArgumentException(Resource.ExceptionDataListTooLarge, nameof(request));
for (int index = 0; index < request.DataList.Count; index++)
{
@@ -169,7 +173,7 @@ namespace Timeline.Services.Timeline }
catch (ImageException e)
{
- throw new TimelinePostCreateDataException(index, "Image validation failed.", e);
+ throw new TimelinePostCreateDataException(index, Resource.ExceptionPostDataImageInvalid, e);
}
break;
case MimeTypes.TextPlain:
@@ -180,11 +184,11 @@ namespace Timeline.Services.Timeline }
catch (DecoderFallbackException e)
{
- throw new TimelinePostCreateDataException(index, "Text is not a valid utf-8 sequence.", e);
+ throw new TimelinePostCreateDataException(index, Resource.ExceptionPostDataNotValidUtf8, e);
}
break;
default:
- throw new TimelinePostCreateDataException(index, "Unsupported content type.");
+ throw new TimelinePostCreateDataException(index, Resource.ExceptionPostDataUnsupportedType);
}
}
@@ -234,6 +238,7 @@ namespace Timeline.Services.Timeline await _database.SaveChangesAsync();
await transaction.CommitAsync();
+ _logger.LogInformation(Resource.LogTimelinePostCreated, timelineId, postEntity.Id);
return postEntity;
}
@@ -243,10 +248,8 @@ namespace Timeline.Services.Timeline if (request is null)
throw new ArgumentNullException(nameof(request));
- {
- if (!_colorValidator.Validate(request.Color, out var message))
- throw new ArgumentException("Color is not valid.", nameof(request));
- }
+ if (request.Color is not null)
+ CheckColor(request.Color, nameof(request));
request.Time = request.Time?.MyToUtc();
@@ -269,6 +272,7 @@ namespace Timeline.Services.Timeline entity.LastUpdated = _clock.GetCurrentTime();
await _database.SaveChangesAsync();
+ _logger.LogInformation(Resource.LogTimelinePostUpdated, timelineId, postId);
return entity;
}
@@ -302,6 +306,7 @@ namespace Timeline.Services.Timeline await _database.SaveChangesAsync();
await transaction.CommitAsync();
+ _logger.LogWarning(Resource.LogTimelinePostDeleted, timelineId, postId);
}
public async Task DeleteAllPostsOfUserAsync(long userId)
diff --git a/BackEnd/Timeline/Services/Timeline/TimelineService.cs b/BackEnd/Timeline/Services/Timeline/TimelineService.cs index cea93272..bde0210a 100644 --- a/BackEnd/Timeline/Services/Timeline/TimelineService.cs +++ b/BackEnd/Timeline/Services/Timeline/TimelineService.cs @@ -23,7 +23,6 @@ namespace Timeline.Services.Timeline private readonly IClock _clock;
private readonly TimelineNameValidator _timelineNameValidator = new TimelineNameValidator();
-
private readonly ColorValidator _colorValidator = new ColorValidator();
public TimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IBasicUserService userService, IClock clock)
@@ -36,7 +35,7 @@ namespace Timeline.Services.Timeline }
- private void ValidateTimelineName(string name, string paramName)
+ private void CheckTimelineName(string name, string paramName)
{
if (!_timelineNameValidator.Validate(name, out var message))
{
@@ -60,7 +59,7 @@ namespace Timeline.Services.Timeline throw new ArgumentNullException(nameof(newProperties));
if (newProperties.Name is not null)
- ValidateTimelineName(newProperties.Name, nameof(newProperties));
+ CheckTimelineName(newProperties.Name, nameof(newProperties));
if (newProperties.Color is not null)
{
@@ -125,6 +124,7 @@ namespace Timeline.Services.Timeline }
await _database.SaveChangesAsync();
+ _logger.LogInformation(Resource.LogTimelineUpdated, id);
}
public async Task<bool> AddMemberAsync(long timelineId, long userId)
@@ -138,7 +138,6 @@ namespace Timeline.Services.Timeline if (await _database.TimelineMembers.AnyAsync(m => m.TimelineId == timelineId && m.UserId == userId))
return false;
-
var entity = new TimelineMemberEntity { UserId = userId, TimelineId = timelineId };
_database.TimelineMembers.Add(entity);
@@ -146,6 +145,8 @@ namespace Timeline.Services.Timeline timelineEntity.LastModified = _clock.GetCurrentTime();
await _database.SaveChangesAsync();
+ _logger.LogInformation(Resource.LogTimelineAddMember, userId, timelineId);
+
return true;
}
@@ -166,6 +167,8 @@ namespace Timeline.Services.Timeline timelineEntity.LastModified = _clock.GetCurrentTime();
await _database.SaveChangesAsync();
+ _logger.LogInformation(Resource.LogTimelineRemoveMember, userId, timelineId);
+
return true;
}
@@ -258,7 +261,7 @@ namespace Timeline.Services.Timeline if (name == null)
throw new ArgumentNullException(nameof(name));
- ValidateTimelineName(name, nameof(name));
+ CheckTimelineName(name, nameof(name));
var conflict = await _database.Timelines.AnyAsync(t => t.Name == name);
@@ -269,6 +272,7 @@ namespace Timeline.Services.Timeline _database.Timelines.Add(entity);
await _database.SaveChangesAsync();
+ _logger.LogInformation(Resource.LogTimelineCreate, name, entity.Id);
return entity;
}
@@ -282,6 +286,7 @@ namespace Timeline.Services.Timeline _database.Timelines.Remove(entity);
await _database.SaveChangesAsync();
+ _logger.LogWarning(Resource.LogTimelineDelete, id);
}
}
}
diff --git a/BackEnd/Timeline/Services/Token/Resource.Designer.cs b/BackEnd/Timeline/Services/Token/Resource.Designer.cs index 07b0057f..ac6f3707 100644 --- a/BackEnd/Timeline/Services/Token/Resource.Designer.cs +++ b/BackEnd/Timeline/Services/Token/Resource.Designer.cs @@ -61,6 +61,15 @@ namespace Timeline.Services.Token { }
/// <summary>
+ /// Looks up a localized string similar to Jwt key is not found. Maybe you forget to do the migration..
+ /// </summary>
+ internal static string ExceptionJwtKeyNotExist {
+ get {
+ return ResourceManager.GetString("ExceptionJwtKeyNotExist", resourceCulture);
+ }
+ }
+
+ /// <summary>
/// Looks up a localized string similar to The token didn't pass verification because {0}..
/// </summary>
internal static string ExceptionJwtUserTokenBadFormat {
diff --git a/BackEnd/Timeline/Services/Token/Resource.resx b/BackEnd/Timeline/Services/Token/Resource.resx index 7abf2e75..06bf03f6 100644 --- a/BackEnd/Timeline/Services/Token/Resource.resx +++ b/BackEnd/Timeline/Services/Token/Resource.resx @@ -117,6 +117,9 @@ <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
+ <data name="ExceptionJwtKeyNotExist" xml:space="preserve">
+ <value>Jwt key is not found. Maybe you forget to do the migration.</value>
+ </data>
<data name="ExceptionJwtUserTokenBadFormat" xml:space="preserve">
<value>The token didn't pass verification because {0}.</value>
</data>
diff --git a/BackEnd/Timeline/Services/Token/UserTokenHandler.cs b/BackEnd/Timeline/Services/Token/UserTokenHandler.cs index 7b57a06c..c1633f4a 100644 --- a/BackEnd/Timeline/Services/Token/UserTokenHandler.cs +++ b/BackEnd/Timeline/Services/Token/UserTokenHandler.cs @@ -29,7 +29,7 @@ namespace Timeline.Services.Token if (key == null)
{
- throw new InvalidOperationException(Resources.Services.UserTokenService.JwtKeyNotExist);
+ throw new InvalidOperationException(Resource.ExceptionJwtKeyNotExist);
}
_tokenSecurityKey = new SymmetricSecurityKey(key);
|