|
| 1 | +using Taskdeck.Application.DTOs; |
| 2 | +using Taskdeck.Application.Interfaces; |
| 3 | +using Taskdeck.Domain.Common; |
| 4 | +using Taskdeck.Domain.Entities; |
| 5 | +using Taskdeck.Domain.Exceptions; |
| 6 | + |
| 7 | +namespace Taskdeck.Application.Services; |
| 8 | + |
| 9 | +public class TomorrowNoteService : ITomorrowNoteService |
| 10 | +{ |
| 11 | + private readonly ITomorrowNoteRepository _repository; |
| 12 | + private readonly IUnitOfWork _unitOfWork; |
| 13 | + |
| 14 | + public TomorrowNoteService(ITomorrowNoteRepository repository, IUnitOfWork unitOfWork) |
| 15 | + { |
| 16 | + _repository = repository; |
| 17 | + _unitOfWork = unitOfWork; |
| 18 | + } |
| 19 | + |
| 20 | + public async Task<Result<TomorrowNoteResponse?>> GetNoteAsync( |
| 21 | + Guid userId, |
| 22 | + DateOnly date, |
| 23 | + CancellationToken cancellationToken = default) |
| 24 | + { |
| 25 | + if (userId == Guid.Empty) |
| 26 | + return Result.Failure<TomorrowNoteResponse?>(ErrorCodes.ValidationError, "User ID is required"); |
| 27 | + |
| 28 | + var note = await _repository.GetByUserAndDateAsync(userId, date, cancellationToken); |
| 29 | + if (note is null) |
| 30 | + return Result.Success<TomorrowNoteResponse?>(null); |
| 31 | + |
| 32 | + return Result.Success<TomorrowNoteResponse?>(MapToResponse(note)); |
| 33 | + } |
| 34 | + |
| 35 | + public async Task<Result<TomorrowNoteResponse>> SaveNoteAsync( |
| 36 | + Guid userId, |
| 37 | + DateOnly date, |
| 38 | + string text, |
| 39 | + CancellationToken cancellationToken = default) |
| 40 | + { |
| 41 | + if (userId == Guid.Empty) |
| 42 | + return Result.Failure<TomorrowNoteResponse>(ErrorCodes.ValidationError, "User ID is required"); |
| 43 | + |
| 44 | + if (date == default) |
| 45 | + return Result.Failure<TomorrowNoteResponse>(ErrorCodes.ValidationError, "Date is required"); |
| 46 | + |
| 47 | + if (text is null) |
| 48 | + return Result.Failure<TomorrowNoteResponse>(ErrorCodes.ValidationError, "Text cannot be null"); |
| 49 | + |
| 50 | + if (text.Length > TomorrowNote.MaxTextLength) |
| 51 | + return Result.Failure<TomorrowNoteResponse>( |
| 52 | + ErrorCodes.ValidationError, |
| 53 | + $"Text cannot exceed {TomorrowNote.MaxTextLength} characters"); |
| 54 | + |
| 55 | + var existing = await _repository.GetByUserAndDateAsync(userId, date, cancellationToken); |
| 56 | + if (existing is not null) |
| 57 | + { |
| 58 | + existing.UpdateText(text); |
| 59 | + await _repository.UpdateAsync(existing, cancellationToken); |
| 60 | + await _unitOfWork.SaveChangesAsync(cancellationToken); |
| 61 | + return Result.Success(MapToResponse(existing)); |
| 62 | + } |
| 63 | + |
| 64 | + var note = new TomorrowNote(userId, date, text); |
| 65 | + await _repository.AddAsync(note, cancellationToken); |
| 66 | + await _unitOfWork.SaveChangesAsync(cancellationToken); |
| 67 | + |
| 68 | + // Race-condition recovery: if a concurrent request created a note for the |
| 69 | + // same (userId, date) between our read and write, the UnitOfWork's conflict |
| 70 | + // resolver detaches our entity and retries SaveChanges (succeeding with no-op). |
| 71 | + // The local 'note' is now phantom data -- never persisted. Re-fetch the winner |
| 72 | + // and apply last-writer-wins so the caller's text is not silently lost. |
| 73 | + var persisted = await _repository.GetByUserAndDateAsync(userId, date, cancellationToken); |
| 74 | + if (persisted is not null && persisted.Id != note.Id) |
| 75 | + { |
| 76 | + persisted.UpdateText(text); |
| 77 | + await _repository.UpdateAsync(persisted, cancellationToken); |
| 78 | + await _unitOfWork.SaveChangesAsync(cancellationToken); |
| 79 | + return Result.Success(MapToResponse(persisted)); |
| 80 | + } |
| 81 | + |
| 82 | + return Result.Success(MapToResponse(note)); |
| 83 | + } |
| 84 | + |
| 85 | + private static TomorrowNoteResponse MapToResponse(TomorrowNote note) |
| 86 | + { |
| 87 | + return new TomorrowNoteResponse( |
| 88 | + note.Id, |
| 89 | + note.Date, |
| 90 | + note.Text, |
| 91 | + note.UpdatedAt, |
| 92 | + note.CreatedAt); |
| 93 | + } |
| 94 | +} |
0 commit comments