-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteHandler.cs
77 lines (66 loc) · 3.2 KB
/
DeleteHandler.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using EnsureThat;
using MediatR;
using Microsoft.Health.Core.Features.Security.Authorization;
using Microsoft.Health.Dicom.Core.Exceptions;
using Microsoft.Health.Dicom.Core.Features.Common;
using Microsoft.Health.Dicom.Core.Features.Security;
using Microsoft.Health.Dicom.Core.Features.Validation;
using Microsoft.Health.Dicom.Core.Messages;
using Microsoft.Health.Dicom.Core.Messages.Delete;
namespace Microsoft.Health.Dicom.Core.Features.Delete;
public class DeleteHandler : BaseHandler, IRequestHandler<DeleteResourcesRequest, DeleteResourcesResponse>
{
private readonly IDeleteService _deleteService;
public DeleteHandler(IAuthorizationService<DataActions> authorizationService, IDeleteService deleteService)
: base(authorizationService)
{
_deleteService = EnsureArg.IsNotNull(deleteService, nameof(deleteService));
}
/// <inheritdoc />
public async Task<DeleteResourcesResponse> Handle(DeleteResourcesRequest request, CancellationToken cancellationToken)
{
EnsureArg.IsNotNull(request, nameof(request));
if (await AuthorizationService.CheckAccess(DataActions.Delete, cancellationToken) != DataActions.Delete)
{
throw new UnauthorizedDicomActionException(DataActions.Delete);
}
ValidateDeleteResourcesRequest(request);
switch (request.ResourceType)
{
case ResourceType.Study:
await _deleteService.DeleteStudyAsync(request.StudyInstanceUid, cancellationToken);
break;
case ResourceType.Series:
await _deleteService.DeleteSeriesAsync(request.StudyInstanceUid, request.SeriesInstanceUid, cancellationToken);
break;
case ResourceType.Instance:
await _deleteService.DeleteInstanceAsync(request.StudyInstanceUid, request.SeriesInstanceUid, request.SopInstanceUid, cancellationToken);
break;
default:
Debug.Fail($"Unknown delete transaction type: {request.ResourceType}", nameof(request));
break;
}
return new DeleteResourcesResponse();
}
private static void ValidateDeleteResourcesRequest(DeleteResourcesRequest request)
{
UidValidation.Validate(request.StudyInstanceUid, nameof(request.StudyInstanceUid));
switch (request.ResourceType)
{
case ResourceType.Series:
UidValidation.Validate(request.SeriesInstanceUid, nameof(request.SeriesInstanceUid));
break;
case ResourceType.Instance:
UidValidation.Validate(request.SeriesInstanceUid, nameof(request.SeriesInstanceUid));
UidValidation.Validate(request.SopInstanceUid, nameof(request.SopInstanceUid));
break;
}
}
}