using MisIngredientesVue.Core.Models; using System.Threading.Tasks; using Dapper; using System; using System.Linq.Expressions; using System.Linq; namespace MisIngredientesVue.Core.Services { public interface IActivityLogsService { Task> GetActivityLogs(string tenantId, int skip, int take, string sortBy, string sortOrder, string q, string type, string entityType, string createdOn); Task Log(string tenantId, int userId, int entityId, string entityType, ActivityLogType type, string comment); } public class ActivityLogsService : IActivityLogsService { private readonly IRepository _repository; public ActivityLogsService(IRepository repository) { _repository = repository; } public Task> GetActivityLogs(string tenantId, int skip, int take, string sortBy, string sortOrder, string q, string type, string entityType, string createdOn) { if (string.IsNullOrEmpty(sortBy)) { sortBy = "createdOn"; sortOrder = "DESC"; } return _repository.Find(tenantId, GetFilterExpression(type, entityType, createdOn), skip, take, sortBy, sortOrder); } private Expression> GetFilterExpression(string type, string entityType, string createdOn) { var typeArray = type?.Split(',').Select(x => (ActivityLogType)Convert.ToInt32(x)).ToArray() ?? new ActivityLogType[] { }; var createdOnFrom = createdOn != null ? (createdOn.Contains(',') ? (DateTime?)DateTime.Parse(createdOn.Split(',')[0]) : (DateTime?)DateTime.Parse(createdOn)) : null; var createdOnTo = createdOn != null ? (createdOn.Contains(',') ? (DateTime?)DateTime.Parse(createdOn.Split(',')[1]) : (DateTime?)DateTime.Parse(createdOn)) : null; return x => x.EntityType.Contains(entityType) && typeArray.Contains(x.Type) && x.CreatedOn >= createdOnFrom && x.CreatedOn <= createdOnTo; } public async Task Log(string tenantId, int userId, int entityId, string entityType, ActivityLogType type, string comment) { //esto se hace a través de triggers //await _repository.Query(async (db) => //{ // await db.ExecuteAsync( // "INSERT INTO activitylogs (tenantId, createdOn, createdById, entityId, entitytype, comment, type) " + // "VALUES (@tenantId, @createdOn, @createdById, @entityId, @entityType, @createdBy, @comment, @type);", // new // { // tenantId, // createdOn = DateTime.UtcNow, // createdById = userId, // entityId, // entityType, // type = (int)type, // comment, // } // ); // return ""; //}); } } }