Skip to content

Instantly share code, notes, and snippets.

View Grinderofl's full-sized avatar

Nero Sule Grinderofl

View GitHub Profile
@Grinderofl
Grinderofl / Startup.cs
Created December 19, 2019 14:13
UnCSS PoC
services.AddWebOptimizer(x =>
{
x.AddBundle("/css/site.css", "text/css;charset=UTF-8", "static/css/styles.css")
.UnCss()
.MinifyCss()
.AutoPrefixCss()
.Concatenate()
.FingerprintUrls();
});
@Grinderofl
Grinderofl / project-summary-query.sql
Last active October 19, 2019 19:35
Dashboard Summary Query
-- Outermost query for the final projection of the calculated values
SELECT
ProjectId, -- Primary key of the project
ProjectName, -- Name of the project
ProjectArchived, -- Whether the project is archived
ProjectQuotedDayRate, -- Daily rate quoted for the client
ProjectBaseDayRate, -- Estimated day rate for the project (cost to the company)
ProjectTotalBudget, -- Total budget (amount billed to the client) of the project
CurrencyCode, -- Currency code (presently only 'GDP')
ClientId, -- Primary key of the client
@Grinderofl
Grinderofl / Artifacts-DotNetCore-Ef.cake
Created June 19, 2019 09:02
Specify starting migration
#addin nuget:?package=Newtonsoft.Json&version=12.0.1
using Newtonsoft.Json;
public class EfMigration
{
public string Id { get; set; }
public string Name { get; set; }
public string SafeName { get; set; }
}
@Grinderofl
Grinderofl / RouteConstraintExample.cs
Created May 31, 2019 09:12
Custom route constraint
public class PageRouteConstraint : IRouteConstraint
{
private static readonly Regex regex = new Regex($"^page=(\\d+)$");
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection != RouteDirection.IncomingRequest || !values.TryGetValue(routeKey, out var page))
{
return false;
}
@Grinderofl
Grinderofl / AbstractAsyncInitializationMiddleware.cs
Created May 15, 2019 12:11
Initialize Database from middleware
public abstract class AbstractAsyncInitializationMiddleware
{
private readonly RequestDelegate next;
private readonly ILogger logger;
private Task initializationTask;
// ReSharper disable AccessToModifiedClosure
protected AbstractAsyncInitializationMiddleware(RequestDelegate next, IApplicationLifetime lifetime, ILogger logger)
{
this.next = next;
@Grinderofl
Grinderofl / TIcketsQuery.cs
Created April 5, 2019 08:03
Projection Example
public async Task<Model> Handle(Request request, CancellationToken cancellationToken)
{
var query = context.Tickets.AsNoTracking().AsQueryable();
query = FilterQuery(query, request);
var total = await query.CountAsync(cancellationToken);
query = OrderQuery(query, request);
query = PageQuery(query, request);
@Grinderofl
Grinderofl / MySigninManager.cs
Created January 9, 2019 12:54
Update last login date
public class MySigninManager : SignInManager<IdentityUser>
{
public MySigninManager(UserManager<IdentityUser> userManager, IHttpContextAccessor contextAccessor,
IUserClaimsPrincipalFactory<IdentityUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor,
ILogger<SignInManager<IdentityUser>> logger, IAuthenticationSchemeProvider schemes)
: base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes)
{
}
protected override async Task<SignInResult> SignInOrTwoFactorAsync(IdentityUser user, bool isPersistent, string loginProvider = null, bool bypassTwoFactor = false)
@Grinderofl
Grinderofl / ImplementationGenerator.cs
Created November 1, 2018 09:12
Interface implementation generator
public class ImplementationGenerator
{
private const string AddServiceFormat = "services.AddScoped<{0},{1}>();";
public string GenerateInterfaceImplementationRegistrations(IEnumerable<Assembly> assemblies)
{
var types = assemblies.SelectMany(x => x.GetExportedTypes())
.Where(x => x.GetInterfaces().Any())
.Where(x => !x.IsAbstract && !x.IsInterface)
.Where(x => x.Namespace.EndsWith(".Impl"));
@Grinderofl
Grinderofl / CreateOrUpdateOperation.cs
Last active October 30, 2018 14:11
Actual idempotent Insert or Update Migration Script for EF Core
public class CreateOrUpdateOperation : MigrationOperation
{
public EntityWithName Entity { get; }
public Type EntityType { get; }
public CreateOrUpdateOperation(EntityWithName entity, Type entityType)
{
Entity = entity;
EntityType = entityType;
}
@Grinderofl
Grinderofl / FilterFieldExpressionFactory.cs
Last active October 17, 2018 17:41
FilterFieldExpressionFactory.cs
public static class FilterFieldExpressionFactory
{
public static Expression<Func<T, bool>> Create<T>(IEnumerable<FieldResult> filterFields) where T : class =>
FilterExpressionFactory<T>.Create(filterFields);
private static class FilterExpressionFactory<T> where T : class
{
private static Expression<Func<T, bool>> CreateFieldValue(FieldResult field, FieldValue fieldValue, ExpressionStarter<T> fieldPredicate) =>
field.CriteriaType == CriteriaType.Is
? CriteriaIs(field, fieldValue, fieldPredicate)