Skip to content

Commit 7218772

Browse files
author
gauffininteractive
committed
Added support for deleting applications
1 parent 0120878 commit 7218772

File tree

22 files changed

+473
-21
lines changed

22 files changed

+473
-21
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using DotNetCqs;
3+
4+
namespace OneTrueError.Api.Core.Applications.Commands
5+
{
6+
/// <summary>
7+
/// Delete an existing application including of all its data.
8+
/// </summary>
9+
public class DeleteApplication : Command
10+
{
11+
/// <summary>
12+
/// Creates a new instance of <see cref="DeleteApplication" />.
13+
/// </summary>
14+
/// <param name="id">application id</param>
15+
public DeleteApplication(int id)
16+
{
17+
if (id <= 0) throw new ArgumentOutOfRangeException("id");
18+
Id = id;
19+
}
20+
21+
/// <summary>
22+
/// Gets id of the application to delete.
23+
/// </summary>
24+
public int Id { get; private set; }
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using DotNetCqs;
2+
3+
namespace OneTrueError.Api.Core.Applications.Events
4+
{
5+
/// <summary>
6+
/// An application have been deleted.
7+
/// </summary>
8+
public class ApplicationDeleted : ApplicationEvent
9+
{
10+
/// <summary>
11+
/// Key used when uploading reports
12+
/// </summary>
13+
public string AppKey { get; set; }
14+
15+
/// <summary>
16+
/// Database PK
17+
/// </summary>
18+
public int ApplicationId { get; set; }
19+
20+
/// <summary>
21+
/// Name of the application
22+
/// </summary>
23+
public string ApplicationName { get; set; }
24+
}
25+
}

src/Server/OneTrueError.Api/OneTrueError.Api.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@
9595
<Compile Include="Core\ApiKeys\Queries\ListApiKeysResultItem.cs" />
9696
<Compile Include="Core\Applications\ApplicationListItem.cs" />
9797
<Compile Include="Core\Applications\Commands\CreateApplication.cs" />
98+
<Compile Include="Core\Applications\Commands\DeleteApplication.cs" />
99+
<Compile Include="Core\Applications\Events\ApplicationDeleted.cs" />
98100
<Compile Include="Core\Applications\NamespaceDoc.cs" />
99101
<Compile Include="Core\Applications\Queries\GetApplicationTeamResult.cs" />
100102
<Compile Include="Core\Applications\Queries\OverviewStatSummary.cs" />

src/Server/OneTrueError.App/Core/Accounts/IAccountRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface IAccountRepository
2020
Task CreateAsync(Account account);
2121

2222
/// <summary>
23-
/// find by using the actiovation key
23+
/// find by using the activation key
2424
/// </summary>
2525
/// <param name="activationKey"></param>
2626
/// <returns>account if found; otherwise <c>null</c>.</returns>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using DotNetCqs;
5+
using Griffin.Container;
6+
using OneTrueError.Api.Core.Applications.Events;
7+
8+
namespace OneTrueError.App.Core.ApiKeys.Events
9+
{
10+
/// <summary>
11+
/// Will either delete an entire apikey (if the only association is with the given application) or just remove the
12+
/// application mapping.
13+
/// </summary>
14+
[Component(RegisterAsSelf = true)]
15+
public class ApplicationDeletedHandler : IApplicationEventSubscriber<ApplicationDeleted>
16+
{
17+
private readonly IApiKeyRepository _repository;
18+
19+
/// <summary>
20+
/// Creates a new instance of <see cref="ApplicationDeletedHandler"/>.
21+
/// </summary>
22+
/// <param name="repository">repos</param>
23+
public ApplicationDeletedHandler(IApiKeyRepository repository)
24+
{
25+
if (repository == null) throw new ArgumentNullException("repository");
26+
_repository = repository;
27+
}
28+
29+
public async Task HandleAsync(ApplicationDeleted e)
30+
{
31+
var apps = await _repository.GetForApplicationAsync(e.ApplicationId);
32+
foreach (var apiKey in apps)
33+
{
34+
if (apiKey.AllowedApplications.Count() == 1)
35+
await _repository.DeleteAsync(apiKey.Id);
36+
else
37+
await _repository.DeleteApplicationMappingAsync(apiKey.Id, e.ApplicationId);
38+
}
39+
}
40+
}
41+
}

src/Server/OneTrueError.App/Core/ApiKeys/IApiKeyRepository.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
23
using Griffin.Data;
34

45
namespace OneTrueError.App.Core.ApiKeys
@@ -8,12 +9,34 @@ namespace OneTrueError.App.Core.ApiKeys
89
/// </summary>
910
public interface IApiKeyRepository
1011
{
12+
/// <summary>
13+
/// Delete all mappings that are for a specific application
14+
/// </summary>
15+
/// <param name="apiKeyId">id for the ApiKey that the application is associated with</param>
16+
/// <param name="applicationId">Application to remove mapping for</param>
17+
/// <returns></returns>
18+
Task DeleteApplicationMappingAsync(int apiKeyId, int applicationId);
19+
20+
/// <summary>
21+
/// Delete a specific ApiKey.
22+
/// </summary>
23+
/// <param name="keyId"></param>
24+
/// <returns></returns>
25+
Task DeleteAsync(int keyId);
26+
1127
/// <summary>
1228
/// Get an key by using the generated string.
1329
/// </summary>
1430
/// <param name="apiKey">key</param>
1531
/// <returns>key</returns>
1632
/// <exception cref="EntityNotFoundException">Given key was not found.</exception>
1733
Task<ApiKey> GetByKeyAsync(string apiKey);
34+
35+
/// <summary>
36+
/// Get all ApiKeys that maps to a specific application
37+
/// </summary>
38+
/// <param name="applicationId">application id</param>
39+
/// <returns>list</returns>
40+
Task<IEnumerable<ApiKey>> GetForApplicationAsync(int applicationId);
1841
}
1942
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Threading.Tasks;
2+
using DotNetCqs;
3+
using Griffin.Container;
4+
using OneTrueError.Api.Core.Applications.Commands;
5+
using OneTrueError.Api.Core.Applications.Events;
6+
7+
namespace OneTrueError.App.Core.Applications.CommandHandlers
8+
{
9+
/// <summary>
10+
/// Handler for <see cref="DeleteApplication"/>.
11+
/// </summary>
12+
[Component(RegisterAsSelf = true)]
13+
public class DeleteApplicationHandler : ICommandHandler<DeleteApplication>
14+
{
15+
private readonly IEventBus _eventBus;
16+
private readonly IApplicationRepository _repository;
17+
18+
/// <summary>
19+
/// Creates a new instance of <see cref="DeleteApplicationHandler"/>.
20+
/// </summary>
21+
/// <param name="repository">used to delete the application</param>
22+
/// <param name="eventBus">to publish ApplicationDeleted</param>
23+
public DeleteApplicationHandler(IApplicationRepository repository, IEventBus eventBus)
24+
{
25+
_repository = repository;
26+
_eventBus = eventBus;
27+
}
28+
29+
public async Task ExecuteAsync(DeleteApplication command)
30+
{
31+
var app = await _repository.GetByIdAsync(command.Id);
32+
await _repository.DeleteAsync(command.Id);
33+
var evt = new ApplicationDeleted {ApplicationName = app.Name, ApplicationId = app.Id, AppKey = app.AppKey};
34+
await _eventBus.PublishAsync(evt);
35+
}
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using DotNetCqs;
4+
using Griffin.Container;
5+
using Griffin.Data;
6+
using OneTrueError.Api.Core.Applications.Events;
7+
8+
namespace OneTrueError.App.Core.Notifications.EventHandlers
9+
{
10+
/// <summary>
11+
/// Will delete all reports for the given application
12+
/// </summary>
13+
[Component(RegisterAsSelf = true)]
14+
public class ApplicationDeletedHandler : IApplicationEventSubscriber<ApplicationDeleted>
15+
{
16+
private IAdoNetUnitOfWork _uow;
17+
18+
/// <summary>
19+
/// Creates a new instance of <see cref="ApplicationDeletedHandler"/>.
20+
/// </summary>
21+
public ApplicationDeletedHandler(IAdoNetUnitOfWork uow)
22+
{
23+
if (uow == null) throw new ArgumentNullException("uow");
24+
_uow = uow;
25+
}
26+
27+
public Task HandleAsync(ApplicationDeleted e)
28+
{
29+
_uow.ExecuteNonQuery("DELETE FROM UserNotificationSettings WHERE ApplicationId = @id", new { id = e.ApplicationId });
30+
return Task.FromResult<object>(null);
31+
}
32+
}
33+
}

src/Server/OneTrueError.App/OneTrueError.App.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@
8181
<Compile Include="Configuration\BaseConfiguration.cs" />
8282
<Compile Include="Configuration\OneTrueErrorConfigSection.cs" />
8383
<Compile Include="Core\ApiKeys\ApiKey.cs" />
84+
<Compile Include="Core\ApiKeys\Events\ApplicationDeletedHandler.cs" />
8485
<Compile Include="Core\ApiKeys\IApiKeyRepository.cs" />
8586
<Compile Include="Core\Applications\ApplicationRole.cs" />
87+
<Compile Include="Core\Applications\CommandHandlers\DeleteApplicationHandler.cs" />
8688
<Compile Include="Core\Applications\EventHandlers\UpdateTeamOnInvitationAccepted.cs" />
89+
<Compile Include="Core\Notifications\EventHandlers\ApplicationDeletedHandler.cs" />
8790
<Compile Include="Core\Notifications\EventHandlers\CheckForFeedbackNotificationsToSend.cs" />
8891
<Compile Include="Core\Reports\PagedReports.cs" />
8992
<Compile Include="GlobalSuppressions.cs" />
@@ -269,6 +272,8 @@
269272
<EmbeddedResource Include="Modules\Messaging\Templating\Layout\Template.html" />
270273
</ItemGroup>
271274
<ItemGroup>
275+
<Folder Include="Core\Accounts\Events\" />
276+
<Folder Include="Core\ErrorReports\Events\" />
272277
<Folder Include="Modules\ReportSpikes\EventHandlers\" />
273278
</ItemGroup>
274279
<ItemGroup>

src/Server/OneTrueError.SqlServer/Core/ApiKeys/ApiKeyRepository.cs

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using Griffin.Container;
@@ -28,6 +29,31 @@ public ApiKeyRepository(IAdoNetUnitOfWork uow)
2829
_uow = uow;
2930
}
3031

32+
/// <summary>
33+
/// Delete all mappings that are for a specific application
34+
/// </summary>
35+
/// <param name="apiKeyId">id for the ApiKey that the application is associated with</param>
36+
/// <param name="applicationId">Application to remove mapping for</param>
37+
/// <returns></returns>
38+
public Task DeleteApplicationMappingAsync(int apiKeyId, int applicationId)
39+
{
40+
_uow.ExecuteNonQuery("DELETE FROM [ApiKeyApplications] WHERE ApiKeyId = @keyId AND ApplicationId = @appId",
41+
new {appId = applicationId, keyId = apiKeyId});
42+
return Task.FromResult<object>(null);
43+
}
44+
45+
/// <summary>
46+
/// Delete a specific ApiKey.
47+
/// </summary>
48+
/// <param name="keyId"></param>
49+
/// <returns></returns>
50+
public Task DeleteAsync(int keyId)
51+
{
52+
_uow.ExecuteNonQuery("DELETE FROM [ApiKeyApplications] WHERE ApiKeyId = @keyId", new {keyId});
53+
_uow.ExecuteNonQuery("DELETE FROM [ApiKeys] WHERE Id = @keyId", new {keyId});
54+
return Task.FromResult<object>(null);
55+
}
56+
3157
/// <summary>
3258
/// Get an key by using the generated string.
3359
/// </summary>
@@ -37,6 +63,7 @@ public ApiKeyRepository(IAdoNetUnitOfWork uow)
3763
public async Task<ApiKey> GetByKeyAsync(string apiKey)
3864
{
3965
if (apiKey == null) throw new ArgumentNullException(nameof(apiKey));
66+
4067
var key = await _uow.FirstAsync<ApiKey>("GeneratedKey=@1", apiKey);
4168
var sql = "SELECT [ApplicationId] FROM [ApiKeyApplications] WHERE [ApiKeyId] = @1";
4269
var apps = await _uow.ToListAsync(new IntMapper(), sql, key.Id);
@@ -47,6 +74,36 @@ public async Task<ApiKey> GetByKeyAsync(string apiKey)
4774
return key;
4875
}
4976

77+
/// <summary>
78+
/// Get all ApiKeys that maps to a specific application
79+
/// </summary>
80+
/// <param name="applicationId">application id</param>
81+
/// <returns>list</returns>
82+
public async Task<IEnumerable<ApiKey>> GetForApplicationAsync(int applicationId)
83+
{
84+
var apiKeyIds = new List<int>();
85+
using (var cmd = _uow.CreateDbCommand())
86+
{
87+
cmd.CommandText = "SELECT ApiKeyId FROM ApiKeyApplications WHERE ApplicationId = @id";
88+
cmd.AddParameter("id", applicationId);
89+
using (var reader = await cmd.ExecuteReaderAsync())
90+
{
91+
while (await reader.ReadAsync())
92+
{
93+
apiKeyIds.Add(reader.GetInt32(0));
94+
}
95+
}
96+
}
97+
98+
var keys = new List<ApiKey>();
99+
foreach (var id in apiKeyIds)
100+
{
101+
var key = await GetByKeyId(id);
102+
keys.Add(key);
103+
}
104+
return keys;
105+
}
106+
50107
/// <summary>
51108
/// Create a new key
52109
/// </summary>
@@ -63,6 +120,24 @@ public async Task CreateAsync(ApiKey key)
63120
}
64121
}
65122

123+
/// <summary>
124+
/// Get an key by using the generated string.
125+
/// </summary>
126+
/// <param name="id">PK</param>
127+
/// <returns>key</returns>
128+
/// <exception cref="EntityNotFoundException">Given key was not found.</exception>
129+
public async Task<ApiKey> GetByKeyId(int id)
130+
{
131+
var key = await _uow.FirstAsync<ApiKey>("id=@1", id);
132+
var sql = "SELECT [ApplicationId] FROM [ApiKeyApplications] WHERE [ApiKeyId] = @1";
133+
var apps = await _uow.ToListAsync(new IntMapper(), sql, key.Id);
134+
foreach (var app in apps)
135+
{
136+
key.Add(app);
137+
}
138+
return key;
139+
}
140+
66141
/// <summary>
67142
/// Update an existing key
68143
/// </summary>
@@ -82,7 +157,7 @@ await _uow.ToListAsync<int>("SELECT ApplicationId FROM ApiKeyApplications WHERE
82157
foreach (var applicationId in removed)
83158
{
84159
_uow.Execute("DELETE FROM ApiKeyApplications WHERE ApiKeyId = @1 AND ApplicationId = @2",
85-
new[] { key.Id, applicationId });
160+
new[] {key.Id, applicationId});
86161
}
87162

88163
var added = key.AllowedApplications.Except(existingMappings);
@@ -100,6 +175,5 @@ private void AddApplication(int apiKeyId, int applicationId)
100175
app = applicationId
101176
});
102177
}
103-
104178
}
105179
}

0 commit comments

Comments
 (0)