Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created December 8, 2025 19:08
Show Gist options
  • Select an option

  • Save dcomartin/5a70241fcd4b285ad52f82e2c7c78a1a to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/5a70241fcd4b285ad52f82e2c7c78a1a to your computer and use it in GitHub Desktop.
public class AddMemberHandler
{
private const int MaxMembers = 100_000;
private readonly IDbConnection _connection;
public AddMemberHandler(IDbConnection connection)
{
_connection = connection;
}
public void Handle(Guid groupChatId, Guid userId)
{
using var trx = _connection.BeginTransaction();
var memberCount = _connection.ExecuteScalar<int>(
"SELECT COUNT(*) FROM GroupChatMembers WHERE GroupChatId = @Id",
new { Id = groupChatId }, trx);
if (memberCount >= MaxMembers)
throw new InvalidOperationException("GroupChat is full");
_connection.Execute(
"INSERT INTO GroupChatMembers (GroupChatId, UserId) VALUES (@GroupChatId, @UserId)",
new { GroupChatId = groupChatId, UserId = userId }, trx);
trx.Commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment