| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Pomodoro.Web.Models; |
| | | 3 | | |
| | | 4 | | namespace Pomodoro.Web.Services.Repositories; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Repository implementation for activity record persistence using IndexedDB |
| | | 8 | | /// </summary> |
| | | 9 | | public class ActivityRepository : IActivityRepository |
| | | 10 | | { |
| | | 11 | | private readonly IIndexedDbService _indexedDb; |
| | | 12 | | private readonly ILogger<ActivityRepository> _logger; |
| | | 13 | | |
| | 33 | 14 | | public ActivityRepository(IIndexedDbService indexedDb, ILogger<ActivityRepository> logger) |
| | 33 | 15 | | { |
| | 33 | 16 | | _indexedDb = indexedDb; |
| | 33 | 17 | | _logger = logger; |
| | 33 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<List<ActivityRecord>> GetAllAsync() |
| | 9 | 21 | | { |
| | 9 | 22 | | var all = await _indexedDb.GetAllAsync<ActivityRecord>(Constants.Storage.ActivitiesStore); |
| | 25 | 23 | | return all?.OrderByDescending(a => a.CompletedAt).ToList() ?? new List<ActivityRecord>(); |
| | 9 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<List<ActivityRecord>> GetByDateRangeAsync(DateTime start, DateTime end) |
| | 12 | 27 | | { |
| | 12 | 28 | | var fromUtc = start.Kind != DateTimeKind.Utc ? DateTime.SpecifyKind(start, DateTimeKind.Local).ToUniversalTime() |
| | 12 | 29 | | var toUtc = end.Kind != DateTimeKind.Utc ? DateTime.SpecifyKind(end, DateTimeKind.Local).ToUniversalTime() : end |
| | 12 | 30 | | var fromDate = fromUtc.ToString(Constants.DateFormats.IsoFormat); |
| | 12 | 31 | | var toDate = toUtc.ToString(Constants.DateFormats.IsoFormat); |
| | | 32 | | |
| | 12 | 33 | | var activities = await _indexedDb.QueryByDateRangeAsync<ActivityRecord>( |
| | 12 | 34 | | Constants.Storage.ActivitiesStore, |
| | 12 | 35 | | Constants.Storage.CompletedAtIndex, |
| | 12 | 36 | | fromDate, |
| | 12 | 37 | | toDate); |
| | | 38 | | |
| | 49 | 39 | | return activities?.OrderByDescending(a => a.CompletedAt).ToList() ?? new List<ActivityRecord>(); |
| | 12 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<List<ActivityRecord>> GetPagedAsync(DateTime start, DateTime end, int skip, int take) |
| | 4 | 43 | | { |
| | 4 | 44 | | var all = await GetByDateRangeAsync(start, end); |
| | 4 | 45 | | return all.Skip(skip).Take(take).ToList(); |
| | 4 | 46 | | } |
| | | 47 | | |
| | | 48 | | public async Task<ActivityRecord?> GetByIdAsync(Guid id) |
| | 3 | 49 | | { |
| | 3 | 50 | | return await _indexedDb.GetAsync<ActivityRecord>(Constants.Storage.ActivitiesStore, id.ToString()); |
| | 3 | 51 | | } |
| | | 52 | | |
| | | 53 | | public async Task<bool> SaveAsync(ActivityRecord activity) |
| | 4 | 54 | | { |
| | 4 | 55 | | var success = await _indexedDb.PutAsync(Constants.Storage.ActivitiesStore, activity); |
| | 4 | 56 | | if (!success) |
| | 2 | 57 | | { |
| | 2 | 58 | | _logger.LogWarning(Constants.Messages.LogFailedToSaveActivity, activity.Id); |
| | 2 | 59 | | } |
| | 4 | 60 | | return success; |
| | 4 | 61 | | } |
| | | 62 | | |
| | | 63 | | public async Task<bool> DeleteAsync(Guid id) |
| | 3 | 64 | | { |
| | 3 | 65 | | return await _indexedDb.DeleteAsync(Constants.Storage.ActivitiesStore, id.ToString()); |
| | 3 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task<int> GetCountAsync(DateTime? start = null, DateTime? end = null) |
| | 7 | 69 | | { |
| | 7 | 70 | | if (start.HasValue && end.HasValue) |
| | 2 | 71 | | { |
| | 2 | 72 | | var activities = await GetByDateRangeAsync(start.Value, end.Value); |
| | 2 | 73 | | return activities.Count; |
| | | 74 | | } |
| | | 75 | | |
| | 5 | 76 | | var all = await GetAllAsync(); |
| | 5 | 77 | | return all.Count; |
| | 7 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task<bool> ClearAllAsync() |
| | 2 | 81 | | { |
| | 2 | 82 | | await _indexedDb.ClearAsync(Constants.Storage.ActivitiesStore); |
| | 2 | 83 | | return true; |
| | 2 | 84 | | } |
| | | 85 | | } |