From 52acf41e331ddbd66befed4692c804b754ba7d5c Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 30 Jan 2020 20:26:52 +0800 Subject: ... --- Timeline/Models/Http/ErrorResponse.cs | 18 +++++----- Timeline/Models/Http/Timeline.cs | 42 ---------------------- Timeline/Models/Http/TimelineCommon.cs | 44 +++++++++++++++++++++++ Timeline/Models/Http/TimelineController.cs | 20 +++++++++++ Timeline/Models/Http/Token.cs | 32 ----------------- Timeline/Models/Http/TokenController.cs | 32 +++++++++++++++++ Timeline/Models/Http/UserController.cs | 26 ++++++++++++++ Timeline/Models/Http/UserInfo.cs | 58 ++++++++++++++++++++++++++++++ 8 files changed, 189 insertions(+), 83 deletions(-) delete mode 100644 Timeline/Models/Http/Timeline.cs create mode 100644 Timeline/Models/Http/TimelineCommon.cs create mode 100644 Timeline/Models/Http/TimelineController.cs delete mode 100644 Timeline/Models/Http/Token.cs create mode 100644 Timeline/Models/Http/TokenController.cs create mode 100644 Timeline/Models/Http/UserInfo.cs (limited to 'Timeline/Models/Http') 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? Add { get; set; } - - public List? Remove { get; set; } - } -} diff --git a/Timeline/Models/Http/TimelineCommon.cs b/Timeline/Models/Http/TimelineCommon.cs new file mode 100644 index 00000000..febb8186 --- /dev/null +++ b/Timeline/Models/Http/TimelineCommon.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; + +namespace Timeline.Models.Http +{ + public enum TimelineVisibility + { + /// + /// All people including those without accounts. + /// + Public, + /// + /// Only people signed in. + /// + Register, + /// + /// Only member. + /// + Private + } + + public class TimelinePostInfo + { + public long Id { get; set; } + public string Content { get; set; } = default!; + public DateTime Time { get; set; } + 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; } = default!; + public UserInfo Owner { get; set; } = default!; + public TimelineVisibility Visibility { get; set; } + public List Members { get; set; } = default!; + } + + public class TimelineInfo : BaseTimelineInfo + { + public string Name { get; set; } = default!; + } +} 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/Token.cs deleted file mode 100644 index 0649f1d1..00000000 --- a/Timeline/Models/Http/Token.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Timeline.Models.Http -{ - public class CreateTokenRequest - { - [Required] - public string Username { get; set; } = default!; - [Required] - public string Password { get; set; } = default!; - // in days, optional - [Range(1, 365)] - public int? Expire { get; set; } - } - - public class CreateTokenResponse - { - public string Token { get; set; } = default!; - public User User { get; set; } = default!; - } - - public class VerifyTokenRequest - { - [Required] - public string Token { get; set; } = default!; - } - - public class VerifyTokenResponse - { - public User User { get; set; } = default!; - } -} diff --git a/Timeline/Models/Http/TokenController.cs b/Timeline/Models/Http/TokenController.cs new file mode 100644 index 00000000..383b2965 --- /dev/null +++ b/Timeline/Models/Http/TokenController.cs @@ -0,0 +1,32 @@ +using System.ComponentModel.DataAnnotations; + +namespace Timeline.Models.Http +{ + public class CreateTokenRequest + { + [Required] + public string Username { get; set; } = default!; + [Required] + public string Password { get; set; } = default!; + // in days, optional + [Range(1, 365)] + public int? Expire { get; set; } + } + + public class CreateTokenResponse + { + public string Token { get; set; } = default!; + public UserInfoForAdmin User { get; set; } = default!; + } + + public class VerifyTokenRequest + { + [Required] + public string Token { get; set; } = default!; + } + + public class VerifyTokenResponse + { + 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(MemberList.Source); + CreateMap(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 + { + 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().AfterMap(); + CreateMap().AfterMap(); + } + } +} -- cgit v1.2.3