Skip to content

Commit 0576d0c

Browse files
author
gauffininteractive
committed
Merge branch 'server-api-client'
2 parents 7c0102e + 7b1d8c1 commit 0576d0c

File tree

36 files changed

+952
-89
lines changed

36 files changed

+952
-89
lines changed

src/Server/OneTrueError.Api.Client.Tests/TryTheClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace OneTrueError.Api.Client.Tests
1313
#if DEBUG
1414
public class TryTheClient
1515
{
16-
[Fact]
16+
//[Fact]
1717
public async Task Test()
1818
{
1919
OneTrueApiClient client = new OneTrueApiClient();
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>

0 commit comments

Comments
 (0)