Skip to content

Instantly share code, notes, and snippets.

@rebelweb
rebelweb / Dockerfile
Created February 10, 2020 03:06
Dot Net Core API Docker Setup
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
# Set working directory
WORKDIR /app
# Copy source code
COPY . ./
# Restore packages
RUN cd src && dotnet restore
@rebelweb
rebelweb / SwashbuckleHelper.cs
Created February 7, 2020 05:41
Swashbuckle Helper
using System;
using System.IO;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Builder;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace DayHiker.API.Helpers
{
@rebelweb
rebelweb / DataContext.cs
Created February 1, 2020 03:15
Data Converter DbContext
using Microsoft.EntityFrameworkCore;
namespace DataConverter
{
public class DataConverterContext : DbContext
{
public DbSet<User> Users { get; set; }
public DataConverterContext(DbContextOptions opts) : base(opts) { }
@rebelweb
rebelweb / Program.cs
Created February 1, 2020 03:11
A simple C# Program to move data from one database to another
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace DataConverter
{
class Program
{
static string NpgsqlConnectionString => Environment.GetEnvironmentVariable("PSQL_CONN");
@rebelweb
rebelweb / Service.cs
Created November 17, 2019 03:34
SMB File Share Access Service in C#
using System;
using System.Linq;
using System.Text;
using SMBLibrary;
namespace FileShare.Access
{
public interface IClientDTO
{
string IPAddress { get; set; }
@rebelweb
rebelweb / FileShareClient.cs
Created November 17, 2019 03:21
SMB Library File Share Client using IDisposable
using System;
using System.Net;
using FileShare.Access.DTO;
using SMBLibrary;
using SMBLibrary.Client;
namespace FileShare.Access
{
public class FileShareClient : SMB2Client, IDisposable
{
class Category < ApplicationRecord
has_many :article_categories, class_name: ArticleCategory, foreign_key: :category_id, dependent: :destroy
has_many :articles, through: :article_categories
end
class Article < ApplicationRecord
belongs_to :author, class_name: User, foreign_key: :author_id
has_many :article_categories, class_name: ArticleCategory, foreign_key: :article_id, dependent: :destroy
has_many :categories, through: :article_categories
has_many :images, class_name: ArticleImage, foreign_key: :article_id, dependent: :destroy
RSpec.describe UpdateCounterCachesJob, type: :job do
it 'updates all cache columns when they are out of sync' do sql = <<-SQL update categories SET articles_count = 5 SQL
category = create(:category)
ActiveRecord::Base.connection.execute(sql)
category.reload
expect(category.articles_count).to eq(5)
UpdateCounterCachesJob.perform_now
category.reload
expect(category.articles_count).to eq(0)
end
{ "category": [ "article_categories" ] }
@rebelweb
rebelweb / update_counter_cache_job.rb
Created October 17, 2019 11:42
Update Counter Cache Job
class UpdateCounterCacheJob < ApplicationJob attr_accessor :cache_columns
def perform(key = 'all')
self.cache_columns = counter_cache_hash
if key == 'all'
cache_columns.keys.each { |obj| update_cache_columns(obj) }
else
update_cache_columns(key)
end
end