using Microsoft.EntityFrameworkCore; using System.Linq; using System.Threading.Tasks; using UserManagement.Common.GenericRespository; using UserManagement.Common.UnitOfWork; using UserManagement.Data; using UserManagement.Data.Dto; using UserManagement.Data.Resources; using UserManagement.Domain; namespace UserManagement.Repository { public class ReminderRepository : GenericRepository, IReminderRepository { private readonly IPropertyMappingService _propertyMappingService; private readonly UserInfoToken _userInfo; public ReminderRepository( IUnitOfWork uow, IPropertyMappingService propertyMappingService, UserInfoToken userInfo ) : base(uow) { _propertyMappingService = propertyMappingService; _userInfo = userInfo; } public async Task GetReminders(ReminderResource reminderResource) { var collectionBeforePaging = All; collectionBeforePaging = collectionBeforePaging.ApplySort(reminderResource.OrderBy, _propertyMappingService.GetPropertyMapping()); if (!string.IsNullOrWhiteSpace(reminderResource.Subject)) { collectionBeforePaging = collectionBeforePaging .Where(c => EF.Functions.Like(c.Subject, $"%{reminderResource.Subject}%")); } if (!string.IsNullOrWhiteSpace(reminderResource.Message)) { collectionBeforePaging = collectionBeforePaging .Where(c => EF.Functions.Like(c.Message, $"%{reminderResource.Message}%")); } if (reminderResource.Frequency.HasValue) { collectionBeforePaging = collectionBeforePaging .Where(c => c.Frequency == reminderResource.Frequency); } var reminders = new ReminderList(); return await reminders.Create( collectionBeforePaging, reminderResource.Skip, reminderResource.PageSize ); } public async Task GetRemindersForLoginUser(ReminderResource reminderResource) { var collectionBeforePaging = All; collectionBeforePaging = collectionBeforePaging.ApplySort(reminderResource.OrderBy, _propertyMappingService.GetPropertyMapping()); //collectionBeforePaging = collectionBeforePaging // .Where(c => c.ReminderUsers.Any(d => d.UserId == _userInfo.Id)); if (!string.IsNullOrWhiteSpace(reminderResource.Subject)) { collectionBeforePaging = collectionBeforePaging .Where(c => EF.Functions.Like(c.Subject, $"%{reminderResource.Subject}%")); } if (!string.IsNullOrWhiteSpace(reminderResource.Message)) { collectionBeforePaging = collectionBeforePaging .Where(c => EF.Functions.Like(c.Message, $"%{reminderResource.Message}%")); } if (reminderResource.Frequency.HasValue) { collectionBeforePaging = collectionBeforePaging .Where(c => c.Frequency == reminderResource.Frequency); } var reminders = new ReminderList(); return await reminders.Create( collectionBeforePaging, reminderResource.Skip, reminderResource.PageSize ); } } }