-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCommandServiceBase.cs
63 lines (53 loc) · 2.07 KB
/
CommandServiceBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Todoist.Net.Models;
namespace Todoist.Net.Services
{
internal abstract class CommandServiceBase
{
private readonly ICollection<Command> _queue;
protected CommandServiceBase(IAdvancedTodoistClient todoistClient)
{
TodoistClient = todoistClient;
}
protected CommandServiceBase(ICollection<Command> queue)
{
_queue = queue;
}
internal IAdvancedTodoistClient TodoistClient { get; }
internal Command CreateAddCommand<T>(CommandType commandType, T entity) where T : BaseEntity
{
var tempId = entity.Id.TempId;
if (tempId == Guid.Empty)
{
tempId = Guid.NewGuid();
}
entity.Id = tempId;
return new Command(commandType, entity, tempId);
}
internal Command CreateEntityCommand(CommandType commandType, ComplexId id)
{
return new Command(commandType, new BaseEntity(id));
}
/// <summary>
/// Executes the command asynchronous.
/// </summary>
/// <param name="command">The command.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Returns <see cref="T:System.Threading.Tasks.Task" />.The task object representing the asynchronous operation.</returns>
/// <exception cref="HttpRequestException">API exception.</exception>
/// <exception cref="AggregateException">Command execution exception.</exception>
internal async Task ExecuteCommandAsync(Command command, CancellationToken cancellationToken = default)
{
if (_queue == null)
{
await TodoistClient.ExecuteCommandsAsync(cancellationToken, command).ConfigureAwait(false);
return;
}
_queue.Add(command);
}
}
}