-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessorService.cs
More file actions
70 lines (60 loc) · 3.29 KB
/
Copy pathProcessorService.cs
File metadata and controls
70 lines (60 loc) · 3.29 KB
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
64
65
66
67
68
69
70
using InvvardDev.Ifttt.Contracts;
using InvvardDev.Ifttt.Models.Core;
using InvvardDev.Ifttt.Models.Trigger;
namespace InvvardDev.Ifttt.Services;
internal abstract class ProcessorService(IProcessorRepository processorRepository, IServiceProvider serviceProvider) : IProcessorService
{
protected abstract ProcessorKind Kind { get; }
public async Task AddOrUpdateProcessor(ProcessorTree processorTree)
{
await (await GetProcessor(processorTree.ProcessorSlug) switch
{
null => processorRepository.AddProcessor(processorTree),
{ } existingProcessorTree when existingProcessorTree.ProcessorType == processorTree.ProcessorType
=> processorRepository.UpdateProcessor(processorTree),
{ } pt when pt.ProcessorType != processorTree.ProcessorType
=> throw new InvalidOperationException($"Conflict: '{pt.Kind}' processor with slug '{pt.ProcessorSlug}' already exists with '{pt.ProcessorType}' type."),
_ => throw new ArgumentOutOfRangeException(nameof(processorTree))
});
}
public async Task AddDataField(string processorSlug, string dataFieldSlug, Type dataFieldType)
{
switch (await GetProcessor(processorSlug))
{
case { } processorTree when processorTree.DataFields.ContainsKey(dataFieldSlug) && processorTree.DataFields[dataFieldSlug] != dataFieldType:
throw new
InvalidOperationException($"Conflict: '{processorTree.Kind}' processor with slug '{processorTree.ProcessorSlug}' already has a data field with slug '{dataFieldSlug}' with a different type '{processorTree.DataFields[dataFieldSlug]}'.");
case { } processorTree when processorTree.DataFields.ContainsKey(dataFieldSlug) && processorTree.DataFields[dataFieldSlug] == dataFieldType:
// Nothing to do
break;
case { } processorTree when !processorTree.DataFields.ContainsKey(dataFieldSlug):
processorTree.DataFields.Add(dataFieldSlug, dataFieldType);
await processorRepository.UpdateProcessor(processorTree);
break;
default:
throw new InvalidOperationException($"Processor with slug '{processorSlug}' does not exist.");
}
}
public Task<bool> Exists(string processorSlug)
=> processorRepository.Exists(Kind.GetProcessorKey(processorSlug));
public async Task<Type?> GetDataFieldType(string processorSlug, string dataFieldSlug)
{
if (await GetProcessor(processorSlug) is { } processorTree
&& processorTree.DataFields.TryGetValue(dataFieldSlug, out var dataFieldType))
{
return dataFieldType;
}
return default;
}
public async Task<ProcessorTree?> GetProcessor(string processorSlug)
=> await processorRepository.GetProcessorByKey(Kind.GetProcessorKey(processorSlug));
public async Task<TInterface?> GetProcessorInstance<TInterface>(string processorSlug)
{
if (await GetProcessor(processorSlug) is { } processorTree
&& ActivatorUtilities.CreateInstance(serviceProvider, processorTree.ProcessorType) is TInterface processor)
{
return processor;
}
return default;
}
}