Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Commit

Permalink
GH-109: Add interface and implementation for comment like
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikfroehling committed Jun 15, 2018
1 parent 4fd8c81 commit 7feba0c
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Source/Lib/TraktApiSharp/Objects/Basic/ITraktCommentLike.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace TraktApiSharp.Objects.Basic
{
using Get.Users;
using System;

public interface ITraktCommentLike
{
DateTime? LikedAt { get; set; }

ITraktUser User { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace TraktApiSharp.Objects.Basic
{
using Get.Users;
using System;

public class TraktCommentLike : ITraktCommentLike
{
public DateTime? LikedAt { get; set; }

public ITraktUser User { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace TraktApiSharp.Objects.Basic.Json.Factories
{
using Objects.Basic.Json.Reader;
using Objects.Basic.Json.Writer;
using Objects.Json;

internal class CommentLikeJsonIOFactory : IJsonIOFactory<ITraktCommentLike>
{
public IObjectJsonReader<ITraktCommentLike> CreateObjectReader() => new CommentLikeObjectJsonReader();

public IArrayJsonReader<ITraktCommentLike> CreateArrayReader() => new CommentLikeArrayJsonReader();

public IObjectJsonWriter<ITraktCommentLike> CreateObjectWriter() => new CommentLikeObjectJsonWriter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace TraktApiSharp.Objects.Basic.Json.Reader
{
using Newtonsoft.Json;
using Objects.Json;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

internal class CommentLikeArrayJsonReader : AArrayJsonReader<ITraktCommentLike>
{
public override async Task<IEnumerable<ITraktCommentLike>> ReadArrayAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
{
if (jsonReader == null)
return await Task.FromResult(default(IEnumerable<ITraktCommentLike>));

if (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.StartArray)
{
var commentLikeReader = new CommentLikeObjectJsonReader();
//var commentLikeReadingTasks = new List<Task<ITraktCommentLike>>();
var commentLikes = new List<ITraktCommentLike>();

//commentLikeReadingTasks.Add(commentLikeReader.ReadObjectAsync(jsonReader, cancellationToken));
ITraktCommentLike commentLike = await commentLikeReader.ReadObjectAsync(jsonReader, cancellationToken);

while (commentLike != null)
{
commentLikes.Add(commentLike);
//commentLikeReadingTasks.Add(commentLikeReader.ReadObjectAsync(jsonReader, cancellationToken));
commentLike = await commentLikeReader.ReadObjectAsync(jsonReader, cancellationToken);
}

//var readCommentLikes = await Task.WhenAll(commentLikeReadingTasks);
//return (IEnumerable<ITraktCommentLike>)readCommentLikes.GetEnumerator();
return commentLikes;
}

return await Task.FromResult(default(IEnumerable<ITraktCommentLike>));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace TraktApiSharp.Objects.Basic.Json.Reader
{
using Get.Users.Json.Reader;
using Newtonsoft.Json;
using Objects.Json;
using System.Threading;
using System.Threading.Tasks;

internal class CommentLikeObjectJsonReader : AObjectJsonReader<ITraktCommentLike>
{
public override async Task<ITraktCommentLike> ReadObjectAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
{
if (jsonReader == null)
return await Task.FromResult(default(ITraktCommentLike));

if (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.StartObject)
{
ITraktCommentLike traktCommentLike = new TraktCommentLike();
var userObjectJsonReader = new UserObjectJsonReader();

while (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.PropertyName)
{
var propertyName = jsonReader.Value.ToString();

switch (propertyName)
{
case JsonProperties.COMMENT_LIKE_PROPERTY_NAME_LIKED_AT:
{
var value = await JsonReaderHelper.ReadDateTimeValueAsync(jsonReader, cancellationToken).ConfigureAwait(false);

if (value.First)
traktCommentLike.LikedAt = value.Second;

break;
}
case JsonProperties.COMMENT_LIKE_PROPERTY_NAME_USER:
traktCommentLike.User = await userObjectJsonReader.ReadObjectAsync(jsonReader, cancellationToken).ConfigureAwait(false);
break;
default:
await JsonReaderHelper.ReadAndIgnoreInvalidContentAsync(jsonReader, cancellationToken).ConfigureAwait(false);
break;
}
}

return traktCommentLike;
}

return await Task.FromResult(default(ITraktCommentLike));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace TraktApiSharp.Objects.Basic.Json.Writer
{
using Extensions;
using Get.Users.Json.Writer;
using Newtonsoft.Json;
using Objects.Json;
using System;
using System.Threading;
using System.Threading.Tasks;

internal class CommentLikeObjectJsonWriter : AObjectJsonWriter<ITraktCommentLike>
{
public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, ITraktCommentLike obj, CancellationToken cancellationToken = default)
{
if (jsonWriter == null)
throw new ArgumentNullException(nameof(jsonWriter));

await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false);

if (obj.LikedAt.HasValue)
{
await jsonWriter.WritePropertyNameAsync(JsonProperties.COMMENT_LIKE_PROPERTY_NAME_LIKED_AT, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.LikedAt.Value.ToTraktLongDateTimeString(), cancellationToken).ConfigureAwait(false);
}

if (obj.User != null)
{
var userObjectJsonWriter = new UserObjectJsonWriter();
await jsonWriter.WritePropertyNameAsync(JsonProperties.COMMENT_LIKE_PROPERTY_NAME_USER, cancellationToken).ConfigureAwait(false);
await userObjectJsonWriter.WriteObjectAsync(jsonWriter, obj.User, cancellationToken).ConfigureAwait(false);
}

await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ static JsonFactoryContainer()
s_jsonIOFactories.Add(typeof(ITraktCertifications), new CertificationsJsonIOFactory());
s_jsonIOFactories.Add(typeof(ITraktComment), new CommentJsonIOFactory());
s_jsonIOFactories.Add(typeof(ITraktCommentItem), new CommentItemJsonIOFactory());
s_jsonIOFactories.Add(typeof(ITraktCommentLike), new CommentLikeJsonIOFactory());
s_jsonIOFactories.Add(typeof(ITraktCrew), new CrewJsonIOFactory());
s_jsonIOFactories.Add(typeof(ITraktCrewMember), new CrewMemberJsonIOFactory());
s_jsonIOFactories.Add(typeof(ITraktError), new ErrorJsonIOFactory());
Expand Down
3 changes: 3 additions & 0 deletions Source/Lib/TraktApiSharp/Objects/Json/JsonProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ internal static class JsonProperties
internal const string COMMENT_ITEM_PROPERTY_NAME_EPISODE = "episode";
internal const string COMMENT_ITEM_PROPERTY_NAME_LIST = "list";

internal const string COMMENT_LIKE_PROPERTY_NAME_LIKED_AT = "liked_at";
internal const string COMMENT_LIKE_PROPERTY_NAME_USER = "user";

internal const string CREW_MEMBER_PROPERTY_NAME_JOB = "job";
internal const string CREW_MEMBER_PROPERTY_NAME_PERSON = "person";

Expand Down
6 changes: 6 additions & 0 deletions Source/Lib/TraktApiSharp/TraktApiSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@
<Compile Include="Objects\Basic\Implementations\TraktCertification.cs" />
<Compile Include="Objects\Basic\Implementations\TraktCertifications.cs" />
<Compile Include="Objects\Basic\Implementations\TraktCommentItem.cs" />
<Compile Include="Objects\Basic\Implementations\TraktCommentLike.cs" />
<Compile Include="Objects\Basic\Implementations\TraktNetwork.cs" />
<Compile Include="Objects\Basic\ITraktCastAndCrew.cs" />
<Compile Include="Objects\Basic\ITraktCastMember.cs" />
<Compile Include="Objects\Basic\ITraktCertification.cs" />
<Compile Include="Objects\Basic\ITraktCertifications.cs" />
<Compile Include="Objects\Basic\ITraktComment.cs" />
<Compile Include="Objects\Basic\ITraktCommentItem.cs" />
<Compile Include="Objects\Basic\ITraktCommentLike.cs" />
<Compile Include="Objects\Basic\ITraktCrew.cs" />
<Compile Include="Objects\Basic\ITraktCrewMember.cs" />
<Compile Include="Objects\Basic\ITraktError.cs" />
Expand All @@ -154,6 +156,7 @@
<Compile Include="Objects\Basic\ITraktStatistics.cs" />
<Compile Include="Objects\Basic\ITraktTranslation.cs" />
<Compile Include="Objects\Basic\Json\Factories\CommentItemJsonIOFactory.cs" />
<Compile Include="Objects\Basic\Json\Factories\CommentLikeJsonIOFactory.cs" />
<Compile Include="Objects\Basic\Json\Reader\CertificationArrayJsonReader.cs" />
<Compile Include="Objects\Basic\Json\Reader\CertificationObjectJsonReader.cs" />
<Compile Include="Objects\Basic\Json\Reader\CertificationsObjectJsonReader.cs" />
Expand All @@ -180,6 +183,8 @@
<Compile Include="Objects\Basic\Json\Reader\CommentArrayJsonReader.cs" />
<Compile Include="Objects\Basic\Json\Reader\CommentItemArrayJsonReader.cs" />
<Compile Include="Objects\Basic\Json\Reader\CommentItemObjectJsonReader.cs" />
<Compile Include="Objects\Basic\Json\Reader\CommentLikeArrayJsonReader.cs" />
<Compile Include="Objects\Basic\Json\Reader\CommentLikeObjectJsonReader.cs" />
<Compile Include="Objects\Basic\Json\Reader\CommentObjectJsonReader.cs" />
<Compile Include="Objects\Basic\Json\Reader\CrewMemberArrayJsonReader.cs" />
<Compile Include="Objects\Basic\Json\Reader\CrewMemberObjectJsonReader.cs" />
Expand All @@ -195,6 +200,7 @@
<Compile Include="Objects\Basic\Json\Writer\CertificationObjectJsonWriter.cs" />
<Compile Include="Objects\Basic\Json\Writer\CertificationsObjectJsonWriter.cs" />
<Compile Include="Objects\Basic\Json\Writer\CommentItemObjectJsonWriter.cs" />
<Compile Include="Objects\Basic\Json\Writer\CommentLikeObjectJsonWriter.cs" />
<Compile Include="Objects\Basic\Json\Writer\CommentObjectJsonWriter.cs" />
<Compile Include="Objects\Basic\Json\Writer\CrewMemberObjectJsonWriter.cs" />
<Compile Include="Objects\Basic\Json\Writer\CrewObjectJsonWriter.cs" />
Expand Down

0 comments on commit 7feba0c

Please sign in to comment.