#493 GanjoorUserPoemVisit += Counter

Related Work Items: #4
This commit is contained in:
Hamid Reza Mohammadi 2026-06-26 19:01:23 +03:30
parent 93f6e7c335
commit 1fdcf9da29
3 changed files with 17 additions and 6 deletions

View File

@ -37,5 +37,10 @@ namespace RMuseum.Models.Ganjoor
/// date/time
/// </summary>
public DateTime DateTime { get; set; }
/// <summary>
/// counter of visits (1 for first and ...)
/// </summary>
public int Counter { get; set; }
}
}

View File

@ -16,7 +16,7 @@ namespace RMuseum.Services
/// <param name="userId"></param>
/// <param name="poemId"></param>
/// <returns></returns>
Task<RServiceResult<bool>> AddAsync(Guid userId, int poemId);
Task<RServiceResult<DateTime?>> AddAsync(Guid userId, int poemId);
/// <summary>
/// delete record

View File

@ -22,31 +22,37 @@ namespace RMuseum.Services.Implementation
/// </summary>
/// <param name="userId"></param>
/// <param name="poemId"></param>
/// <returns></returns>
public async Task<RServiceResult<bool>> AddAsync(Guid userId, int poemId)
/// <returns>previous visit date/time if any</returns>
public async Task<RServiceResult<DateTime?>> AddAsync(Guid userId, int poemId)
{
bool keepHistory = false;
var kRes = await _optionsService.GetValueAsync("KeepHistory", userId, null);
if (!string.IsNullOrEmpty(kRes.Result))
bool.TryParse(kRes.Result, out keepHistory);
if (!keepHistory)
return new RServiceResult<bool>(false);
return new RServiceResult<DateTime?>(null);
var oldTracks = await _context.GanjoorUserPoemVisits.Where(v => v.PoemId == poemId && v.UserId == userId).ToArrayAsync();
int count = oldTracks.Any() ? oldTracks.Last().Counter : 0;
DateTime? preVisit = oldTracks.Any() ? oldTracks.Last().DateTime : null;
_context.RemoveRange(oldTracks);
await _context.SaveChangesAsync();
count++;
GanjoorUserPoemVisit visit = new GanjoorUserPoemVisit()
{
UserId = userId,
PoemId = poemId,
DateTime = DateTime.Now
DateTime = DateTime.Now,
Counter = count,
};
_context.Add(visit);
await _context.SaveChangesAsync();
return new RServiceResult<bool>(true);
return new RServiceResult<DateTime?>(preVisit);
}
/// <summary>