|
| 1 | +using System.Text; |
| 2 | +using JetBrains.Annotations; |
| 3 | +using JsonApiDotNetCoreClientExample.Models; |
| 4 | + |
| 5 | +namespace Example; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Prints the specified people, their assigned todo-items, and its tags. |
| 9 | +/// </summary> |
| 10 | +[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)] |
| 11 | +internal sealed class PeopleMessageFormatter |
| 12 | +{ |
| 13 | + public static void PrintPeople(PersonCollectionResponseDocument? peopleResponse) |
| 14 | + { |
| 15 | + string message = WritePeople(peopleResponse); |
| 16 | + Console.WriteLine(message); |
| 17 | + } |
| 18 | + |
| 19 | + private static string WritePeople(PersonCollectionResponseDocument? peopleResponse) |
| 20 | + { |
| 21 | + if (peopleResponse == null) |
| 22 | + { |
| 23 | + return "The HTTP response hasn't changed, so no response body was returned."; |
| 24 | + } |
| 25 | + |
| 26 | + var builder = new StringBuilder(); |
| 27 | + builder.AppendLine($"Found {peopleResponse.Data.Count} people:"); |
| 28 | + |
| 29 | + foreach (PersonDataInResponse person in peopleResponse.Data) |
| 30 | + { |
| 31 | + WritePerson(person, peopleResponse.Included!, builder); |
| 32 | + } |
| 33 | + |
| 34 | + return builder.ToString(); |
| 35 | + } |
| 36 | + |
| 37 | + private static void WritePerson(PersonDataInResponse person, ICollection<DataInResponse> includes, StringBuilder builder) |
| 38 | + { |
| 39 | + ToManyTodoItemInResponse assignedTodoItems = person.Relationships!.AssignedTodoItems!; |
| 40 | + |
| 41 | + builder.AppendLine($" Person {person.Id}: {person.Attributes!.DisplayName} with {assignedTodoItems.Data!.Count} assigned todo-items:"); |
| 42 | + WriteRelatedTodoItems(assignedTodoItems.Data, includes, builder); |
| 43 | + } |
| 44 | + |
| 45 | + private static void WriteRelatedTodoItems(IEnumerable<TodoItemIdentifier> todoItemIdentifiers, ICollection<DataInResponse> includes, StringBuilder builder) |
| 46 | + { |
| 47 | + foreach (TodoItemIdentifier todoItemIdentifier in todoItemIdentifiers) |
| 48 | + { |
| 49 | + TodoItemDataInResponse includedTodoItem = includes.OfType<TodoItemDataInResponse>().Single(include => include.Id == todoItemIdentifier.Id); |
| 50 | + ToManyTagInResponse tags = includedTodoItem.Relationships!.Tags!; |
| 51 | + |
| 52 | + builder.AppendLine($" TodoItem {includedTodoItem.Id}: {includedTodoItem.Attributes!.Description} with {tags.Data!.Count} tags:"); |
| 53 | + WriteRelatedTags(tags.Data, includes, builder); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static void WriteRelatedTags(IEnumerable<TagIdentifier> tagIdentifiers, ICollection<DataInResponse> includes, StringBuilder builder) |
| 58 | + { |
| 59 | + foreach (TagIdentifier tagIdentifier in tagIdentifiers) |
| 60 | + { |
| 61 | + TagDataInResponse includedTag = includes.OfType<TagDataInResponse>().Single(include => include.Id == tagIdentifier.Id); |
| 62 | + builder.AppendLine($" Tag {includedTag.Id}: {includedTag.Attributes!.Name}"); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments