Account Settings Client (AccountSettingsClient) Class
Namespace: Cxt.Account.Settings.API.ClientsImplements: IAccountSettingsClient
Concrete implementation for IAccountSettingsClient, handling direct database interactions for CRUD operations on account visibility settings using IDatabaseConnection.
Purpose
- Performs CRUD operations against the database for account settings.
- Implements an "upsert" logic for adding preferences (updates if existing, else inserts).
- Abstracts database communication specifics from the service layer.
Dependencies
IDatabaseConnection: Injected via constructor. Provides methods for database connection management and SQL execution. (Details inDatabase Connectivity Strategy Overview).SqlStatementConstants: Provides SQL query strings. (Details inSQL Statement Constants).
Method Implementations
Add Client Account Settings Preferences Client
csharp
public async Task<bool> AddClientAccountSettingsPreferencesClient(AddAccountSettingDto request)- Description: Persists a new account setting preference. Implements an "upsert" logic: updates if the setting exists, otherwise inserts a new one.
- Parameters:
Parameter Type Description requestAddAccountSettingDtoContains details for the new setting (expects AccountHiddenas an integer). - Returns:
Task<bool>-trueif add/update is successful;falseon failure (e.g., after all retries). - Details:
- Behavior:
- Checks for an existing setting using
SqlStatementConstants.RetrieveSingleAccountSql(parameters: GCN, ProfileId, AccountNumber, AccountType fromrequest). - If an existing setting is found, it calls
UpdateClientAccountSettingsPreferencesClient(request)to update it. - If no existing setting is found, it uses
SqlStatementConstants.InsertAccountSettingSqlto insert a new record.- Maps fields from the
AddAccountSettingDtoto the SQL parameters. - Sets
DateCreatedandDateUpdatedtoDateTime.UtcNow(formatted as "yyyy-MM-dd HH:mm:ss").
- Maps fields from the
- Checks for an existing setting using
- Retry Logic:
- Catches
Microsoft.Data.Sqlite.SqliteExceptionspecifically forSqliteErrorCode == 5(database locked/busy). - Retries the operation up to 3 times, with a 100ms delay (
Task.Delay(100)) between each attempt.
- Catches
- Error Handling: Logs other exceptions to
Console.WriteLine. The database connection is closed in afinallyblock associated with the database operations.
- Behavior:
Retrieve Client Account Settings Preferences Client
csharp
public async Task<List<AccountSettingsPreferencesDto>> RetrieveClientAccountSettingsPreferencesClient(string gcn, CancellationToken cancellationToken)- Description: Retrieves all stored account setting preferences for a client based on their GCN.
- Parameters:
Parameter Type Description gcnstringThe Global Client Number. cancellationTokenCancellationTokenToken for observing cancellation requests. - Returns:
Task<List<AccountSettingsPreferencesDto>>- A list ofAccountSettingsPreferencesDtoobjects. Returns an empty list if no settings are found or if an error occurs. - Details:
- Behavior:
- Uses
SqlStatementConstants.RetrieveAccountSettingSqlwith the providedgcnas a parameter. - Opens the database connection using
_dbConnection.OpenAsync(). - Executes the query using
_dbConnection.ExecuteReaderAsync(). - Iterates through the results from the
DbDataReader:- For each row, it creates an
AccountSettingsPreferencesDtoobject. - Maps database columns to the DTO properties:
GCN,ProfileId,AccountName,AccountNumber,AccountType,AccountStatus,ProductSegment,BusinessUnit,AccountHidden(integer from DB), andDateUpdated.
- For each row, it creates an
- Adds each populated DTO to a list.
- Uses
- Error Handling: Logs exceptions to
Console.WriteLine. The database connection is closed in afinallyblock.
- Behavior:
Update Client Account Settings Preferences Client
csharp
public async Task<bool> UpdateClientAccountSettingsPreferencesClient(AddAccountSettingDto request)- Description: Updates an existing account setting preference in the database.
- Parameters:
Parameter Type Description requestAddAccountSettingDtoContains the updated details. The target account is identified by GCN, AccountNumber, and ProfileId within the DTO. - Returns:
Task<bool>-trueif the update is successful;falseon failure. - Details:
- Behavior:
- Uses
SqlStatementConstants.UpdateAccountSettingSql. - Maps fields from the
AddAccountSettingDtoto SQL parameters:GCN,AccountName(defaults toAccountNumber-AccountTypeifrequest.AccountNameis empty/null),AccountNumber,AccountType,AccountHidden(integer value from DTO).
- Sets the
DateUpdatedSQL parameter toDateTime.UtcNow(formatted as "yyyy-MM-dd HH:mm:ss"). - Opens the connection and executes the non-query command.
- Uses
- Error Handling: Logs exceptions to
Console.WriteLine. The database connection is closed in afinallyblock.
- Behavior:
Delete Client Account Settings Preferences Client
csharp
public async Task<bool> DeleteClientAccountSettingsPreferencesClient(AccountSettingsDeleteRequest request)- Description: Deletes an account setting preference from the database.
- Parameters:
Parameter Type Description requestAccountSettingsDeleteRequestSpecifies the GCN, AccountNumber, and AccountType of the preference to delete. - Returns:
Task<bool>-trueif the deletion is successful;falseon failure. - Details:
- Behavior:
- Uses
SqlStatementConstants.DeleteAccountSettingSql. - Maps fields from the
AccountSettingsDeleteRequest(GCN,AccountNumber,AccountType) to SQL parameters. - Opens the connection and executes the non-query command.
- Uses
- Error Handling: Logs exceptions to
Console.WriteLine. The database connection is closed in afinallyblock.
- Behavior:
Key Operations and Logic
- Database Interaction: All database operations are delegated to the
IDatabaseConnectionservice. - SQL Centralization: SQL queries are sourced from
SqlStatementConstants. - Upsert for Add: The
AddClient...method implements update-or-insert logic. - Date Management:
DateCreatedandDateUpdatedare set toDateTime.UtcNowand formatted as "yyyy-MM-dd HH:mm:ss" for database storage. - Connection Lifecycle: Database connections are explicitly opened before an operation and closed in a
finallyblock to ensure resource release. - Logging: Current implementation uses
Console.WriteLinefor error logging. For production, a structured logging framework (e.g., Serilog, NLog,Microsoft.Extensions.Logging) is recommended. - Backend Agnosticism: The use of
IDatabaseConnectionallows potential support for different database systems by providing a corresponding implementation.