using Microsoft.EntityFrameworkCore;
using RMuseum.DbContext;
using RMuseum.Models.Ganjoor;
using RSecurityBackend.Models.Generic;
using RSecurityBackend.Services;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace RMuseum.Services.Implementation
{
///
/// User Visits Tracking Service
///
public class UserVisitsTrackingService
{
///
/// add record
///
///
///
///
public async Task> AddAsync(Guid userId, int poemId)
{
bool keepHistory = false;
var kRes = await _optionsService.GetValueAsync("KeepHistory", userId);
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);
}
///
/// options service
///
protected readonly IRGenericOptionsService _optionsService;
///
/// Database Context
///
protected readonly RMuseumDbContext _context;
///
/// constructor
///
///
///
public UserVisitsTrackingService(RMuseumDbContext context, IRGenericOptionsService optionsService)
{
_context = context;
_optionsService = optionsService;
}
}
}