Skip to content

Commit

Permalink
Manage blog settings (#62)
Browse files Browse the repository at this point in the history
* Manage blog settings #23
- UpdateBlogSettingsCommand
- BlogSettings db migration

* BlogSettingsConfigurationProvider and EntityChangeWatcher
* change to IOptionsSnapshot
  • Loading branch information
petervandenhout authored Sep 25, 2019
1 parent b90178b commit 3386438
Show file tree
Hide file tree
Showing 56 changed files with 1,040 additions and 133 deletions.
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ steps:
# Release: triggered by a version tag
- task: DotNetCoreCLI@2
displayName: Publish
#condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
inputs:
command: publish
publishWebProjects: false
Expand All @@ -57,7 +57,7 @@ steps:
# Delete the "Areas" folder, this should not be deployed
- task: DeleteFiles@1
displayName: Publish Cleanup
#condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/'))
inputs:
sourceFolder: $(build.artifactstagingdirectory)\Opw.PineBlog.Sample
contents: 'Areas'
Expand Down
2 changes: 1 addition & 1 deletion samples/Opw.PineBlog.Sample/Opw.PineBlog.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.7.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.8.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="WaffleGenerator" Version="4.0.2" />
Expand Down
3 changes: 3 additions & 0 deletions samples/Opw.PineBlog.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.ConfigureAppConfiguration((hostingContext, config) => {
config.AddPineBlogConfiguration(reloadOnChange: true);
})
.ConfigureLogging((hostingContext, builder) =>
{
builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
Expand Down
1 change: 1 addition & 0 deletions samples/Opw.PineBlog.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Opw.PineBlog.RazorPages;
using Opw.PineBlog.Sample.Middleware;
using Opw.PineBlog.EntityFrameworkCore;

namespace Opw.PineBlog.Sample
{
Expand Down
2 changes: 1 addition & 1 deletion src/Opw.AspNetCore/Opw.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Opw.HttpExceptions.AspNetCore" Version="2.2.0" />
<PackageReference Include="Opw.HttpExceptions.AspNetCore" Version="2.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="4.0.1" />
</ItemGroup>
Expand Down
31 changes: 0 additions & 31 deletions src/Opw.EntityFrameworkCore/DbContextConfigurer.cs

This file was deleted.

53 changes: 53 additions & 0 deletions src/Opw.EntityFrameworkCore/DbContextOptionsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Data.SqlClient;

namespace Opw.EntityFrameworkCore
{
public static class DbContextOptionsHelper
{
private static readonly InMemoryDatabaseRoot _inMemoryDatabaseRoot = new InMemoryDatabaseRoot();

public static void ConfigureOptionsBuilder(DbContextOptionsBuilder builder, string connectionString)
{
if (connectionString.IndexOf("inMemory", StringComparison.InvariantCultureIgnoreCase) >= 0)
{
// don't raise the error warning us that the in memory db doesn't support transactions
builder.ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning));

var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
var dbName = connectionStringBuilder.InitialCatalog;

builder.UseInMemoryDatabase(dbName, _inMemoryDatabaseRoot);
}
else
{
builder.UseSqlServer(connectionString);
}
}

public static Action<DbContextOptionsBuilder> Configure(string connectionString)
{
return options =>
{
if (connectionString.IndexOf("inMemory", StringComparison.InvariantCultureIgnoreCase) >= 0)
{
// don't raise the error warning us that the in memory db doesn't support transactions
options.ConfigureWarnings(w => w.Ignore(InMemoryEventId.TransactionIgnoredWarning));

var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
var dbName = connectionStringBuilder.InitialCatalog;

options.UseInMemoryDatabase(dbName, _inMemoryDatabaseRoot);
}
else
{
options.UseSqlServer(connectionString);
}
};

}
}
}
25 changes: 25 additions & 0 deletions src/Opw.EntityFrameworkCore/EntityChangeEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;

namespace Opw.EntityFrameworkCore
{
/// <summary>
/// Event arguments for events relating to tracked Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntrys.
/// </summary>
public class EntityChangeEventArgs : EventArgs
{
/// <summary>
/// The Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry for the entity.
/// </summary>
public EntityEntry Entry { get; }

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended
/// to be used directly from your code. This API may change or be removed in future releases.
/// </summary>
public EntityChangeEventArgs(EntityEntry entityEntry)
{
Entry = entityEntry;
}
}
}
39 changes: 39 additions & 0 deletions src/Opw.EntityFrameworkCore/EntityChangeWatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Threading;

namespace Opw.EntityFrameworkCore
{
/// <summary>
/// Watches for changes on entities.
/// </summary>
public class EntityChangeWatcher
{
/// <summary>
/// An event fired when an entity that is tracked by the associated Microsoft.EntityFrameworkCore.DbContext
/// has moved from one Microsoft.EntityFrameworkCore.EntityState to another.
/// </summary>
public event EventHandler<EntityChangeEventArgs> Changed;

/// <summary>
/// Let the EntityChangeWatcher know an entity has changed.
/// </summary>
/// <param name="e">Event arguments for events relating to tracked Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntrys.</param>
public void OnChanged(EntityChangeEventArgs e)
{
ThreadPool.QueueUserWorkItem((_) => Changed?.Invoke(this, e));
}

#region singleton

private static readonly Lazy<EntityChangeWatcher> lazy = new Lazy<EntityChangeWatcher>(() => new EntityChangeWatcher());

private EntityChangeWatcher() { }

/// <summary>
/// Singleton instance of EntityChangeWatcher.
/// </summary>
public static EntityChangeWatcher Instance => lazy.Value;

#endregion
}
}
14 changes: 14 additions & 0 deletions src/Opw.EntityFrameworkCore/EntityDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Linq;
using System.Threading;
Expand All @@ -19,6 +20,7 @@ protected EntityDbContext(DbContextOptions options) : base(options) { }
{
UpdatedCreateDate();
UpdatedModifiedDate();
NotifyEntityChangeWatchers();

try
{
Expand All @@ -34,6 +36,7 @@ protected EntityDbContext(DbContextOptions options) : base(options) { }
{
UpdatedCreateDate();
UpdatedModifiedDate();
NotifyEntityChangeWatchers();

try
{
Expand All @@ -49,6 +52,7 @@ protected EntityDbContext(DbContextOptions options) : base(options) { }
{
UpdatedCreateDate();
UpdatedModifiedDate();
NotifyEntityChangeWatchers();

try
{
Expand All @@ -65,6 +69,7 @@ protected EntityDbContext(DbContextOptions options) : base(options) { }
{
UpdatedCreateDate();
UpdatedModifiedDate();
NotifyEntityChangeWatchers();

try
{
Expand All @@ -77,6 +82,15 @@ protected EntityDbContext(DbContextOptions options) : base(options) { }
}
}

private void NotifyEntityChangeWatchers()
{
foreach(var entry in ChangeTracker.Entries()
.Where(i => i.State == EntityState.Modified || i.State == EntityState.Added))
{
EntityChangeWatcher.Instance.OnChanged(new EntityChangeEventArgs(entry));
}
}

private void UpdatedCreateDate()
{
var entries = ChangeTracker.Entries()
Expand Down
12 changes: 12 additions & 0 deletions src/Opw.EntityFrameworkCore/IEntityChangeWatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Opw.EntityFrameworkCore
{
public interface IEntityChangeWatcher
{
event EventHandler<EntityEntryEventArgs> Changed;

void OnChanged(EntityEntryEventArgs e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Opw.PineBlog.Entities
{
public class Settings : IEntityCreated, IEntityModified
public class BlogSettings : IEntityCreated, IEntityModified
{
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
Expand Down
32 changes: 22 additions & 10 deletions src/Opw.PineBlog.Abstractions/PineBlogOptions.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@

namespace Opw
namespace Opw.PineBlog
{
/// <summary>
/// Represents the possible PineBlog options for services.
/// </summary>
public class PineBlogOptions
{
/// <summary>
/// Blog Title.
/// </summary>
public string Title { get; set; }
public string Description { get; set; }
public int ItemsPerPage { get; set; }
public string PagingUrlPartFormat { get; set; }
public string CategoryUrlPartFormat { get; set; }
public bool CreateAndSeedDatabases { get; set; }

/// <summary>
/// The version of the current running code.
/// Blog description.
/// </summary>
public string Version { get; set; }
public string Description { get; set; }

/// <summary>
/// The theme for the blog, defaults to "default".
/// Number of items per page.
/// </summary>
public string Theme { get; set; } = "default";
public int ItemsPerPage { get; set; }

/// <summary>
/// Cover URL.
Expand All @@ -38,6 +36,20 @@ public class PineBlogOptions
/// </summary>
public string CoverLink { get; set; }

public string PagingUrlPartFormat { get; set; }
public string CategoryUrlPartFormat { get; set; }
public bool CreateAndSeedDatabases { get; set; }

/// <summary>
/// The version of the current running code.
/// </summary>
public string Version { get; set; }

/// <summary>
/// The theme for the blog, defaults to "default".
/// </summary>
public string Theme { get; set; } = "default";

/// <summary>
/// The URL of the location where the images and other files are stored.
/// Can be the web host, a CDN or a local host.
Expand Down
16 changes: 0 additions & 16 deletions src/Opw.PineBlog.Core/ApplicationBuilderExtensions.cs

This file was deleted.

Loading

0 comments on commit 3386438

Please sign in to comment.