-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-600: Add update list post json object
- Loading branch information
1 parent
5b31c04
commit d7b3fe8
Showing
7 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Source/Lib/Trakt.NET/Objects/Post/Syncs/Lists/ITraktUpdateListPost.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Source/Lib/Trakt.NET/Objects/Post/Syncs/Lists/Implementations/TraktUpdateListPost.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Source/Lib/Trakt.NET/Objects/Post/Syncs/Lists/Json/Factories/UpdateListPostJsonIOFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Source/Lib/Trakt.NET/Objects/Post/Syncs/Lists/Json/Reader/UpdateListPostObjectJsonReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Source/Lib/Trakt.NET/Objects/Post/Syncs/Lists/Json/Writer/UpdateListPostObjectJsonWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...sts/Trakt.NET.Objects.Post.Tests/Syncs/Lists/Implementations/TraktUpdateListPost_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"" | ||
}"; | ||
} | ||
} |