Skip to content

Instantly share code, notes, and snippets.

View alastair-todd's full-sized avatar

Alastair Todd alastair-todd

  • North Yorkshire, UK
View GitHub Profile
@alastair-todd
alastair-todd / PostgresFullTextSearchApi.cs
Created March 28, 2022 08:33
Form a text search query (tsquery) with dbcontext / ado.net
public async Task<NpgsqlTsQuery> ToTsQuery(DbContext dbContext, string searchTerm)
{
var connection = dbContext.Database.GetDbConnection() as NpgsqlConnection;
await using var cmd = new NpgsqlCommand("select plainto_tsquery('english', @search_term)", connection);
cmd.CommandType = CommandType.Text;
if (connection!.State != ConnectionState.Open) connection.Open();
cmd.Parameters.Add(new NpgsqlParameter("search_term", searchTerm));
@alastair-todd
alastair-todd / PostgresFullTextSearchApi.cs
Last active March 28, 2022 08:34
Form a text search vector (tsvector) with dbcontext / ado.net
public async Task<NpgsqlTsVector> ToTsVector(DbContext dbContext, string content)
{
var connection = dbContext.Database.GetDbConnection() as NpgsqlConnection;
await using var cmd = new NpgsqlCommand("select to_tsvector('english', @content)", connection );
cmd.CommandType = CommandType.Text;
if (connection!.State != ConnectionState.Open) connection.Open();
cmd.Parameters.Add(new NpgsqlParameter("content", content));
@alastair-todd
alastair-todd / http-retry.interceptor.ts
Created April 13, 2022 15:55
Angular interceptor to retry the request given a specific error and also modify the request headers
import { Injectable } from '@angular/core';
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable, of, throwError } from 'rxjs';
import { delay, catchError, switchMap } from 'rxjs/operators';
import { LoggingService } from '@logging/logging.service';
@Injectable()
export class ObjectContextExceptionRetryInterceptor implements HttpInterceptor {
private retryCount = 3;
private retryWaitTime = 1500;
@alastair-todd
alastair-todd / cypress-amplify-js.ts
Last active February 27, 2024 14:11
Cypress extension for logging in with amplify-js auth
/**
* amplify-js / cognito auth helper
* specific personas are logged-in and their tokens are cached to save on round-trips.
*/
import { Amplify } from 'aws-amplify';
import { signIn, SignInOutput, signOut } from '@aws-amplify/auth';
import { ResourcesConfig } from '@aws-amplify/core';
console.log('configuring amplify');
public static class PasswordGenerator
{
// https://stackoverflow.com/questions/65393858/force-the-password-to-contain-lower-case-upper-case-special-character-and-numb
private const string Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const string Lowercase = "abcdefghijklmnopqrstuvwxyz";
private const string Symbol = "!-_*+&$#@')(%";
private const string Number = "0123456789";