aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline/Models')
-rw-r--r--Timeline/Models/Converters/JsonDateTimeConverter.cs1
-rw-r--r--Timeline/Models/Http/ErrorResponse.cs18
-rw-r--r--Timeline/Models/Http/Timeline.cs42
-rw-r--r--Timeline/Models/Http/TimelineCommon.cs (renamed from Timeline/Models/Timeline.cs)25
-rw-r--r--Timeline/Models/Http/TimelineController.cs20
-rw-r--r--Timeline/Models/Http/TokenController.cs (renamed from Timeline/Models/Http/Token.cs)4
-rw-r--r--Timeline/Models/Http/UserController.cs26
-rw-r--r--Timeline/Models/Http/UserInfo.cs58
-rw-r--r--Timeline/Models/Validation/NicknameValidator.cs1
-rw-r--r--Timeline/Models/Validation/UsernameValidator.cs1
10 files changed, 122 insertions, 74 deletions
diff --git a/Timeline/Models/Converters/JsonDateTimeConverter.cs b/Timeline/Models/Converters/JsonDateTimeConverter.cs
index 69af53c1..ef129a01 100644
--- a/Timeline/Models/Converters/JsonDateTimeConverter.cs
+++ b/Timeline/Models/Converters/JsonDateTimeConverter.cs
@@ -6,7 +6,6 @@ using System.Text.Json.Serialization;
namespace Timeline.Models.Converters
{
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
public class JsonDateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
diff --git a/Timeline/Models/Http/ErrorResponse.cs b/Timeline/Models/Http/ErrorResponse.cs
index 6a53e0c3..87516638 100644
--- a/Timeline/Models/Http/ErrorResponse.cs
+++ b/Timeline/Models/Http/ErrorResponse.cs
@@ -184,14 +184,14 @@ namespace Timeline.Models.Http
public static class UserController
{
- public static CommonResponse ChangeUsername_Conflict(params object?[] formatArgs)
+ public static CommonResponse UsernameConflict(params object?[] formatArgs)
{
- return new CommonResponse(ErrorCodes.UserController.ChangeUsername_Conflict, string.Format(UserController_ChangeUsername_Conflict, formatArgs));
+ return new CommonResponse(ErrorCodes.UserController.UsernameConflict, string.Format(UserController_UsernameConflict, formatArgs));
}
- public static CommonResponse CustomMessage_ChangeUsername_Conflict(string message, params object?[] formatArgs)
+ public static CommonResponse CustomMessage_UsernameConflict(string message, params object?[] formatArgs)
{
- return new CommonResponse(ErrorCodes.UserController.ChangeUsername_Conflict, string.Format(message, formatArgs));
+ return new CommonResponse(ErrorCodes.UserController.UsernameConflict, string.Format(message, formatArgs));
}
public static CommonResponse ChangePassword_BadOldPassword(params object?[] formatArgs)
@@ -244,18 +244,18 @@ namespace Timeline.Models.Http
public static class TimelineController
{
- public static CommonResponse PostOperationDelete_NotExist(params object?[] formatArgs)
+ public static CommonResponse MemberPut_NotExist(params object?[] formatArgs)
{
- return new CommonResponse(ErrorCodes.TimelineController.PostOperationDelete_NotExist, string.Format(TimelineController_PostOperationDelete_NotExist, formatArgs));
+ return new CommonResponse(ErrorCodes.TimelineController.MemberPut_NotExist, string.Format(TimelineController_MemberPut_NotExist, formatArgs));
}
- public static CommonResponse CustomMessage_PostOperationDelete_NotExist(string message, params object?[] formatArgs)
+ public static CommonResponse CustomMessage_MemberPut_NotExist(string message, params object?[] formatArgs)
{
- return new CommonResponse(ErrorCodes.TimelineController.PostOperationDelete_NotExist, string.Format(message, formatArgs));
+ return new CommonResponse(ErrorCodes.TimelineController.MemberPut_NotExist, string.Format(message, formatArgs));
}
}
}
-}
+} \ No newline at end of file
diff --git a/Timeline/Models/Http/Timeline.cs b/Timeline/Models/Http/Timeline.cs
deleted file mode 100644
index 3029434e..00000000
--- a/Timeline/Models/Http/Timeline.cs
+++ /dev/null
@@ -1,42 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.ComponentModel.DataAnnotations;
-
-namespace Timeline.Models.Http
-{
- public class TimelinePostCreateRequest
- {
- [Required(AllowEmptyStrings = true)]
- public string Content { get; set; } = default!;
-
- public DateTime? Time { get; set; }
- }
-
- public class TimelinePostCreateResponse
- {
- public long Id { get; set; }
-
- public DateTime Time { get; set; }
- }
-
- public class TimelinePostDeleteRequest
- {
- [Required]
- public long? Id { get; set; }
- }
-
- public class TimelinePropertyChangeRequest
- {
- public string? Description { get; set; }
-
- public TimelineVisibility? Visibility { get; set; }
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "This is a DTO class.")]
- public class TimelineMemberChangeRequest
- {
- public List<string>? Add { get; set; }
-
- public List<string>? Remove { get; set; }
- }
-}
diff --git a/Timeline/Models/Timeline.cs b/Timeline/Models/Http/TimelineCommon.cs
index 752c698d..febb8186 100644
--- a/Timeline/Models/Timeline.cs
+++ b/Timeline/Models/Http/TimelineCommon.cs
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
-namespace Timeline.Models
+namespace Timeline.Models.Http
{
public enum TimelineVisibility
{
@@ -22,30 +22,19 @@ namespace Timeline.Models
public class TimelinePostInfo
{
public long Id { get; set; }
-
- public string? Content { get; set; }
-
+ public string Content { get; set; } = default!;
public DateTime Time { get; set; }
-
- /// <summary>
- /// The username of the author.
- /// </summary>
- public string Author { get; set; } = default!;
+ public UserInfo Author { get; set; } = default!;
+ public DateTime LastUpdated { get; set; } = default!;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "This is a DTO class.")]
public class BaseTimelineInfo
{
- public string? Description { get; set; }
-
- /// <summary>
- /// The username of the owner.
- /// </summary>
- public string Owner { get; set; } = default!;
-
+ public string Description { get; set; } = default!;
+ public UserInfo Owner { get; set; } = default!;
public TimelineVisibility Visibility { get; set; }
-
- public List<string> Members { get; set; } = default!;
+ public List<UserInfo> Members { get; set; } = default!;
}
public class TimelineInfo : BaseTimelineInfo
diff --git a/Timeline/Models/Http/TimelineController.cs b/Timeline/Models/Http/TimelineController.cs
new file mode 100644
index 00000000..f9a4d3e5
--- /dev/null
+++ b/Timeline/Models/Http/TimelineController.cs
@@ -0,0 +1,20 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+
+namespace Timeline.Models.Http
+{
+ public class TimelinePostCreateRequest
+ {
+ [Required(AllowEmptyStrings = true)]
+ public string Content { get; set; } = default!;
+
+ public DateTime? Time { get; set; }
+ }
+
+ public class TimelinePatchRequest
+ {
+ public string? Description { get; set; }
+
+ public TimelineVisibility? Visibility { get; set; }
+ }
+}
diff --git a/Timeline/Models/Http/Token.cs b/Timeline/Models/Http/TokenController.cs
index 0649f1d1..383b2965 100644
--- a/Timeline/Models/Http/Token.cs
+++ b/Timeline/Models/Http/TokenController.cs
@@ -16,7 +16,7 @@ namespace Timeline.Models.Http
public class CreateTokenResponse
{
public string Token { get; set; } = default!;
- public User User { get; set; } = default!;
+ public UserInfoForAdmin User { get; set; } = default!;
}
public class VerifyTokenRequest
@@ -27,6 +27,6 @@ namespace Timeline.Models.Http
public class VerifyTokenResponse
{
- public User User { get; set; } = default!;
+ public UserInfoForAdmin User { get; set; } = default!;
}
}
diff --git a/Timeline/Models/Http/UserController.cs b/Timeline/Models/Http/UserController.cs
index 229ca1e5..e4c95cbd 100644
--- a/Timeline/Models/Http/UserController.cs
+++ b/Timeline/Models/Http/UserController.cs
@@ -1,5 +1,7 @@
+using AutoMapper;
using System.ComponentModel.DataAnnotations;
using Timeline.Models.Validation;
+using Timeline.Services;
namespace Timeline.Models.Http
{
@@ -17,6 +19,21 @@ namespace Timeline.Models.Http
public bool? Administrator { get; set; }
}
+ public class CreateUserRequest
+ {
+ [Required, Username]
+ public string Username { get; set; } = default!;
+
+ [Required, MinLength(1)]
+ public string Password { get; set; } = default!;
+
+ [Required]
+ public bool? Administrator { get; set; }
+
+ [Nickname]
+ public string? Nickname { get; set; }
+ }
+
public class ChangePasswordRequest
{
[Required(AllowEmptyStrings = false)]
@@ -24,4 +41,13 @@ namespace Timeline.Models.Http
[Required(AllowEmptyStrings = false)]
public string NewPassword { get; set; } = default!;
}
+
+ public class UserControllerAutoMapperProfile : Profile
+ {
+ public UserControllerAutoMapperProfile()
+ {
+ CreateMap<UserPatchRequest, User>(MemberList.Source);
+ CreateMap<CreateUserRequest, User>(MemberList.Source);
+ }
+ }
}
diff --git a/Timeline/Models/Http/UserInfo.cs b/Timeline/Models/Http/UserInfo.cs
new file mode 100644
index 00000000..6029b8aa
--- /dev/null
+++ b/Timeline/Models/Http/UserInfo.cs
@@ -0,0 +1,58 @@
+using AutoMapper;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Infrastructure;
+using Microsoft.AspNetCore.Mvc.Routing;
+using Timeline.Controllers;
+using Timeline.Services;
+
+namespace Timeline.Models.Http
+{
+ public interface IUserInfo
+ {
+ string Username { get; set; }
+ string Nickname { get; set; }
+ string AvatarUrl { get; set; }
+ }
+
+ public class UserInfo : IUserInfo
+ {
+ public string Username { get; set; } = default!;
+ public string Nickname { get; set; } = default!;
+ public string AvatarUrl { get; set; } = default!;
+ }
+
+ public class UserInfoForAdmin : IUserInfo
+ {
+ public string Username { get; set; } = default!;
+ public string Nickname { get; set; } = default!;
+ public string AvatarUrl { get; set; } = default!;
+ public bool Administrator { get; set; }
+ }
+
+ public class UserInfoSetAvatarUrlAction : IMappingAction<object, IUserInfo>
+ {
+ private readonly IActionContextAccessor _actionContextAccessor;
+ private readonly IUrlHelperFactory _urlHelperFactory;
+
+ public UserInfoSetAvatarUrlAction(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory)
+ {
+ _actionContextAccessor = actionContextAccessor;
+ _urlHelperFactory = urlHelperFactory;
+ }
+
+ public void Process(object source, IUserInfo destination, ResolutionContext context)
+ {
+ var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
+ destination.AvatarUrl = urlHelper.ActionLink(nameof(UserAvatarController.Get), nameof(UserAvatarController), new { destination.Username });
+ }
+ }
+
+ public class UserInfoAutoMapperProfile : Profile
+ {
+ public UserInfoAutoMapperProfile()
+ {
+ CreateMap<User, UserInfo>().AfterMap<UserInfoSetAvatarUrlAction>();
+ CreateMap<User, UserInfoForAdmin>().AfterMap<UserInfoSetAvatarUrlAction>();
+ }
+ }
+}
diff --git a/Timeline/Models/Validation/NicknameValidator.cs b/Timeline/Models/Validation/NicknameValidator.cs
index f6626a2a..53a2916b 100644
--- a/Timeline/Models/Validation/NicknameValidator.cs
+++ b/Timeline/Models/Validation/NicknameValidator.cs
@@ -5,7 +5,6 @@ namespace Timeline.Models.Validation
{
public class NicknameValidator : Validator<string>
{
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Already checked in base.")]
protected override (bool, string) DoValidate(string value)
{
if (value.Length > 10)
diff --git a/Timeline/Models/Validation/UsernameValidator.cs b/Timeline/Models/Validation/UsernameValidator.cs
index fc6cdf37..d8f3bdc0 100644
--- a/Timeline/Models/Validation/UsernameValidator.cs
+++ b/Timeline/Models/Validation/UsernameValidator.cs
@@ -8,7 +8,6 @@ namespace Timeline.Models.Validation
{
public const int MaxLength = 26;
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Already checked in base class.")]
protected override (bool, string) DoValidate(string value)
{
if (value.Length == 0)