ruby
class Order < ApplicationRecord
  class InvalidTransition < StandardError; end

  enum status: { pending: 0, paid: 1, shipped: 2, cancelled: 3 }

  has_many :order_transitions, -> { order(:created_at) }, dependent: :destroy

Order State Machine With Guarded Transitions and an Audit Trail in Rails

rails state-machine activerecord
by codesnips 3 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Generating URL Slugs from Titles with a Laravel Model Observer

laravel eloquent observers
by codesnips 4 tabs
java
package com.shop.orders.events;

import java.math.BigDecimal;
import java.time.Instant;

public record OrderPlacedEvent(

Transactional Domain Events With Spring's ApplicationEventPublisher and @TransactionalEventListener

spring spring-boot domain-events
by codesnips 4 tabs
rust
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize)]
pub struct SignupRequest {
    pub email: String,
    pub password: String,

Accumulating Field-Level Validation Errors in Rust Signup Forms

rust validation error-handling
by codesnips 3 tabs
go
package workpool

import (
	"context"
	"sync"
)

Bounded Worker Pool Processing Jobs from a Buffered Channel in Go

go concurrency worker-pool
by codesnips 3 tabs
typescript
export interface Todo {
  id: string;
  title: string;
  done: boolean;
  pending: boolean;
}

Optimistic To-Do Toggle in React with Rollback via useReducer

react optimistic-ui hooks
by codesnips 3 tabs
ruby
class CreateStripeEvents < ActiveRecord::Migration[7.1]
  def change
    create_table :stripe_events do |t|
      t.string :stripe_event_id, null: false
      t.string :event_type, null: false
      t.string :status, null: false, default: "received"

Idempotent Stripe Webhook Processing in Rails with a Durable Event Log

rails stripe webhooks
by codesnips 4 tabs
php
<?php

namespace App\Http\Controllers;

use App\Jobs\ProcessWebhook;
use App\Support\WebhookSignature;

Debounce Duplicate Webhooks in Laravel by Dispatching a Delayed Queued Job

laravel webhooks queues
by codesnips 3 tabs
javascript
class TokenBucket {
  constructor(capacity, refillRatePerSec) {
    this.capacity = capacity;
    this.refillRate = refillRatePerSec;
    this.tokens = capacity;
    this.lastRefill = Date.now();

Token Bucket Rate Limiter as Express Middleware

express rate-limiting token-bucket
by codesnips 3 tabs
ruby
namespace :cleanup do
  desc "Enqueue a job to purge expired sessions"
  task expired_sessions: :environment do
    job = ExpiredSessionCleanupJob.perform_later
    Rails.logger.info("[cleanup:expired_sessions] enqueued job #{job.job_id}")
  end

Recurring Cleanup with a Rake Task and an Idempotent Active Job in Rails

rails background-jobs active-job
by codesnips 4 tabs
php
<?php

namespace App\Policies;

use App\Models\Post;
use App\Models\User;

Authorize Post Editing With a Laravel Policy and Gate the Controller

laravel authorization policies
by codesnips 3 tabs
rust
use std::sync::Arc;
use std::time::Duration;

use futures::stream::{FuturesUnordered, StreamExt};
use reqwest::Client;
use tokio::sync::Semaphore;

Bounded Concurrent HTTP Fan-Out With FuturesUnordered and Semaphore in Rust

rust async tokio
by codesnips 3 tabs