From 7f16552dadddc348837bff494a0333a8ace919ca Mon Sep 17 00:00:00 2001 From: HueByte Date: Sun, 9 Oct 2022 02:09:29 +0200 Subject: [PATCH] T #98 added paginator reminder implementation --- .../Services/Reminder/ReminderService.cs | 3 -- .../Services/ReminderService.cs | 39 ++++++++++++------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs b/src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs index 186c663..6bdfacc 100644 --- a/src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs +++ b/src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs @@ -2,14 +2,11 @@ using Discord.Interactions; using Discord.WebSocket; using Huppy.Core.Entities; -using Huppy.Core.Interfaces.IRepositories; using Huppy.Core.Interfaces.IServices; using Huppy.Core.Utilities; using Huppy.Kernel.Constants; using HuppyService.Service.Protos; using HuppyService.Service.Protos.Models; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Internal; using Microsoft.Extensions.Logging; namespace Huppy.Core.Services.Reminder; diff --git a/src/HuppyService/HuppyService.Service/Services/ReminderService.cs b/src/HuppyService/HuppyService.Service/Services/ReminderService.cs index ffa5517..39cef28 100644 --- a/src/HuppyService/HuppyService.Service/Services/ReminderService.cs +++ b/src/HuppyService/HuppyService.Service/Services/ReminderService.cs @@ -2,6 +2,7 @@ using HuppyService.Core.Interfaces.IRepositories; using HuppyService.Core.Models; using HuppyService.Core.Utilities; +using HuppyService.Infrastructure.Repositories; using HuppyService.Service.Protos; using HuppyService.Service.Protos.Models; using Microsoft.EntityFrameworkCore; @@ -18,6 +19,28 @@ public ReminderService(IReminderRepository reminderRepository, ILogger GetSortedUserReminders(SortedUserRemindersInput request, ServerCallContext context) + { + var query = await _reminderRepository.GetAllAsync(); + var reminders = await query + .Where(reminder => reminder.UserId == request.UserId) + .OrderBy(e => e.RemindDate) + .Skip(request.Skip) + .Take(request.Take) + .ToListAsync(); + + ReminderModelCollection? result = new(); + result.ReminderModels.AddRange(reminders.Select(reminder => new ReminderModel + { + Id = reminder.Id, + Message = reminder.Message, + UserId = reminder.UserId, + UnixTime = Miscellaneous.DateTimeToUnixTimeStamp(reminder.RemindDate) + }).ToList()); + + return result; + } + public override async Task GetRemindersCount(GetReminderCountInput request, ServerCallContext context) { var query = await _reminderRepository.GetAllAsync(); @@ -55,22 +78,8 @@ public override async Task AddReminder(ReminderModel request, Ser if (!result) throw new Exception("Failed to create reminder"); - //ReminderInput reminderInput = new() { User = user, Message = reminder.Message }; - _logger.LogInformation("Added reminder for [{user}] at [{date}] UTC", request.UserId, reminder.RemindDate); - //if (date < FetchingDate) - //{ - // await _eventService.AddEvent(date, reminder.Id.ToString()!, reminderInput, async (input) => - // { - // if (input is ReminderInput data) - // { - // data.Message ??= ""; - // await StandardReminder(data.User, data.Message); - // } - // }); - //} - return new ReminderModel() { Id = reminder.Id, Message = reminder.Message, UnixTime = request.UnixTime, UserId = reminder.UserId }; } @@ -88,7 +97,7 @@ public override async Task AddReminder(ReminderModel request, Ser .Where(reminder => remindDateStart < remindDateEnd) .ToListAsync(); - if (!reminders.Any()) return null; + if (!reminders.Any()) return new(); ReminderModelCollection? result = new(); result.ReminderModels.AddRange(reminders.Select(reminder => new ReminderModel