Table of Contents

Class GuildUpdateService

Namespace
FishMMO.Database.Npgsql.Services
Assembly
FishMMO-DB.dll

Service for managing guild update notifications in the database. Tracks when guilds have been modified so clients can poll for changes. Implements execution strategies for automatic retry on transient database failures. Returns DatabaseResult for consistent, safe error handling.

public sealed class GuildUpdateService : BaseService<GuildUpdateEntity>, IGuildUpdateService
Inheritance
GuildUpdateService
Implements
Inherited Members

Remarks

This service manages guild update tracking including:

  • Recording guild updates with atomic UPSERT operations
  • Fetching guilds modified since a given timestamp
  • Cleaning up update records for deleted guilds

Database operations are executed via the BaseService execution wrappers for:

  • Automatic transient failure retry
  • Centralized exception handling and mapping
  • Consistent DatabaseResult pattern

The update tracking pattern allows clients to efficiently poll for changes without needing to re-fetch all guild data on each request.

Constructors

GuildUpdateService(INpgsqlDbContextFactory)

Initializes a new instance of GuildUpdateService.

public GuildUpdateService(INpgsqlDbContextFactory dbContextFactory)

Parameters

dbContextFactory INpgsqlDbContextFactory

DbContext factory for creating contexts.

Exceptions

ArgumentNullException

Thrown when dbContextFactory is null.

Methods

DeleteAsync(long, CancellationToken)

Deletes all update records for a guild.

public Task<DatabaseResult<int>> DeleteAsync(long guildId, CancellationToken cancellationToken = default)

Parameters

guildId long

Guild ID.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<int>>

A DatabaseResult<T> containing the number of records deleted on success, or a DatabaseException on failure.

Remarks

This is an idempotent cleanup operation. Unlike entity delete methods, this method does NOT throw DatabaseEntityNotFoundException when no records exist. Instead, it returns 0 rows deleted. This design supports safe concurrent cleanup where multiple callers may attempt to delete the same records.

Uses a single-statement DELETE.

FetchAsync(List<long>, DateTime, CancellationToken)

Fetches guild update records for specified guilds updated since last fetch.

public Task<DatabaseResult<List<GuildUpdateData>>> FetchAsync(List<long> guildIds, DateTime lastFetch, CancellationToken cancellationToken = default)

Parameters

guildIds List<long>

List of guild IDs to check.

lastFetch DateTime

Timestamp to compare against.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<List<GuildUpdateData>>>

A DatabaseResult<T> containing the list of guild update data on success, or a DatabaseException on failure.

Remarks

Filters by both timestamp and guild ID list.

PersistAsync(long, CancellationToken)

Saves or updates the last update timestamp for a guild using atomic UPSERT.

public Task<DatabaseResult> PersistAsync(long guildId, CancellationToken cancellationToken = default)

Parameters

guildId long

Guild ID.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult>

A DatabaseResult indicating success or containing a DatabaseException on failure.

Remarks

Uses a single-statement PostgreSQL INSERT ... ON CONFLICT DO UPDATE with a conditional update to avoid regressing last_update.