| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using Pomodoro.Web.Models; |
| | | 3 | | |
| | | 4 | | namespace Pomodoro.Web.Services.Repositories; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Repository implementation for task persistence using IndexedDB |
| | | 8 | | /// </summary> |
| | | 9 | | public class TaskRepository : ITaskRepository |
| | | 10 | | { |
| | | 11 | | private readonly IIndexedDbService _indexedDb; |
| | | 12 | | private readonly ILogger<TaskRepository> _logger; |
| | | 13 | | |
| | 22 | 14 | | public TaskRepository(IIndexedDbService indexedDb, ILogger<TaskRepository> logger) |
| | 22 | 15 | | { |
| | 22 | 16 | | _indexedDb = indexedDb; |
| | 22 | 17 | | _logger = logger; |
| | 22 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<List<TaskItem>> GetAllAsync() |
| | 6 | 21 | | { |
| | 6 | 22 | | var all = await _indexedDb.GetAllAsync<TaskItem>(Constants.Storage.TasksStore); |
| | 12 | 23 | | return all?.Where(t => !t.IsDeleted).ToList() ?? new List<TaskItem>(); |
| | 6 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<List<TaskItem>> GetAllIncludingDeletedAsync() |
| | 3 | 27 | | { |
| | 3 | 28 | | var all = await _indexedDb.GetAllAsync<TaskItem>(Constants.Storage.TasksStore); |
| | 3 | 29 | | return all ?? new List<TaskItem>(); |
| | 3 | 30 | | } |
| | | 31 | | |
| | | 32 | | public async Task<TaskItem?> GetByIdAsync(Guid id) |
| | 6 | 33 | | { |
| | 6 | 34 | | return await _indexedDb.GetAsync<TaskItem>(Constants.Storage.TasksStore, id.ToString()); |
| | 6 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<bool> SaveAsync(TaskItem task) |
| | 5 | 38 | | { |
| | 5 | 39 | | var success = await _indexedDb.PutAsync(Constants.Storage.TasksStore, task); |
| | 5 | 40 | | if (!success) |
| | 2 | 41 | | { |
| | 2 | 42 | | _logger.LogWarning(Constants.Messages.LogFailedToSaveTask, task.Id); |
| | 2 | 43 | | } |
| | 5 | 44 | | return success; |
| | 5 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<bool> SoftDeleteAsync(Guid id) |
| | 3 | 48 | | { |
| | 3 | 49 | | var task = await GetByIdAsync(id); |
| | 4 | 50 | | if (task == null) return false; |
| | | 51 | | |
| | 2 | 52 | | task.IsDeleted = true; |
| | 2 | 53 | | task.DeletedAt = DateTime.UtcNow; |
| | 2 | 54 | | return await SaveAsync(task); |
| | 3 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<bool> HardDeleteAsync(Guid id) |
| | 3 | 58 | | { |
| | 3 | 59 | | return await _indexedDb.DeleteAsync(Constants.Storage.TasksStore, id.ToString()); |
| | 3 | 60 | | } |
| | | 61 | | |
| | | 62 | | public async Task<int> GetCountAsync() |
| | 2 | 63 | | { |
| | 2 | 64 | | var all = await GetAllAsync(); |
| | 2 | 65 | | return all.Count; |
| | 2 | 66 | | } |
| | | 67 | | |
| | | 68 | | public async Task ClearAllAsync() |
| | 1 | 69 | | { |
| | 1 | 70 | | await _indexedDb.ClearAsync(Constants.Storage.TasksStore); |
| | 1 | 71 | | } |
| | | 72 | | } |