Skip to content

Commit

Permalink
Merge branch 'server-api-client'
Browse files Browse the repository at this point in the history
  • Loading branch information
gauffininteractive committed Oct 3, 2016
2 parents 7c0102e + 7b1d8c1 commit 0576d0c
Show file tree
Hide file tree
Showing 36 changed files with 952 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/Server/OneTrueError.Api.Client.Tests/TryTheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace OneTrueError.Api.Client.Tests
#if DEBUG
public class TryTheClient
{
[Fact]
//[Fact]
public async Task Test()
{
OneTrueApiClient client = new OneTrueApiClient();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using DotNetCqs;

namespace OneTrueError.Api.Core.Applications.Commands
{
/// <summary>
/// Delete an existing application including of all its data.
/// </summary>
public class DeleteApplication : Command
{
/// <summary>
/// Creates a new instance of <see cref="DeleteApplication" />.
/// </summary>
/// <param name="id">application id</param>
public DeleteApplication(int id)
{
if (id <= 0) throw new ArgumentOutOfRangeException("id");
Id = id;
}

/// <summary>
/// Gets id of the application to delete.
/// </summary>
public int Id { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using DotNetCqs;

namespace OneTrueError.Api.Core.Applications.Events
{
/// <summary>
/// An application have been deleted.
/// </summary>
public class ApplicationDeleted : ApplicationEvent
{
/// <summary>
/// Key used when uploading reports
/// </summary>
public string AppKey { get; set; }

/// <summary>
/// Database PK
/// </summary>
public int ApplicationId { get; set; }

/// <summary>
/// Name of the application
/// </summary>
public string ApplicationName { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/Server/OneTrueError.Api/OneTrueError.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
<Compile Include="Core\ApiKeys\Queries\ListApiKeysResultItem.cs" />
<Compile Include="Core\Applications\ApplicationListItem.cs" />
<Compile Include="Core\Applications\Commands\CreateApplication.cs" />
<Compile Include="Core\Applications\Commands\DeleteApplication.cs" />
<Compile Include="Core\Applications\Events\ApplicationDeleted.cs" />
<Compile Include="Core\Applications\NamespaceDoc.cs" />
<Compile Include="Core\Applications\Queries\GetApplicationTeamResult.cs" />
<Compile Include="Core\Applications\Queries\OverviewStatSummary.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IAccountRepository
Task CreateAsync(Account account);

/// <summary>
/// find by using the actiovation key
/// find by using the activation key
/// </summary>
/// <param name="activationKey"></param>
/// <returns>account if found; otherwise <c>null</c>.</returns>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using DotNetCqs;
using Griffin.Container;
using OneTrueError.Api.Core.Applications.Events;

namespace OneTrueError.App.Core.ApiKeys.Events
{
/// <summary>
/// Will either delete an entire apikey (if the only association is with the given application) or just remove the
/// application mapping.
/// </summary>
[Component(RegisterAsSelf = true)]
public class ApplicationDeletedHandler : IApplicationEventSubscriber<ApplicationDeleted>
{
private readonly IApiKeyRepository _repository;

/// <summary>
/// Creates a new instance of <see cref="ApplicationDeletedHandler"/>.
/// </summary>
/// <param name="repository">repos</param>
public ApplicationDeletedHandler(IApiKeyRepository repository)
{
if (repository == null) throw new ArgumentNullException("repository");
_repository = repository;
}

public async Task HandleAsync(ApplicationDeleted e)
{
var apps = await _repository.GetForApplicationAsync(e.ApplicationId);
foreach (var apiKey in apps)
{
if (apiKey.AllowedApplications.Count() == 1)
await _repository.DeleteAsync(apiKey.Id);
else
await _repository.DeleteApplicationMappingAsync(apiKey.Id, e.ApplicationId);
}
}
}
}
25 changes: 24 additions & 1 deletion src/Server/OneTrueError.App/Core/ApiKeys/IApiKeyRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Griffin.Data;

namespace OneTrueError.App.Core.ApiKeys
Expand All @@ -8,12 +9,34 @@ namespace OneTrueError.App.Core.ApiKeys
/// </summary>
public interface IApiKeyRepository
{
/// <summary>
/// Delete all mappings that are for a specific application
/// </summary>
/// <param name="apiKeyId">id for the ApiKey that the application is associated with</param>
/// <param name="applicationId">Application to remove mapping for</param>
/// <returns></returns>
Task DeleteApplicationMappingAsync(int apiKeyId, int applicationId);

/// <summary>
/// Delete a specific ApiKey.
/// </summary>
/// <param name="keyId"></param>
/// <returns></returns>
Task DeleteAsync(int keyId);

/// <summary>
/// Get an key by using the generated string.
/// </summary>
/// <param name="apiKey">key</param>
/// <returns>key</returns>
/// <exception cref="EntityNotFoundException">Given key was not found.</exception>
Task<ApiKey> GetByKeyAsync(string apiKey);

/// <summary>
/// Get all ApiKeys that maps to a specific application
/// </summary>
/// <param name="applicationId">application id</param>
/// <returns>list</returns>
Task<IEnumerable<ApiKey>> GetForApplicationAsync(int applicationId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Threading.Tasks;
using DotNetCqs;
using Griffin.Container;
using OneTrueError.Api.Core.Applications.Commands;
using OneTrueError.Api.Core.Applications.Events;

namespace OneTrueError.App.Core.Applications.CommandHandlers
{
/// <summary>
/// Handler for <see cref="DeleteApplication"/>.
/// </summary>
[Component(RegisterAsSelf = true)]
public class DeleteApplicationHandler : ICommandHandler<DeleteApplication>
{
private readonly IEventBus _eventBus;
private readonly IApplicationRepository _repository;

/// <summary>
/// Creates a new instance of <see cref="DeleteApplicationHandler"/>.
/// </summary>
/// <param name="repository">used to delete the application</param>
/// <param name="eventBus">to publish ApplicationDeleted</param>
public DeleteApplicationHandler(IApplicationRepository repository, IEventBus eventBus)
{
_repository = repository;
_eventBus = eventBus;
}

public async Task ExecuteAsync(DeleteApplication command)
{
var app = await _repository.GetByIdAsync(command.Id);
await _repository.DeleteAsync(command.Id);
var evt = new ApplicationDeleted {ApplicationName = app.Name, ApplicationId = app.Id, AppKey = app.AppKey};
await _eventBus.PublishAsync(evt);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Threading.Tasks;
using DotNetCqs;
using Griffin.Container;
using Griffin.Data;
using OneTrueError.Api.Core.Applications.Events;

namespace OneTrueError.App.Core.Notifications.EventHandlers
{
/// <summary>
/// Will delete all reports for the given application
/// </summary>
[Component(RegisterAsSelf = true)]
public class ApplicationDeletedHandler : IApplicationEventSubscriber<ApplicationDeleted>
{
private IAdoNetUnitOfWork _uow;

/// <summary>
/// Creates a new instance of <see cref="ApplicationDeletedHandler"/>.
/// </summary>
public ApplicationDeletedHandler(IAdoNetUnitOfWork uow)
{
if (uow == null) throw new ArgumentNullException("uow");
_uow = uow;
}

public Task HandleAsync(ApplicationDeleted e)
{
_uow.ExecuteNonQuery("DELETE FROM UserNotificationSettings WHERE ApplicationId = @id", new { id = e.ApplicationId });
return Task.FromResult<object>(null);
}
}
}
5 changes: 5 additions & 0 deletions src/Server/OneTrueError.App/OneTrueError.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@
<Compile Include="Configuration\BaseConfiguration.cs" />
<Compile Include="Configuration\OneTrueErrorConfigSection.cs" />
<Compile Include="Core\ApiKeys\ApiKey.cs" />
<Compile Include="Core\ApiKeys\Events\ApplicationDeletedHandler.cs" />
<Compile Include="Core\ApiKeys\IApiKeyRepository.cs" />
<Compile Include="Core\Applications\ApplicationRole.cs" />
<Compile Include="Core\Applications\CommandHandlers\DeleteApplicationHandler.cs" />
<Compile Include="Core\Applications\EventHandlers\UpdateTeamOnInvitationAccepted.cs" />
<Compile Include="Core\Notifications\EventHandlers\ApplicationDeletedHandler.cs" />
<Compile Include="Core\Notifications\EventHandlers\CheckForFeedbackNotificationsToSend.cs" />
<Compile Include="Core\Reports\PagedReports.cs" />
<Compile Include="GlobalSuppressions.cs" />
Expand Down Expand Up @@ -269,6 +272,8 @@
<EmbeddedResource Include="Modules\Messaging\Templating\Layout\Template.html" />
</ItemGroup>
<ItemGroup>
<Folder Include="Core\Accounts\Events\" />
<Folder Include="Core\ErrorReports\Events\" />
<Folder Include="Modules\ReportSpikes\EventHandlers\" />
</ItemGroup>
<ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions src/Server/OneTrueError.Data.Common/IDatabaseUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ namespace OneTrueError.Infrastructure
{
public interface ISetupDatabaseTools
{
/// <summary>
/// Check if the current DB schema is out of date compared to the embedded schema resources.
/// </summary>
bool CanSchemaBeUpgraded();

/// <summary>
/// Update DB schema to latest version.
/// </summary>
void UpgradeDatabaseSchema();

/// <summary>
/// Used to check if the given connection string actually works
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Server/OneTrueError.SqlServer.Tests/ConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
Expand All @@ -18,5 +19,13 @@ public static IAdoNetUnitOfWork Create()
connection.Open();
return new AdoNetUnitOfWork(connection, true);
}

public static IDbConnection CreateConnection()
{
var connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["Db"].ConnectionString;
connection.Open();
return connection;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
<Compile Include="Core\ApiKeys\Queries\ListApiKeysHandlerTests.cs" />
<Compile Include="Modules\Geolocation\ErrorOriginRepositoryTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SchemaManagerTests.cs" />
<Compile Include="TestTools.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand All @@ -103,6 +105,10 @@
<Project>{5EF42A74-9323-49FA-A1F6-974D6DE77202}</Project>
<Name>OneTrueError.App</Name>
</ProjectReference>
<ProjectReference Include="..\OneTrueError.Data.Common\OneTrueError.Infrastructure.csproj">
<Project>{A78A50DA-C9D7-47F2-8528-D7EE39D91924}</Project>
<Name>OneTrueError.Infrastructure</Name>
</ProjectReference>
<ProjectReference Include="..\OneTrueError.SqlServer\OneTrueError.SqlServer.csproj">
<Project>{B967BEEA-CDDD-4A83-A4F2-1C946099ED51}</Project>
<Name>OneTrueError.SqlServer</Name>
Expand Down
Loading

0 comments on commit 0576d0c

Please sign in to comment.