diff options
author | crupest <crupest@outlook.com> | 2020-03-10 03:04:23 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-03-10 03:04:23 +0800 |
commit | e7ca87f25dae2f806469043ee556d4790d9ebcae (patch) | |
tree | 17d64590d81fc02a2b8e3df88963288b0ca85bb7 /Timeline/Models | |
parent | 893ed4eee63997f2d91d6715d4355d9f767b2c40 (diff) | |
download | timeline-e7ca87f25dae2f806469043ee556d4790d9ebcae.tar.gz timeline-e7ca87f25dae2f806469043ee556d4790d9ebcae.tar.bz2 timeline-e7ca87f25dae2f806469043ee556d4790d9ebcae.zip |
...
Diffstat (limited to 'Timeline/Models')
-rw-r--r-- | Timeline/Models/Converters/JsonDateTimeConverter.cs | 2 | ||||
-rw-r--r-- | Timeline/Models/Http/Common.cs | 4 | ||||
-rw-r--r-- | Timeline/Models/Http/ErrorResponse.cs | 4 | ||||
-rw-r--r-- | Timeline/Models/Http/Timeline.cs | 119 | ||||
-rw-r--r-- | Timeline/Models/Http/TimelineCommon.cs | 116 | ||||
-rw-r--r-- | Timeline/Models/Http/TimelineController.cs | 4 | ||||
-rw-r--r-- | Timeline/Models/Http/TokenController.cs | 2 | ||||
-rw-r--r-- | Timeline/Models/Http/UserController.cs | 6 | ||||
-rw-r--r-- | Timeline/Models/Http/UserInfo.cs | 18 | ||||
-rw-r--r-- | Timeline/Models/Timeline.cs | 74 | ||||
-rw-r--r-- | Timeline/Models/User.cs | 18 | ||||
-rw-r--r-- | Timeline/Models/Validation/NameValidator.cs | 4 | ||||
-rw-r--r-- | Timeline/Models/Validation/NicknameValidator.cs | 4 | ||||
-rw-r--r-- | Timeline/Models/Validation/TimelineNameValidator.cs | 2 | ||||
-rw-r--r-- | Timeline/Models/Validation/UsernameValidator.cs | 2 | ||||
-rw-r--r-- | Timeline/Models/Validation/Validator.cs | 6 |
16 files changed, 240 insertions, 145 deletions
diff --git a/Timeline/Models/Converters/JsonDateTimeConverter.cs b/Timeline/Models/Converters/JsonDateTimeConverter.cs index ef129a01..057186de 100644 --- a/Timeline/Models/Converters/JsonDateTimeConverter.cs +++ b/Timeline/Models/Converters/JsonDateTimeConverter.cs @@ -4,7 +4,7 @@ using System.Globalization; using System.Text.Json; using System.Text.Json.Serialization; -namespace Timeline.Models.Converters +namespace TimelineApp.Models.Converters { public class JsonDateTimeConverter : JsonConverter<DateTime> { diff --git a/Timeline/Models/Http/Common.cs b/Timeline/Models/Http/Common.cs index a9fc8a79..f30fdb0d 100644 --- a/Timeline/Models/Http/Common.cs +++ b/Timeline/Models/Http/Common.cs @@ -1,6 +1,6 @@ -using static Timeline.Resources.Models.Http.Common;
+using static TimelineApp.Resources.Models.Http.Common;
-namespace Timeline.Models.Http
+namespace TimelineApp.Models.Http
{
public class CommonResponse
{
diff --git a/Timeline/Models/Http/ErrorResponse.cs b/Timeline/Models/Http/ErrorResponse.cs index 9f7e70e1..d6cb5ca0 100644 --- a/Timeline/Models/Http/ErrorResponse.cs +++ b/Timeline/Models/Http/ErrorResponse.cs @@ -1,7 +1,7 @@
-using static Timeline.Resources.Messages;
+using static TimelineApp.Resources.Messages;
-namespace Timeline.Models.Http
+namespace TimelineApp.Models.Http
{
public static class ErrorResponse
diff --git a/Timeline/Models/Http/Timeline.cs b/Timeline/Models/Http/Timeline.cs new file mode 100644 index 00000000..144cca7c --- /dev/null +++ b/Timeline/Models/Http/Timeline.cs @@ -0,0 +1,119 @@ +using AutoMapper;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Infrastructure;
+using Microsoft.AspNetCore.Mvc.Routing;
+using System;
+using System.Collections.Generic;
+using TimelineApp.Controllers;
+
+namespace TimelineApp.Models.Http
+{
+ public class TimelinePostContentInfo
+ {
+ public string Type { get; set; } = default!;
+ public string? Text { get; set; }
+ public string? Url { get; set; }
+ }
+
+ public class TimelinePostInfo
+ {
+ public long Id { get; set; }
+ public TimelinePostContentInfo Content { get; set; } = default!;
+ public DateTime Time { get; set; }
+ public UserInfo Author { get; set; } = default!;
+ public DateTime LastUpdated { get; set; } = default!;
+ }
+
+ public class TimelineInfo
+ {
+ public string? Name { get; set; }
+ public string Description { get; set; } = default!;
+ public UserInfo Owner { get; set; } = default!;
+ public TimelineVisibility Visibility { get; set; }
+#pragma warning disable CA2227 // Collection properties should be read only
+ public List<UserInfo> Members { get; set; } = default!;
+#pragma warning restore CA2227 // Collection properties should be read only
+
+#pragma warning disable CA1707 // Identifiers should not contain underscores
+ public TimelineInfoLinks _links { get; set; } = default!;
+#pragma warning restore CA1707 // Identifiers should not contain underscores
+ }
+
+ public class TimelineInfoLinks
+ {
+ public string Self { get; set; } = default!;
+ public string Posts { get; set; } = default!;
+ }
+
+ public class TimelineInfoLinksValueResolver : IValueResolver<Timeline, TimelineInfo, TimelineInfoLinks>
+ {
+ private readonly IActionContextAccessor _actionContextAccessor;
+ private readonly IUrlHelperFactory _urlHelperFactory;
+
+ public TimelineInfoLinksValueResolver(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory)
+ {
+ _actionContextAccessor = actionContextAccessor;
+ _urlHelperFactory = urlHelperFactory;
+ }
+
+ public TimelineInfoLinks Resolve(Timeline source, TimelineInfo destination, TimelineInfoLinks destMember, ResolutionContext context)
+ {
+ if (_actionContextAccessor.ActionContext == null)
+ throw new InvalidOperationException("No action context, can't fill urls.");
+
+ var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
+
+
+ return new TimelineInfoLinks
+ {
+ Self = urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { source.Name }),
+ Posts = urlHelper.ActionLink(nameof(TimelineController.PostListGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { source.Name })
+ };
+ }
+ }
+
+ public class TimelinePostConverter : ITypeConverter<ITimelinePostContent, TimelinePostContentInfo>
+ {
+ private readonly IActionContextAccessor _actionContextAccessor;
+ private readonly IUrlHelperFactory _urlHelperFactory;
+
+ public TimelinePostConverter(IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory)
+ {
+ _actionContextAccessor = actionContextAccessor;
+ _urlHelperFactory = urlHelperFactory;
+ }
+
+ public TimelinePostContentInfo Convert(ITimelinePostContent source, TimelinePostContentInfo destination, ResolutionContext context)
+ {
+ if (_actionContextAccessor.ActionContext == null)
+ throw new InvalidOperationException("No action context, can't fill urls.");
+
+ var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
+
+ if (source is TextTimelinePostContent textContent)
+ {
+ return new TimelinePostContentInfo
+ {
+ Type = TimelinePostContentTypes.Text,
+ Text = textContent.Text
+ };
+ }
+ else if (source is ImageTimelinePostContent imageContent)
+ {
+ return new TimelinePostContentInfo
+ {
+ Type = TimelinePostContentTypes.Image,
+ Url = urlHelper.ActionLink(action: "PostDataGet", nameof(TimelineController)[0..^nameof(Controller).Length], new { source.Name })
+ };
+ }
+ }
+ }
+
+ public class TimelineInfoAutoMapperProfile : Profile
+ {
+ public TimelineInfoAutoMapperProfile()
+ {
+ CreateMap<Timeline, TimelineInfo>().ForMember(u => u._links, opt => opt.MapFrom<TimelineInfoLinksValueResolver>());
+ }
+ }
+}
diff --git a/Timeline/Models/Http/TimelineCommon.cs b/Timeline/Models/Http/TimelineCommon.cs deleted file mode 100644 index 3be4f895..00000000 --- a/Timeline/Models/Http/TimelineCommon.cs +++ /dev/null @@ -1,116 +0,0 @@ -using Microsoft.AspNetCore.Mvc;
-using System;
-using System.Collections.Generic;
-using System.Text.Json.Serialization;
-using Timeline.Controllers;
-
-namespace Timeline.Models.Http
-{
- public enum TimelineVisibility
- {
- /// <summary>
- /// All people including those without accounts.
- /// </summary>
- Public,
- /// <summary>
- /// Only people signed in.
- /// </summary>
- Register,
- /// <summary>
- /// Only member.
- /// </summary>
- Private
- }
-
- public static class TimelinePostContentTypes
- {
- public const string Text = "text";
- public const string Image = "image";
- }
-
- public interface ITimelinePostContent
- {
- public string Type { get; }
- }
-
- public class TextTimelinePostContent : ITimelinePostContent
- {
- public TextTimelinePostContent() { }
- public TextTimelinePostContent(string text) { Text = text; }
-
- public string Type { get; } = TimelinePostContentTypes.Text;
- public string Text { get; set; } = "";
- }
-
- public class ImageTimelinePostContent : ITimelinePostContent
- {
- public ImageTimelinePostContent() { }
- public ImageTimelinePostContent(string dataTag) { DataTag = dataTag; }
-
- public string Type { get; } = TimelinePostContentTypes.Image;
- [JsonIgnore]
- public string DataTag { get; set; } = "";
- public string? Url { get; set; } = null;
- }
-
- public class TimelinePostInfo
- {
- public long Id { get; set; }
- // use object to make json serializer use the runtime type
- public object Content { get; set; } = default!;
- public DateTime Time { get; set; }
- public UserInfo Author { get; set; } = default!;
- public DateTime LastUpdated { get; set; } = default!;
- }
-
- public class TimelineInfo
- {
- public string? Name { get; set; }
- public string Description { get; set; } = default!;
- public UserInfo Owner { get; set; } = default!;
- public TimelineVisibility Visibility { get; set; }
-#pragma warning disable CA2227 // Collection properties should be read only
- public List<UserInfo> Members { get; set; } = default!;
-#pragma warning restore CA2227 // Collection properties should be read only
-
-#pragma warning disable CA1707 // Identifiers should not contain underscores
- public TimelineInfoLinks? _links { get; set; }
-#pragma warning restore CA1707 // Identifiers should not contain underscores
- }
-
- public class TimelineInfoLinks
- {
- public string Self { get; set; } = default!;
- public string Posts { get; set; } = default!;
- }
-
- public static class TimelineInfoExtensions
- {
- public static TimelineInfo FillLinks(this TimelineInfo info, IUrlHelper urlHelper)
- {
- if (info == null)
- throw new ArgumentNullException(nameof(info));
- if (urlHelper == null)
- throw new ArgumentNullException(nameof(urlHelper));
-
- if (string.IsNullOrEmpty(info.Name))
- {
- info._links = new TimelineInfoLinks
- {
- Self = urlHelper.ActionLink(nameof(PersonalTimelineController.TimelineGet), nameof(PersonalTimelineController)[0..^nameof(Controller).Length], new { info.Owner.Username }),
- Posts = urlHelper.ActionLink(nameof(PersonalTimelineController.PostListGet), nameof(PersonalTimelineController)[0..^nameof(Controller).Length], new { info.Owner.Username })
- };
- }
- else
- {
- info._links = new TimelineInfoLinks
- {
- Self = urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { info.Name }),
- Posts = urlHelper.ActionLink(nameof(TimelineController.PostListGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { info.Name })
- };
- }
-
- return info;
- }
- }
-}
diff --git a/Timeline/Models/Http/TimelineController.cs b/Timeline/Models/Http/TimelineController.cs index ce5f3b98..b389c549 100644 --- a/Timeline/Models/Http/TimelineController.cs +++ b/Timeline/Models/Http/TimelineController.cs @@ -1,8 +1,8 @@ using System;
using System.ComponentModel.DataAnnotations;
-using Timeline.Models.Validation;
+using TimelineApp.Models.Validation;
-namespace Timeline.Models.Http
+namespace TimelineApp.Models.Http
{
public class TimelinePostCreateRequestContent
{
diff --git a/Timeline/Models/Http/TokenController.cs b/Timeline/Models/Http/TokenController.cs index ea8b59ed..3376fb61 100644 --- a/Timeline/Models/Http/TokenController.cs +++ b/Timeline/Models/Http/TokenController.cs @@ -1,6 +1,6 @@ using System.ComponentModel.DataAnnotations;
-namespace Timeline.Models.Http
+namespace TimelineApp.Models.Http
{
public class CreateTokenRequest
{
diff --git a/Timeline/Models/Http/UserController.cs b/Timeline/Models/Http/UserController.cs index e4c95cbd..c024c757 100644 --- a/Timeline/Models/Http/UserController.cs +++ b/Timeline/Models/Http/UserController.cs @@ -1,9 +1,9 @@ using AutoMapper;
using System.ComponentModel.DataAnnotations;
-using Timeline.Models.Validation;
-using Timeline.Services;
+using TimelineApp.Models.Validation;
+using TimelineApp.Services;
-namespace Timeline.Models.Http
+namespace TimelineApp.Models.Http
{
public class UserPatchRequest
{
diff --git a/Timeline/Models/Http/UserInfo.cs b/Timeline/Models/Http/UserInfo.cs index 68c6d8bd..fcbbe7c9 100644 --- a/Timeline/Models/Http/UserInfo.cs +++ b/Timeline/Models/Http/UserInfo.cs @@ -2,19 +2,19 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
-using Timeline.Controllers;
-using Timeline.Services;
+using System;
+using TimelineApp.Controllers;
-namespace Timeline.Models.Http
+namespace TimelineApp.Models.Http
{
public class UserInfo
{
public string Username { get; set; } = default!;
public string Nickname { get; set; } = default!;
public bool? Administrator { get; set; } = default!;
-#pragma warning disable CA1707
- public UserInfoLinks? _links { get; set; }
-#pragma warning restore CA1707
+#pragma warning disable CA1707 // Identifiers should not contain underscores
+ public UserInfoLinks _links { get; set; } = default!;
+#pragma warning restore CA1707 // Identifiers should not contain underscores
}
public class UserInfoLinks
@@ -24,7 +24,7 @@ namespace Timeline.Models.Http public string Timeline { get; set; } = default!;
}
- public class UserInfoLinksValueResolver : IValueResolver<User, UserInfo, UserInfoLinks?>
+ public class UserInfoLinksValueResolver : IValueResolver<User, UserInfo, UserInfoLinks>
{
private readonly IActionContextAccessor _actionContextAccessor;
private readonly IUrlHelperFactory _urlHelperFactory;
@@ -35,10 +35,10 @@ namespace Timeline.Models.Http _urlHelperFactory = urlHelperFactory;
}
- public UserInfoLinks? Resolve(User source, UserInfo destination, UserInfoLinks? destMember, ResolutionContext context)
+ public UserInfoLinks Resolve(User source, UserInfo destination, UserInfoLinks destMember, ResolutionContext context)
{
if (_actionContextAccessor.ActionContext == null)
- return null;
+ throw new InvalidOperationException("No action context, can't fill urls.");
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
var result = new UserInfoLinks
diff --git a/Timeline/Models/Timeline.cs b/Timeline/Models/Timeline.cs new file mode 100644 index 00000000..52f38e42 --- /dev/null +++ b/Timeline/Models/Timeline.cs @@ -0,0 +1,74 @@ +using System;
+using System.Collections.Generic;
+
+namespace TimelineApp.Models
+{
+ public enum TimelineVisibility
+ {
+ /// <summary>
+ /// All people including those without accounts.
+ /// </summary>
+ Public,
+ /// <summary>
+ /// Only people signed in.
+ /// </summary>
+ Register,
+ /// <summary>
+ /// Only member.
+ /// </summary>
+ Private
+ }
+
+ public static class TimelinePostContentTypes
+ {
+ public const string Text = "text";
+ public const string Image = "image";
+ }
+
+ public interface ITimelinePostContent
+ {
+ public string Type { get; }
+ }
+
+ public class TextTimelinePostContent : ITimelinePostContent
+ {
+ public TextTimelinePostContent(string text) { Text = text; }
+
+ public string Type { get; } = TimelinePostContentTypes.Text;
+ public string Text { get; set; }
+ }
+
+ public class ImageTimelinePostContent : ITimelinePostContent
+ {
+ public ImageTimelinePostContent(string dataTag) { DataTag = dataTag; }
+
+ public string Type { get; } = TimelinePostContentTypes.Image;
+ public string DataTag { get; set; }
+ }
+
+ public class TimelinePost
+ {
+ public long Id { get; set; }
+ public ITimelinePostContent Content { get; set; } = default!;
+ public DateTime Time { get; set; }
+ public User Author { get; set; } = default!;
+ public DateTime LastUpdated { get; set; } = default!;
+ }
+
+ public class Timeline
+ {
+ public string Name { get; set; } = default!;
+ public string Description { get; set; } = default!;
+ public User Owner { get; set; } = default!;
+ public TimelineVisibility Visibility { get; set; }
+#pragma warning disable CA2227 // Collection properties should be read only
+ public List<User> Members { get; set; } = default!;
+#pragma warning restore CA2227 // Collection properties should be read only
+ }
+
+ public class TimelineChangePropertyRequest
+ {
+ public string? Description { get; set; }
+ public TimelineVisibility? Visibility { get; set; }
+ }
+}
diff --git a/Timeline/Models/User.cs b/Timeline/Models/User.cs new file mode 100644 index 00000000..3b88a1b3 --- /dev/null +++ b/Timeline/Models/User.cs @@ -0,0 +1,18 @@ +namespace TimelineApp.Models
+{
+ public class User
+ {
+ public string? Username { get; set; }
+ public string? Nickname { get; set; }
+
+ #region adminsecret
+ public bool? Administrator { get; set; }
+ #endregion adminsecret
+
+ #region secret
+ public long? Id { get; set; }
+ public string? Password { get; set; }
+ public long? Version { get; set; }
+ #endregion secret
+ }
+}
diff --git a/Timeline/Models/Validation/NameValidator.cs b/Timeline/Models/Validation/NameValidator.cs index dec2b872..cf276309 100644 --- a/Timeline/Models/Validation/NameValidator.cs +++ b/Timeline/Models/Validation/NameValidator.cs @@ -1,7 +1,7 @@ using System.Linq;
-using static Timeline.Resources.Models.Validation.NameValidator;
+using static TimelineApp.Resources.Models.Validation.NameValidator;
-namespace Timeline.Models.Validation
+namespace TimelineApp.Models.Validation
{
public class NameValidator : Validator<string>
{
diff --git a/Timeline/Models/Validation/NicknameValidator.cs b/Timeline/Models/Validation/NicknameValidator.cs index 1d6ab163..cef80b9f 100644 --- a/Timeline/Models/Validation/NicknameValidator.cs +++ b/Timeline/Models/Validation/NicknameValidator.cs @@ -1,7 +1,7 @@ using System;
-using static Timeline.Resources.Models.Validation.NicknameValidator;
+using static TimelineApp.Resources.Models.Validation.NicknameValidator;
-namespace Timeline.Models.Validation
+namespace TimelineApp.Models.Validation
{
public class NicknameValidator : Validator<string>
{
diff --git a/Timeline/Models/Validation/TimelineNameValidator.cs b/Timeline/Models/Validation/TimelineNameValidator.cs index f1ab54e8..68110466 100644 --- a/Timeline/Models/Validation/TimelineNameValidator.cs +++ b/Timeline/Models/Validation/TimelineNameValidator.cs @@ -1,6 +1,6 @@ using System;
-namespace Timeline.Models.Validation
+namespace TimelineApp.Models.Validation
{
public class TimelineNameValidator : NameValidator
{
diff --git a/Timeline/Models/Validation/UsernameValidator.cs b/Timeline/Models/Validation/UsernameValidator.cs index 87bbf85f..bc0a9fe4 100644 --- a/Timeline/Models/Validation/UsernameValidator.cs +++ b/Timeline/Models/Validation/UsernameValidator.cs @@ -1,6 +1,6 @@ using System;
-namespace Timeline.Models.Validation
+namespace TimelineApp.Models.Validation
{
public class UsernameValidator : NameValidator
{
diff --git a/Timeline/Models/Validation/Validator.cs b/Timeline/Models/Validation/Validator.cs index ead7dbef..2c61705e 100644 --- a/Timeline/Models/Validation/Validator.cs +++ b/Timeline/Models/Validation/Validator.cs @@ -2,10 +2,10 @@ using Microsoft.Extensions.Localization;
using System;
using System.ComponentModel.DataAnnotations;
-using Timeline.Helpers;
-using static Timeline.Resources.Models.Validation.Validator;
+using TimelineApp.Helpers;
+using static TimelineApp.Resources.Models.Validation.Validator;
-namespace Timeline.Models.Validation
+namespace TimelineApp.Models.Validation
{
/// <summary>
/// A validator to validate value.
|