Skip to content

Commit

Permalink
GH-600: Add update list post json object
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikfroehling committed Jan 7, 2024
1 parent 5b31c04 commit d7b3fe8
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Source/Lib/Trakt.NET/Objects/Json/JsonFactoryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
using Post.Syncs.History.Json.Factories;
using Post.Syncs.History.Responses;
using Post.Syncs.History.Responses.Json.Factories;
using Post.Syncs.Lists;
using Post.Syncs.Lists.Json.Factories;
using Post.Syncs.Ratings;
using Post.Syncs.Ratings.Json.Factories;
using Post.Syncs.Ratings.Responses;
Expand Down Expand Up @@ -364,6 +366,9 @@ static JsonFactoryContainer()
s_jsonIOFactories.Add(typeof(ITraktSyncHistoryRemovePostResponseGroup), new SyncHistoryRemovePostResponseGroupJsonIOFactory());
s_jsonIOFactories.Add(typeof(ITraktSyncHistoryRemovePostResponseNotFoundGroup), new SyncHistoryRemovePostResponseNotFoundGroupJsonIOFactory());

// sync list update post objects
s_jsonIOFactories.Add(typeof(ITraktUpdateListPost), new UpdateListPostJsonIOFactory());

// sync ratings post objects
s_jsonIOFactories.Add(typeof(ITraktSyncRatingsPost), new SyncRatingsPostJsonIOFactory());
s_jsonIOFactories.Add(typeof(ITraktSyncRatingsPostEpisode), new SyncRatingsPostEpisodeJsonIOFactory());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace TraktNet.Objects.Post.Syncs.Lists
{
using Enums;
using Requests.Interfaces;

/// <summary>A Trakt list update post.</summary>
public interface ITraktUpdateListPost : IRequestBody
{
/// <summary>The description for the list.</summary>
string Description { get; set; }

/// <summary>The sort by value for the list.</summary>
TraktSortBy SortBy { get; set; }

/// <summary>The sort how value for the list.</summary>
TraktSortHow SortHow { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace TraktNet.Objects.Post.Syncs.Lists
{
using Enums;
using Exceptions;
using Objects.Json;
using System.Threading;
using System.Threading.Tasks;

/// <summary>A Trakt list update post.</summary>
public class TraktUpdateListPost : ITraktUpdateListPost
{
/// <summary>The description for the list.</summary>
public string Description { get; set; }

/// <summary>The sort by value for the list.</summary>
public TraktSortBy SortBy { get; set; }

/// <summary>The sort how value for the list.</summary>
public TraktSortHow SortHow { get; set; }

public Task<string> ToJson(CancellationToken cancellationToken = default)
{
IObjectJsonWriter<ITraktUpdateListPost> objectJsonWriter = JsonFactoryContainer.CreateObjectWriter<ITraktUpdateListPost>();
return objectJsonWriter.WriteObjectAsync(this, cancellationToken);
}

public void Validate()
{
bool hasNoDescription = string.IsNullOrEmpty(Description);
bool hasNoSortBy = SortBy == null || SortBy == TraktSortBy.Unspecified;
bool hasNoSortHow = SortHow == null || SortHow == TraktSortHow.Unspecified;

if (hasNoDescription && hasNoSortBy && hasNoSortHow) {
throw new TraktPostValidationException("no list update values set");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace TraktNet.Objects.Post.Syncs.Lists.Json.Factories
{
using Objects.Json;
using Reader;
using Writer;

internal class UpdateListPostJsonIOFactory : IJsonIOFactory<ITraktUpdateListPost>
{
public IObjectJsonReader<ITraktUpdateListPost> CreateObjectReader() => new UpdateListPostObjectJsonReader();

public IObjectJsonWriter<ITraktUpdateListPost> CreateObjectWriter() => new UpdateListPostObjectJsonWriter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace TraktNet.Objects.Post.Syncs.Lists.Json.Reader
{
using Enums;
using Newtonsoft.Json;
using Objects.Json;
using System.Threading;
using System.Threading.Tasks;

internal sealed class UpdateListPostObjectJsonReader : AObjectJsonReader<ITraktUpdateListPost>
{
public override async Task<ITraktUpdateListPost> ReadObjectAsync(JsonTextReader jsonReader, CancellationToken cancellationToken = default)
{
CheckJsonTextReader(jsonReader);

if (await jsonReader.ReadAsync(cancellationToken) && jsonReader.TokenType == JsonToken.StartObject) {
ITraktUpdateListPost updateListPost = new TraktUpdateListPost();

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

switch (propertyName) {
case JsonProperties.PROPERTY_NAME_DESCRIPTION:
updateListPost.Description = await jsonReader.ReadAsStringAsync(cancellationToken);
break;
case JsonProperties.PROPERTY_NAME_SORT_BY:
updateListPost.SortBy = await JsonReaderHelper.ReadEnumerationValueAsync<TraktSortBy>(jsonReader, cancellationToken);
break;
case JsonProperties.PROPERTY_NAME_SORT_HOW:
updateListPost.SortHow = await JsonReaderHelper.ReadEnumerationValueAsync<TraktSortHow>(jsonReader, cancellationToken);
break;
default:
await JsonReaderHelper.ReadAndIgnoreInvalidContentAsync(jsonReader, cancellationToken);
break;
}
}

return updateListPost;
}

return await Task.FromResult(default(ITraktUpdateListPost));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace TraktNet.Objects.Post.Syncs.Lists.Json.Writer
{
using Enums;
using Newtonsoft.Json;
using Objects.Json;
using System.Threading;
using System.Threading.Tasks;

internal sealed class UpdateListPostObjectJsonWriter : AObjectJsonWriter<ITraktUpdateListPost>
{
public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, ITraktUpdateListPost obj, CancellationToken cancellationToken = default)
{
CheckJsonTextWriter(jsonWriter);
await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false);

if (!string.IsNullOrEmpty(obj.Description)) {
await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_DESCRIPTION, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.Description, cancellationToken).ConfigureAwait(false);
}

if (obj.SortBy != null && obj.SortBy != TraktSortBy.Unspecified) {
await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_SORT_BY, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.SortBy.ObjectName, cancellationToken).ConfigureAwait(false);
}

if (obj.SortHow != null && obj.SortHow != TraktSortHow.Unspecified) {
await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_SORT_HOW, cancellationToken).ConfigureAwait(false);
await jsonWriter.WriteValueAsync(obj.SortHow.ObjectName, cancellationToken).ConfigureAwait(false);
}

await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
namespace TraktNet.Objects.Post.Tests.Syncs.Lists.Implementations
{
using FluentAssertions;
using System;
using System.Threading.Tasks;
using Trakt.NET.Tests.Utility.Traits;
using TraktNet.Enums;
using TraktNet.Exceptions;
using TraktNet.Objects.Post.Syncs.Lists;
using TraktNet.Objects.Post.Syncs.Lists.Json.Reader;
using Xunit;

[TestCategory("Objects.Post.Syncs.Lists.Implementations")]
public class TraktUpdateListPost_Tests
{
[Fact]
public void Test_TraktUpdateListPost_Default_Constructor()
{
var updateListPost = new TraktUpdateListPost();

updateListPost.Description.Should().BeNull();
updateListPost.SortBy.Should().BeNull();
updateListPost.SortHow.Should().BeNull();
}

[Fact]
public async Task Test_TraktUpdateListPost_From_Json()
{
var jsonReader = new UpdateListPostObjectJsonReader();
var updateListPost = await jsonReader.ReadObjectAsync(JSON) as TraktUpdateListPost;

updateListPost.Should().NotBeNull();
updateListPost.Description.Should().Be("Updated description");
updateListPost.SortBy.Should().Be(TraktSortBy.Rank);
updateListPost.SortHow.Should().Be(TraktSortHow.Descending);
}

[Fact]
public void Test_TraktUpdateListPost_Validate()
{
ITraktUpdateListPost updateListPost = new TraktUpdateListPost();

// description = null, sortBy = null, sortHow = null
Action act = () => updateListPost.Validate();
_ = act.Should().Throw<TraktPostValidationException>();

// description = empty, sortBy = null, sortHow = null
updateListPost.Description = string.Empty;
_ = act.Should().Throw<TraktPostValidationException>();

// description = not empty, sortBy = null, sortHow = null
updateListPost.Description = "description";
_ = act.Should().NotThrow();

// description = null, sortBy = unspecified, sortHow = null
updateListPost.Description = null;
updateListPost.SortBy = TraktSortBy.Unspecified;
_ = act.Should().Throw<TraktPostValidationException>();

// description = null, sortBy = has value, sortHow = null
updateListPost.SortBy = TraktSortBy.Rank;
_ = act.Should().NotThrow();

// description = null, sortBy = null, sortHow = unspecified
updateListPost.SortBy = null;
updateListPost.SortHow = TraktSortHow.Unspecified;
_ = act.Should().Throw<TraktPostValidationException>();

// description = null, sortBy = null, sortHow = has value
updateListPost.SortHow = TraktSortHow.Descending;
_ = act.Should().NotThrow();
}

private const string JSON =
@"{
""description"": ""Updated description"",
""sort_by"": ""rank"",
""sort_how"": ""desc""
}";
}
}

0 comments on commit d7b3fe8

Please sign in to comment.