aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models/Http
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-03-10 03:04:23 +0800
committercrupest <crupest@outlook.com>2020-03-10 03:04:23 +0800
commit84079146cf4c179cfe2770386dcd9f1cd1e7b14a (patch)
tree17d64590d81fc02a2b8e3df88963288b0ca85bb7 /Timeline/Models/Http
parentc3c9618c8493349fb3d6464d69359f7be6e8ce24 (diff)
downloadtimeline-84079146cf4c179cfe2770386dcd9f1cd1e7b14a.tar.gz
timeline-84079146cf4c179cfe2770386dcd9f1cd1e7b14a.tar.bz2
timeline-84079146cf4c179cfe2770386dcd9f1cd1e7b14a.zip
...
Diffstat (limited to 'Timeline/Models/Http')
-rw-r--r--Timeline/Models/Http/Common.cs4
-rw-r--r--Timeline/Models/Http/ErrorResponse.cs4
-rw-r--r--Timeline/Models/Http/Timeline.cs119
-rw-r--r--Timeline/Models/Http/TimelineCommon.cs116
-rw-r--r--Timeline/Models/Http/TimelineController.cs4
-rw-r--r--Timeline/Models/Http/TokenController.cs2
-rw-r--r--Timeline/Models/Http/UserController.cs6
-rw-r--r--Timeline/Models/Http/UserInfo.cs18
8 files changed, 138 insertions, 135 deletions
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