-
Notifications
You must be signed in to change notification settings - Fork 0
Hydration Repository #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
748f5d3
Preps
musakai 917e4bb
Hydration Repository Implementation
musakai b11bb91
DataContext fix
musakai 500f518
Update Domca.Core/Repositories/IHydrationRepository.cs
musakai 9e9a655
Update Domca.EntityFrameworkCore/Repositories/HydrationRepository.cs
musakai 5b63f76
Update Domca.EntityFrameworkCore/Repositories/HydrationRepository.cs
musakai 914ad33
Update Domca.EntityFrameworkCore/Repositories/HydrationRepository.cs
musakai 80e7844
Update Domca.Core/Repositories/IHydrationRepository.cs
musakai e0d68a6
Update Domca.Core/Repositories/IHydrationRepository.cs
musakai 10f6c18
Update Domca.Core/Repositories/IHydrationRepository.cs
musakai 70734ca
Update Domca.Core/Repositories/IHydrationRepository.cs
musakai 4d9a27b
Update Domca.EntityFrameworkCore/Repositories/HydrationRepository.cs
musakai 89a87b3
Update Domca.EntityFrameworkCore/Repositories/HydrationRepository.cs
musakai a08f8c1
Update Domca.EntityFrameworkCore/Repositories/HydrationRepository.cs
musakai 933b923
Fixes
musakai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| using Domca.Core.Entities; | ||
| using Domca.Core.Entities.IDs; | ||
|
|
||
| namespace Domca.Core.Repositories; | ||
|
|
||
| /// <summary> | ||
| /// Defines a contract for managing and retrieving hydration records within a repository. | ||
| /// </summary> | ||
| /// <remarks>This interface provides asynchronous methods for querying hydration record and synchronous methods | ||
| /// for adding, updating, and removing hydration records. Implementations are expected to handle data persistence and ensure | ||
| /// thread safety where appropriate. Methods that accept a <see cref="CancellationToken"/> allow callers to cancel | ||
| /// ongoing operations, which is useful for long-running queries or when integrating with responsive | ||
| /// applications.</remarks> | ||
| public interface IHydrationRepository | ||
| { | ||
| // Read Methods | ||
| #region Read Methods | ||
|
|
||
| /// <summary> | ||
| /// Asynchronously retrieves a hydration record by its unique identifier. | ||
| /// </summary> | ||
| /// <param name="id">The unique identifier of the hydration record to retrieve.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A task that represents the asynchronous operation. The task result contains the hydration record if found; | ||
| /// otherwise, null.</returns> | ||
| Task<HydrationRecord?> GetByIdAsync(HydrationRecordId id, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the hydration record associated with the specified user, if one exists. | ||
| /// </summary> | ||
| /// <param name="id">The identifier of the user whose hydration record is to be retrieved. Cannot be null.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A task that represents the asynchronous operation. The task result contains the hydration record for the | ||
| /// specified user, or <c>null</c> if no record is found.</returns> | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Task<List<HydrationRecord>?> GetByUserAsync(UserId id, CancellationToken cancellationToken = default); | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Retrieves all hydration records associated with the specified user. | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| /// <param name="id">The unique identifier of the user whose hydration records are to be retrieved.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> | ||
| /// <returns>A list of hydration records for the specified user. The list is empty if the user has no hydration records.</returns> | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Task<List<HydrationRecord>?> GetByUserForTodayAsync(UserId id, CancellationToken cancellationToken = default); | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Retrieves all hydration records for the specified user within the week containing the given reference date. | ||
| /// </summary> | ||
| /// <remarks>The week is defined as starting on Monday and ending before the following Monday, based on | ||
| /// the provided reference date.</remarks> | ||
| /// <param name="id">The unique identifier of the user whose hydration records are to be retrieved.</param> | ||
| /// <param name="referenceDate">A date used to determine the target week. The method returns records from the week that includes this date, | ||
| /// starting on Monday.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A list of hydration records for the specified user that fall within the week containing the reference date. The | ||
| /// list is empty if no records are found.</returns> | ||
| Task<List<HydrationRecord>?> GetByUserForWeekAsync(UserId id, DateTime referenceDate, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Retrieves all hydration records for the specified user within the given month and year. | ||
| /// </summary> | ||
| /// <param name="id">The unique identifier of the user whose hydration records are to be retrieved.</param> | ||
| /// <param name="month">The month for which to retrieve records, specified as an integer from 1 (January) to 12 (December).</param> | ||
| /// <param name="year">The year for which to retrieve records.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A task that represents the asynchronous operation. The task result contains a list of hydration records for the | ||
| /// specified user and month, or <see langword="null"/> if no records are found.</returns> | ||
| Task<List<HydrationRecord>?> GetByUserForMonthAsync(UserId id, int month, int year, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Retrieves all hydration records for the specified user and year asynchronously. | ||
| /// </summary> | ||
| /// <param name="id">The unique identifier of the user whose hydration records are to be retrieved.</param> | ||
| /// <param name="year">The year for which to retrieve hydration records. Must be a four-digit year.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A list of hydration records for the specified user and year, or <see langword="null"/> if no records are found.</returns> | ||
| Task<List<HydrationRecord>?> GetByUserForYearAsync(UserId id, int year, CancellationToken cancellationToken = default); | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #endregion | ||
|
|
||
| // Write Methods | ||
| #region Write Methods | ||
|
|
||
| /// <summary> | ||
| /// Adds a hydration record to the data context for tracking and persistence. | ||
| /// </summary> | ||
| /// <remarks>The record is not saved to the database until changes are committed. This method does not | ||
| /// check for duplicate records.</remarks> | ||
| /// <param name="hydrationRecord">The hydration record to add to the context. Cannot be null.</param> | ||
| void Add(HydrationRecord hydrationRecord); | ||
|
|
||
| /// <summary> | ||
| /// Adds a collection of hydration records to the data context for insertion. | ||
| /// </summary> | ||
| /// <remarks>This method stages the specified hydration records for addition to the underlying data store. | ||
| /// Changes are not persisted until the context is saved. If any record in the collection already exists in the | ||
| /// context, it may result in duplicate entries unless handled appropriately.</remarks> | ||
| /// <param name="hydrationRecords">The collection of <see cref="HydrationRecord"/> objects to add. Cannot be null.</param> | ||
| void AddRange(List<HydrationRecord> hydrationRecords); | ||
|
|
||
| /// <summary> | ||
| /// Updates the specified hydration record in the data store. | ||
| /// </summary> | ||
| /// <remarks>If the specified record does not exist in the data store, no changes will be made. This | ||
| /// method does not save changes to the database; call SaveChanges to persist updates.</remarks> | ||
| /// <param name="hydrationRecord">The hydration record to update. Must not be null.</param> | ||
| void Update(HydrationRecord hydrationRecord); | ||
|
|
||
| /// <summary> | ||
| /// Removes the specified hydration record from the data context. | ||
| /// </summary> | ||
| /// <param name="hydrationRecord">The hydration record to remove from the context. Cannot be null.</param> | ||
| void Remove(HydrationRecord hydrationRecord); | ||
|
|
||
| #endregion | ||
| } | ||
134 changes: 134 additions & 0 deletions
134
Domca.EntityFrameworkCore/Repositories/HydrationRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| using Domca.Core.Entities; | ||
| using Domca.Core.Entities.IDs; | ||
| using Domca.Core.Repositories; | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace Domca.EntityFrameworkCore.Repositories; | ||
|
|
||
| /// <summary> | ||
| /// Provides access to hydration-related data operations using the specified data context. | ||
| /// </summary> | ||
| /// <param name="context">The data context used to interact with the underlying hydration data store. Cannot be null.</param> | ||
| public class HydrationRepository(DataContext context) : IHydrationRepository | ||
| { | ||
| /// <summary> | ||
| /// Asynchronously retrieves a hydration record by its unique identifier. | ||
| /// </summary> | ||
| /// <param name="id">The unique identifier of the hydration record to retrieve.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A task that represents the asynchronous operation. The task result contains the hydration record if found; | ||
| /// otherwise, null.</returns> | ||
| public async Task<HydrationRecord?> GetByIdAsync(HydrationRecordId id, CancellationToken cancellationToken = default) | ||
| => await context.HydrationRecords.FindAsync([id], cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Asynchronously retrieves all hydration records associated with the specified user. | ||
| /// </summary> | ||
| /// <param name="id">The unique identifier of the user whose hydration records are to be retrieved.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A task that represents the asynchronous operation. The task result contains a list of hydration records for the specified user. The list is empty if the user has no hydration records.</returns> | ||
| public async Task<List<HydrationRecord>?> GetByUserAsync(UserId id, CancellationToken cancellationToken = default) | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| => await context.HydrationRecords | ||
| .Where(h => h.UserId == id) | ||
| .ToListAsync(cancellationToken); | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Asynchronously retrieves all hydration records for the specified user that were created on the current UTC date. | ||
| /// </summary> | ||
| /// <remarks>The method compares record dates using UTC time. If called near midnight UTC, results may | ||
| /// differ from local time expectations.</remarks> | ||
| /// <param name="id">The unique identifier of the user whose hydration records are to be retrieved.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A list of hydration records for the specified user for today, or <see langword="null"/> if no records are found.</returns> | ||
| public async Task<List<HydrationRecord>?> GetByUserForTodayAsync(UserId id, CancellationToken cancellationToken = default) | ||
| => await context.HydrationRecords | ||
| .Where(h => h.UserId == id | ||
| && h.Date.Date == DateTime.UtcNow.Date) | ||
| .ToListAsync(cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Retrieves all hydration records for the specified user within the week containing the given reference date. | ||
| /// </summary> | ||
| /// <remarks>The week is defined as starting on Monday and ending before the following Monday, based on | ||
| /// the provided reference date.</remarks> | ||
| /// <param name="id">The unique identifier of the user whose hydration records are to be retrieved.</param> | ||
| /// <param name="referenceDate">A date used to determine the target week. The method returns records from the week that includes this date, | ||
| /// starting on Monday.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A list of hydration records for the specified user that fall within the week containing the reference date. The | ||
| /// list is empty if no records are found.</returns> | ||
| public async Task<List<HydrationRecord>?> GetByUserForWeekAsync(UserId id, DateTime referenceDate, CancellationToken cancellationToken = default) | ||
| { | ||
| int diff = (7 + (referenceDate.DayOfWeek - DayOfWeek.Monday)) % 7; | ||
|
|
||
| var startOfWeek = referenceDate.Date.AddDays(-1 * diff); | ||
musakai marked this conversation as resolved.
Dismissed
Show dismissed
Hide dismissed
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| var endOfWeek = startOfWeek.AddDays(7); | ||
|
|
||
| return await context.HydrationRecords | ||
| .Where(h => h.UserId == id | ||
| && h.Date >= startOfWeek | ||
| && h.Date < endOfWeek) | ||
| .ToListAsync(cancellationToken); | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves all hydration records for the specified user within the given month and year. | ||
| /// </summary> | ||
| /// <param name="id">The unique identifier of the user whose hydration records are to be retrieved.</param> | ||
| /// <param name="month">The month for which to retrieve records, specified as an integer from 1 (January) to 12 (December).</param> | ||
| /// <param name="year">The year for which to retrieve records.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A task that represents the asynchronous operation. The task result contains a list of hydration records for the | ||
| /// specified user and month, or <see langword="null"/> if no records are found.</returns> | ||
| public async Task<List<HydrationRecord>?> GetByUserForMonthAsync(UserId id, int month, int year, CancellationToken cancellationToken = default) | ||
| => await context.HydrationRecords | ||
| .Where(h => h.UserId == id | ||
| && h.Date.Month == month | ||
| && h.Date.Year == year) | ||
| .ToListAsync(cancellationToken); | ||
|
|
||
| /// <summary> | ||
| /// Retrieves all hydration records for the specified user and year. | ||
| /// </summary> | ||
| /// <param name="id">The unique identifier of the user whose hydration records are to be retrieved.</param> | ||
| /// <param name="year">The year for which to retrieve hydration records. Must be a four-digit year.</param> | ||
| /// <param name="cancellationToken">A cancellation token that can be used to cancel the asynchronous operation.</param> | ||
| /// <returns>A list of hydration records for the specified user and year, or <see langword="null"/> if no records are found.</returns> | ||
| public async Task<List<HydrationRecord>?> GetByUserForYearAsync(UserId id, int year, CancellationToken cancellationToken = default) | ||
| => await context.HydrationRecords | ||
| .Where(h => h.UserId == id | ||
| && h.Date.Year == year) | ||
| .ToListAsync(cancellationToken); | ||
musakai marked this conversation as resolved.
Show resolved
Hide resolved
musakai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Adds a hydration record to the data context for tracking and persistence. | ||
| /// </summary> | ||
| /// <remarks>The record is not saved to the database until changes are committed. This method does not | ||
| /// check for duplicate records.</remarks> | ||
| /// <param name="hydrationRecord">The hydration record to add to the context. Cannot be null.</param> | ||
| public void Add(HydrationRecord hydrationRecord) => context.HydrationRecords.Add(hydrationRecord); | ||
|
|
||
| /// <summary> | ||
| /// Adds a collection of hydration records to the data context for insertion. | ||
| /// </summary> | ||
| /// <remarks>This method stages the specified hydration records for addition to the underlying data store. | ||
| /// Changes are not persisted until the context is saved. If any record in the collection already exists in the | ||
| /// context, it may result in duplicate entries unless handled appropriately.</remarks> | ||
| /// <param name="hydrationRecords">The collection of <see cref="HydrationRecord"/> objects to add. Cannot be null.</param> | ||
| public void AddRange(List<HydrationRecord> hydrationRecords) => context.HydrationRecords.AddRange(hydrationRecords); | ||
|
|
||
| /// <summary> | ||
| /// Updates the specified hydration record in the data store. | ||
| /// </summary> | ||
| /// <remarks>If the specified record does not exist in the data store, no changes will be made. This | ||
| /// method does not save changes to the database; call SaveChanges to persist updates.</remarks> | ||
| /// <param name="hydrationRecord">The hydration record to update. Must not be null.</param> | ||
| public void Update(HydrationRecord hydrationRecord) => context.HydrationRecords.Update(hydrationRecord); | ||
|
|
||
| /// <summary> | ||
| /// Removes the specified hydration record from the data context. | ||
| /// </summary> | ||
| /// <param name="hydrationRecord">The hydration record to remove from the context. Cannot be null.</param> | ||
| public void Remove(HydrationRecord hydrationRecord) => context.HydrationRecords.Remove(hydrationRecord); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.