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; namespace RMuseum.Services.Implementation { /// /// User Visits Tracking Service Implementation /// public class UserVisitsTrackingService : IUserVisitsTrackingService { /// /// add record /// /// /// /// public async Task> 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(false); var oldTracks = await _context.GanjoorUserPoemVisits.Where(v => v.PoemId == poemId && v.UserId == userId).ToArrayAsync(); _context.RemoveRange(oldTracks); await _context.SaveChangesAsync(); GanjoorUserPoemVisit visit = new GanjoorUserPoemVisit() { UserId = userId, PoemId = poemId, DateTime = DateTime.Now }; _context.Add(visit); await _context.SaveChangesAsync(); return new RServiceResult(true); } /// /// delete record /// /// /// /// public async Task> 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(false, "record not found!"); _context.Remove(rec); await _context.SaveChangesAsync(); return new RServiceResult(true); } /// /// start or stop tracking user /// /// /// /// public async Task> SwitchTrackingAsync(Guid userId, bool start) { var res = await _optionsService.SetAsync("KeepHistory", start.ToString(), userId, null); if (!string.IsNullOrEmpty(res.ExceptionString)) return new RServiceResult(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(false, exp.ToString()); } } return new RServiceResult(true); } /// /// get user history /// /// /// /// public async Task> GetUserHistoryAsync(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.Paginate(source, paging); List result = new List(); 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())); } /// /// options service /// protected readonly IRGenericOptionsService _optionsService; /// /// Database Context /// protected readonly RMuseumDbContext _context; /// /// constructor /// /// /// public UserVisitsTrackingService(RMuseumDbContext context, IRGenericOptionsService optionsService) { _context = context; _optionsService = optionsService; } } }