|
| 1 | +using System; |
| 2 | +using GitLabApiClient.Models.Issues.Responses; |
| 3 | +using GitLabApiClient.Models.Projects.Responses; |
| 4 | +using GitLabApiClient.Models.MergeRequests.Responses; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using Newtonsoft.Json.Converters; |
| 7 | +using Newtonsoft.Json.Linq; |
| 8 | + |
| 9 | +namespace GitLabApiClient.Models.ToDoList.Responses |
| 10 | +{ |
| 11 | + |
| 12 | + [JsonConverter(typeof(ToDoItemConverter))] |
| 13 | + public interface IToDo |
| 14 | + { |
| 15 | + int? Id { get; set; } |
| 16 | + Project Project { get; set; } |
| 17 | + Assignee Author { get; set; } |
| 18 | + ToDoActionType? ActionType { get; set; } |
| 19 | + ToDoTargetType? TargetType { get; set; } |
| 20 | + string TargetUrl { get; set; } |
| 21 | + string Body { get; set; } |
| 22 | + ToDoState? State { get; set; } |
| 23 | + string CreatedAt { get; set; } |
| 24 | + } |
| 25 | + |
| 26 | + public abstract class ToDo : IToDo |
| 27 | + { |
| 28 | + [JsonProperty("id")] |
| 29 | + public int? Id { get; set; } |
| 30 | + |
| 31 | + [JsonProperty("project")] |
| 32 | + public Project Project { get; set; } |
| 33 | + |
| 34 | + [JsonProperty("author")] |
| 35 | + public Assignee Author { get; set; } |
| 36 | + |
| 37 | + [JsonProperty("action_name")] |
| 38 | + public ToDoActionType? ActionType { get; set; } |
| 39 | + |
| 40 | + [JsonProperty("target_type")] |
| 41 | + public ToDoTargetType? TargetType { get; set; } |
| 42 | + |
| 43 | + [JsonProperty("target_url")] |
| 44 | + public string TargetUrl { get; set; } |
| 45 | + |
| 46 | + [JsonProperty("body")] |
| 47 | + public string Body { get; set; } |
| 48 | + |
| 49 | + [JsonProperty("state")] |
| 50 | + public ToDoState? State { get; set; } |
| 51 | + |
| 52 | + [JsonProperty("created_at")] |
| 53 | + public string CreatedAt { get; set; } |
| 54 | + } |
| 55 | + |
| 56 | + public sealed class ToDoIssue : ToDo |
| 57 | + { |
| 58 | + [JsonProperty("target")] |
| 59 | + public Issue Target { get; set; } |
| 60 | + } |
| 61 | + |
| 62 | + public sealed class ToDoMergeRequest : ToDo |
| 63 | + { |
| 64 | + [JsonProperty("target")] |
| 65 | + public MergeRequest Target { get; set; } |
| 66 | + } |
| 67 | + |
| 68 | + public class ToDoItemConverter : CustomCreationConverter<IToDo> |
| 69 | + { |
| 70 | + public override IToDo Create(Type objectType) |
| 71 | + { |
| 72 | + throw new NotImplementedException(); |
| 73 | + } |
| 74 | + |
| 75 | + public IToDo Create(JObject jObject) |
| 76 | + { |
| 77 | + string type = (string)jObject.Property("target_type"); |
| 78 | + switch (type) |
| 79 | + { |
| 80 | + case "Issue": |
| 81 | + return new ToDoIssue(); |
| 82 | + case "MergeRequest": |
| 83 | + return new ToDoMergeRequest(); |
| 84 | + default: |
| 85 | + throw new ApplicationException($"ToDo target not supported."); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
| 90 | + { |
| 91 | + var jObject = JObject.Load(reader); |
| 92 | + var target = Create(jObject); |
| 93 | + serializer.Populate(jObject.CreateReader(), target); |
| 94 | + return target; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments