docs: #272 implementing delete and get operations + enabling/disabling the corresponding option

This commit is contained in:
Hamid Reza Mohammadi 2022-01-09 17:45:55 +03:30
parent e2f8180322
commit ede632acf3
3 changed files with 118 additions and 1 deletions

View File

@ -12695,6 +12695,30 @@
<param name="poemId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.UserVisitsTrackingService.DeleteAsync(System.Guid,System.Guid)">
<summary>
delete record
</summary>
<param name="userId"></param>
<param name="recordId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.UserVisitsTrackingService.SwitchTrackingAsync(System.Guid,System.Boolean)">
<summary>
start or stop tracking user
</summary>
<param name="userId"></param>
<param name="start"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.UserVisitsTrackingService.GetUserHistory(RSecurityBackend.Models.Generic.PagingParameterModel,System.Guid)">
<summary>
get user history
</summary>
<param name="paging"></param>
<param name="userId"></param>
<returns></returns>
</member>
<member name="F:RMuseum.Services.Implementation.UserVisitsTrackingService._optionsService">
<summary>
options service

View File

@ -170,7 +170,7 @@ namespace RMuseum.Services.Implementation
List<GanjoorUserBookmarkViewModel> result = new List<GanjoorUserBookmarkViewModel>();
foreach (var bookmark in bookmarksPage.Bookmarks)
{
var verses = await _context.GanjoorVerses.Where(v => v.PoemId == bookmark.PoemId && v.CoupletIndex == bookmark.CoupletIndex).OrderBy(v => v.VOrder).ToListAsync();
var verses = await _context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == bookmark.PoemId && v.CoupletIndex == bookmark.CoupletIndex).OrderBy(v => v.VOrder).ToListAsync();
result.Add
(
new GanjoorUserBookmarkViewModel()

View File

@ -1,9 +1,12 @@
using Microsoft.EntityFrameworkCore;
using RMuseum.DbContext;
using RMuseum.Models.Ganjoor;
using RMuseum.Models.Ganjoor.ViewModels;
using RSecurityBackend.Models.Generic;
using RSecurityBackend.Services;
using RSecurityBackend.Services.Implementation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -46,6 +49,96 @@ namespace RMuseum.Services.Implementation
return new RServiceResult<bool>(true);
}
/// <summary>
/// delete record
/// </summary>
/// <param name="userId"></param>
/// <param name="recordId"></param>
/// <returns></returns>
public async Task<RServiceResult<bool>> DeleteAsync(Guid userId, Guid recordId)
{
var rec = await _context.GanjoorUserPoemVisits.Where(v => v.UserId == userId && v.Id == recordId).SingleOrDefaultAsync();//userId is not needed but it is added to prevent accidental delete of other users data
if(rec == null)
return new RServiceResult<bool>(false, "record not found!");
_context.Remove(rec);
await _context.SaveChangesAsync();
return new RServiceResult<bool>(true);
}
/// <summary>
/// start or stop tracking user
/// </summary>
/// <param name="userId"></param>
/// <param name="start"></param>
/// <returns></returns>
public async Task<RServiceResult<bool>> SwitchTrackingAsync(Guid userId, bool start)
{
var res = await _optionsService.SetAsync("KeepHistory", start.ToString(), userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return new RServiceResult<bool>(false, res.ExceptionString);
if(!start)
{
try
{
var recs = await _context.GanjoorUserPoemVisits.Where(v => v.UserId == userId).ToArrayAsync();
_context.RemoveRange(recs);
await _context.SaveChangesAsync();
}
catch (Exception exp)
{
return new RServiceResult<bool>(false, exp.ToString());
}
}
return new RServiceResult<bool>(true);
}
/// <summary>
/// get user history
/// </summary>
/// <param name="paging"></param>
/// <param name="userId"></param>
/// <returns></returns>
public async Task<RServiceResult<(PaginationMetadata PagingMeta, GanjoorUserBookmarkViewModel[] HistoryItems)>> GetUserHistory(PagingParameterModel paging, Guid userId)
{
var source =
_context.GanjoorUserPoemVisits
.Include(b => b.Poem).ThenInclude(p => p.Cat).ThenInclude(c => c.Poet)
.Where(b => b.UserId == userId)
.OrderByDescending(b => b.DateTime)
.AsQueryable();
(PaginationMetadata PagingMeta, GanjoorUserPoemVisit[] HistoryItems) historiesPage =
await QueryablePaginator<GanjoorUserPoemVisit>.Paginate(source, paging);
List<GanjoorUserBookmarkViewModel> result = new List<GanjoorUserBookmarkViewModel>();
foreach (var historyItem in historiesPage.HistoryItems)
{
var verses = await _context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == historyItem.PoemId && v.CoupletIndex == 0).OrderBy(v => v.VOrder).ToListAsync();
result.Add
(
new GanjoorUserBookmarkViewModel()
{
Id = historyItem.Id,
PoetName = historyItem.Poem.Cat.Poet.Nickname,
PoetImageUrl = $"{WebServiceUrl.Url}{$"/api/ganjoor/poet/image/{historyItem.Poem.FullUrl.Substring(1, historyItem.Poem.FullUrl.IndexOf('/', 1) - 1)}.gif"}",
PoemFullTitle = historyItem.Poem.FullTitle,
PoemFullUrl = historyItem.Poem.FullUrl,
CoupletIndex = 0,
VerseText = verses.Count == 0 ? "" : verses[0].Text,
Verse2Text = verses.Count < 2 ? "" : verses[1].Text,
DateTime = historyItem.DateTime
}
);
}
return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorUserBookmarkViewModel[] HistoryItems)>
((historiesPage.PagingMeta, result.ToArray()));
}
/// <summary>
/// options service
/// </summary>