Skip to content

Instantly share code, notes, and snippets.

View sulton-max's full-sized avatar
📌
Focusing

Sultonbek Murodbekovich Rakhimov sulton-max

📌
Focusing
View GitHub Profile
public static class CollectionExtensions
{
// public static ICollection<(TSource FirstItem, TSource SecondItem)> ZipIntersectBy<TSource, TKey>(
// this ICollection<TSource> first,
// ICollection<TSource> second,
// Func<TSource, TKey> keySelector
// )
// {
// // solution #1
@sulton-max
sulton-max / ReferenceExtensions.cs
Created August 6, 2023 14:09
Extension to get variable reference for managed and unmanaged types
public static class ReferenceExtensions
{
public static string GetReferenceForManagedType<TValue>(TValue value) =>
GCHandle.Alloc(value, GCHandleType.Pinned).AddrOfPinnedObject().ToString();
public static unsafe string GetReferenceForUnmanagedType<TValue>(ref TValue value) where TValue : unmanaged
{
fixed (TValue* pointer = &value)
{
return ((IntPtr)pointer).ToString();
@sulton-max
sulton-max / WebStorm.cmd
Created July 23, 2023 05:40 — forked from M1chaelTran/WebStorm.cmd
Add `Open with WebStorm` to Windows right click context menu
@echo off
:: change the path below to match your installed version
SET WebStormPath=C:\Program Files\JetBrains\WebStorm 2017.2.2\bin\webstorm64.exe
echo Adding file entries
@reg add "HKEY_CLASSES_ROOT\*\shell\WebStorm" /t REG_SZ /v "" /d "Open with WebStorm" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\WebStorm" /t REG_EXPAND_SZ /v "Icon" /d "%WebStormPath%,0" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\WebStorm\command" /t REG_SZ /v "" /d "%WebStormPath% \"%%1\"" /f
public class EfCoreDataStorageBroker : DbContext, IDataStorageBroker
{
public DbSet<Book> Books => Set<Book>();
public DbSet<User> Users => Set<User>();
private static DbContextOptions<EfCoreDataStorageBroker> GetOptions()
{
var optionsBuilder = new DbContextOptionsBuilder<EfCoreDataStorageBroker>();
optionsBuilder.UseInMemoryDatabase("Tigernet");
return optionsBuilder.Options;
/// <summary>
/// Defines methods for logger broker
/// </summary>
public interface ILoggerBroker
{
/// <summary>
/// Formats and writes an critical log message
/// </summary>
/// <param name="messageTemplate">Log message template</param>
/// <param name="arg0">Argument 0</param>
@sulton-max
sulton-max / Moq.cs
Created May 5, 2023 14:08
Mocking with Moq
// Initializing a mock object
private readonly Mock<ILogger<BookService>> _loggerMock = new();
// Verifying a call
_loggerMock.Verify(x => x.Log(It.IsAny<LogLevel>(),
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => o.ToString()!.Contains($"Book with id {id} was not found")),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception, string>>()!),
Times.Once);
@sulton-max
sulton-max / TimeOperations.ts
Created April 14, 2023 13:45
Typescript Time operations
getFormattedDate(date: Date): string {
date = new Date(date.toLocaleString());
const now = new Date();
const diff = now.getTime() - date.getTime();
// Time constants in milliseconds
const minute = 60 * 1000;
const hour = 60 * minute;
@sulton-max
sulton-max / ApiResponse.ts
Last active August 22, 2023 03:26
Axios Client
import type { ProblemDetails } from "@/models/resources/problemDetails";
export class ApiResponse<T> {
public response: T | null;
public error: ProblemDetails | null;
public status: number;
constructor(response: T | null, error: ProblemDetails | null, status: number) {
this.response = response;
this.error = error;
@sulton-max
sulton-max / ISecurityTokenService.cs
Created April 8, 2023 16:32
Custom Security Token Provider
public interface ISecurityTokenService
{
string Generate(string id, SecurityTokenType type);
bool Validate(string token, string id, SecurityTokenType type, TimeSpan expirationTime);
}
public class HttpContextExtensions
{
#region Authentication
public async static ValueTask<IEnumerable<AuthenticationScheme>> GetAuthenticationProviders(this HttpContext context)
{
var provider = context.RequestServices.GetService<IAuthenticationSchemeProvider>();
return provider is not null
? (await provider.GetAllSchemesAsync()).Where(x => !string.IsNullOrWhiteSpace(x.DisplayName))
: Enumerable.Empty<AuthenticationScheme>();