|
| 1 | +using System; |
| 2 | + |
| 3 | +using Newtonsoft.Json; |
| 4 | + |
| 5 | +namespace Todoist.Net.Models |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Represents durations for tasks. |
| 9 | + /// </summary> |
| 10 | + public class Duration |
| 11 | + { |
| 12 | + |
| 13 | + private int _amount; |
| 14 | + private DurationUnit _unit; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Initializes a new instance of the <see cref="Duration" /> class. |
| 18 | + /// </summary> |
| 19 | + /// <param name="amount">The time amount.</param> |
| 20 | + /// <param name="unit">The time unit.</param> |
| 21 | + public Duration(int amount, DurationUnit unit) |
| 22 | + { |
| 23 | + Amount = amount; |
| 24 | + Unit = unit; |
| 25 | + } |
| 26 | + |
| 27 | + internal Duration() |
| 28 | + { |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets or sets the duration time amount. |
| 33 | + /// </summary> |
| 34 | + /// <remarks> |
| 35 | + /// Must be a positive integer (greater than zero). |
| 36 | + /// </remarks> |
| 37 | + /// <value> |
| 38 | + /// The time amount. |
| 39 | + /// </value> |
| 40 | + /// <exception cref="ArgumentOutOfRangeException">Duration amount must be greater than zero.</exception>" |
| 41 | + [JsonProperty("amount")] |
| 42 | + public int Amount |
| 43 | + { |
| 44 | + get => _amount; |
| 45 | + set => _amount = value > 0 |
| 46 | + ? value |
| 47 | + : throw new ArgumentOutOfRangeException(nameof(Amount), "Duration amount must be greater than zero."); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets or sets the duration time unit. |
| 52 | + /// </summary> |
| 53 | + /// <remarks> |
| 54 | + /// Either <c>minute</c> or <c>day</c>. |
| 55 | + /// </remarks> |
| 56 | + /// <value> |
| 57 | + /// The duration unit. |
| 58 | + /// </value> |
| 59 | + /// <exception cref="ArgumentNullException">Unit</exception> |
| 60 | + [JsonProperty("unit")] |
| 61 | + public DurationUnit Unit |
| 62 | + { |
| 63 | + get => _unit; |
| 64 | + set => _unit = value ?? throw new ArgumentNullException(nameof(Unit)); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Gets the value of the duration as a <see cref="TimeSpan"/> object. |
| 70 | + /// </summary> |
| 71 | + /// <value> |
| 72 | + /// The <see cref="TimeSpan"/> value of the duration. |
| 73 | + /// </value> |
| 74 | + [JsonIgnore] |
| 75 | + public TimeSpan TimeValue |
| 76 | + { |
| 77 | + get |
| 78 | + { |
| 79 | + switch (Unit) |
| 80 | + { |
| 81 | + case var _ when Unit == DurationUnit.Minute: |
| 82 | + return TimeSpan.FromMinutes(Amount); |
| 83 | + case var _ when Unit == DurationUnit.Day: |
| 84 | + return TimeSpan.FromDays(Amount); |
| 85 | + default: |
| 86 | + throw new NotImplementedException(); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | +} |
0 commit comments