diff --git a/src/Huppy/Huppy.App/Commands/SlashCommands/DevCommands.cs b/src/Huppy/Huppy.App/Commands/SlashCommands/DevCommands.cs index a598d81..78631c5 100644 --- a/src/Huppy/Huppy.App/Commands/SlashCommands/DevCommands.cs +++ b/src/Huppy/Huppy.App/Commands/SlashCommands/DevCommands.cs @@ -9,6 +9,7 @@ using Huppy.Core.Services.HuppyCacheStorage; using Huppy.Core.Services.JobManager; using Huppy.Core.Services.Paginator.Entities; +using Huppy.Core.Utilities; using Huppy.Kernel.Constants; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -112,14 +113,14 @@ await ModifyOriginalResponseAsync((msg) => foreach (var ticket in tickets) { - ticket.CreatedDate = DateTime.SpecifyKind(ticket.CreatedDate, DateTimeKind.Utc); - + // ticket.CreatedDate = DateTime.SpecifyKind(ticket.CreatedDate, DateTimeKind.Utc); + var userName = _client.GetUser(ticket.UserId); StringBuilder sb = new(); sb.AppendLine($"> Ticked ID:`{ticket.Id}`"); - sb.AppendLine($"> Created date: {TimestampTag.FromDateTime(ticket.CreatedDate)}"); + sb.AppendLine($"> Created date: {TimestampTag.FromDateTime(Miscellaneous.UnixTimeStampToUtcDateTime(ticket.CreatedDate))}"); sb.AppendLine($"> Status: {(ticket.IsClosed ? "`Closed`" : "`Open`")}"); sb.AppendLine($"> Topic: `{ticket.Topic}`"); - sb.AppendLine($"> Owner: `{ticket.User.Username} ({ticket.UserId})`"); + sb.AppendLine($"> Owner: `{userName} ({ticket.UserId})`"); embed.AddField("📜 Ticket", sb.ToString()); } diff --git a/src/Huppy/Huppy.App/Commands/SlashCommands/ReminderCommands.cs b/src/Huppy/Huppy.App/Commands/SlashCommands/ReminderCommands.cs index 858807e..820a31d 100644 --- a/src/Huppy/Huppy.App/Commands/SlashCommands/ReminderCommands.cs +++ b/src/Huppy/Huppy.App/Commands/SlashCommands/ReminderCommands.cs @@ -97,7 +97,7 @@ await ModifyOriginalResponseAsync((msg) => embed.WithTitle("Success") .WithDescription($"Reminder with `{reminderId}` ID got removed") - .AddField("Date", TimestampTag.FromDateTime(DateTime.SpecifyKind(Miscellaneous.UnixTimeStampToUtcDateTime(reminder.UnixTime), DateTimeKind.Utc))) + .AddField("Date", TimestampTag.FromDateTime(DateTime.SpecifyKind(Miscellaneous.UnixTimeStampToUtcDateTime(reminder.RemindDate), DateTimeKind.Utc))) .AddField("Message", $"```vb\n{reminder.Message}\n```"); await ModifyOriginalResponseAsync((msg) => @@ -168,7 +168,7 @@ public async Task GetUserReminders() foreach (var reminder in reminders) { - TimestampTag timestamp = TimestampTag.FromDateTime(DateTime.SpecifyKind(Miscellaneous.UnixTimeStampToUtcDateTime(reminder.UnixTime), DateTimeKind.Utc)); + TimestampTag timestamp = TimestampTag.FromDateTime(DateTime.SpecifyKind(Miscellaneous.UnixTimeStampToUtcDateTime(reminder.RemindDate), DateTimeKind.Utc)); embed.AddField( $"✨ Reminder at {timestamp} | ID: {reminder.Id}", $"```vb\n{reminder.Message}```", diff --git a/src/Huppy/Huppy.App/Commands/SlashCommands/TicketCommands.cs b/src/Huppy/Huppy.App/Commands/SlashCommands/TicketCommands.cs index 229add9..d34f39e 100644 --- a/src/Huppy/Huppy.App/Commands/SlashCommands/TicketCommands.cs +++ b/src/Huppy/Huppy.App/Commands/SlashCommands/TicketCommands.cs @@ -4,6 +4,7 @@ using Huppy.Core.Attributes; using Huppy.Core.Interfaces.IServices; using Huppy.Core.Services.Paginator.Entities; +using Huppy.Core.Utilities; using Huppy.Kernel.Constants; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -51,7 +52,7 @@ public async Task CreateTicketAsync(string topic, string description) .WithThumbnailUrl(Icons.Huppy1) .WithTitle("Ticket created!") .AddField("Ticket ID", $"`{ticket.Id}`", true) - .AddField("Creation date", TimestampTag.FromDateTime(ticket.CreatedDate), true) + .AddField("Creation date", TimestampTag.FromDateTime(Miscellaneous.UnixTimeStampToUtcDateTime(ticket.CreatedDate)), true) .AddField("Status", ticket.IsClosed ? "`Closed`" : "`Open`", true) .AddField("Topic", ticket.Topic) .AddField("Ticket description", ticket.Description) @@ -92,13 +93,15 @@ public async Task GetStatusAsync(string ticketId) } else { - ticket.CreatedDate = DateTime.SpecifyKind(ticket.CreatedDate, DateTimeKind.Utc); + // ticket.CreatedDate = DateTime.SpecifyKind(ticket.CreatedDate, DateTimeKind.Utc); + DateTime? date = Miscellaneous.UnixTimeStampToUtcDateTime(ticket.ClosedDate); + string closedDate = date is not null ? TimestampTag.FromDateTime(Miscellaneous.UnixTimeStampToUtcDateTime(ticket.CreatedDate)).ToString() : "Ticket still open"; embed = new EmbedBuilder().WithColor(Color.Magenta) .WithThumbnailUrl(Icons.Huppy1) .WithTitle("Ticket details") - .AddField("Creation date", TimestampTag.FromDateTime(ticket.CreatedDate), true) - .AddField("Closed date", ticket.ClosedDate is not null ? TimestampTag.FromDateTime(ticket.CreatedDate) : "Ticket still open", true) + .AddField("Creation date", TimestampTag.FromDateTime(Miscellaneous.UnixTimeStampToUtcDateTime(ticket.CreatedDate)), true) + .AddField("Closed date", closedDate, true) .AddField("Status", ticket.IsClosed ? "`Closed`" : "`Open`", true) .AddField("Topic", ticket.Topic) .AddField("Ticket description", ticket.Description) @@ -148,11 +151,11 @@ await ModifyOriginalResponseAsync((msg) => foreach (var ticket in tickets) { - ticket.CreatedDate = DateTime.SpecifyKind(ticket.CreatedDate, DateTimeKind.Utc); + // ticket.CreatedDate = DateTime.SpecifyKind(ticket.CreatedDate, DateTimeKind.Utc); StringBuilder sb = new(); sb.AppendLine($"> Ticked ID:`{ticket.Id}`"); - sb.AppendLine($"> Created date: {TimestampTag.FromDateTime(ticket.CreatedDate)}"); + sb.AppendLine($"> Created date: {TimestampTag.FromDateTime(Miscellaneous.UnixTimeStampToUtcDateTime(ticket.CreatedDate))}"); sb.AppendLine($"> Status: {(ticket.IsClosed ? "`Closed`" : "`Open`")}"); sb.AppendLine($"> Topic: `{ticket.Topic}`"); diff --git a/src/Huppy/Huppy.App/Configuration/ModuleConfigurator.cs b/src/Huppy/Huppy.App/Configuration/ModuleConfigurator.cs index 9b4c65d..c307dca 100644 --- a/src/Huppy/Huppy.App/Configuration/ModuleConfigurator.cs +++ b/src/Huppy/Huppy.App/Configuration/ModuleConfigurator.cs @@ -81,6 +81,11 @@ public ModuleConfigurator AddGRPCServices() options.Address = huppyCoreUrl; }); + _services.AddGrpcClient((services, options) => + { + options.Address = huppyCoreUrl; + }); + return this; } diff --git a/src/Huppy/Huppy.Core/Interfaces/IServices/ITicketService.cs b/src/Huppy/Huppy.Core/Interfaces/IServices/ITicketService.cs index 6465ac8..db38835 100644 --- a/src/Huppy/Huppy.Core/Interfaces/IServices/ITicketService.cs +++ b/src/Huppy/Huppy.Core/Interfaces/IServices/ITicketService.cs @@ -1,17 +1,18 @@ using Discord; using Huppy.Core.Models; +using HuppyService.Service.Protos.Models; namespace Huppy.Core.Interfaces.IServices { public interface ITicketService { Task GetCountAsync(ulong userId); - Task> GetTicketsAsync(); - Task> GetTicketsAsync(ulong userId); - Task> GetPaginatedTickets(int skip, int take); - Task> GetPaginatedTickets(ulong userId, int skip, int take); - Task GetTicketAsync(string ticketId, ulong userId); - Task AddTicketAsync(IUser user, string topic, string description); + Task> GetTicketsAsync(); + Task> GetTicketsAsync(ulong userId); + Task> GetPaginatedTickets(int skip, int take); + Task> GetPaginatedTickets(ulong userId, int skip, int take); + Task GetTicketAsync(string ticketId, ulong userId); + Task AddTicketAsync(IUser user, string topic, string description); Task RemoveTicketAsync(string ticketId); Task UpdateTicketAsync(string ticketId, string description); Task CloseTicket(string ticketId, string answer); diff --git a/src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs b/src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs index 583d431..ea186dd 100644 --- a/src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs +++ b/src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs @@ -69,7 +69,7 @@ public async Task RegisterFreshRemindersAsync() // add reminder ReminderInput reminderInput = new() { User = user, Message = reminder.Message }; - await _eventService.AddEvent(Miscellaneous.UnixTimeStampToUtcDateTime(reminder.UnixTime), reminder.Id.ToString(), reminderInput, async (input) => + await _eventService.AddEvent(Miscellaneous.UnixTimeStampToUtcDateTime(reminder.RemindDate), reminder.Id.ToString(), reminderInput, async (input) => { if (input is ReminderInput data) { @@ -99,7 +99,7 @@ public async Task AddReminderAsync(DateTime date, IUser user, string message) ReminderModel reminder = new() { Message = message, - UnixTime = Miscellaneous.DateTimeToUnixTimestamp(date), + RemindDate = Miscellaneous.DateTimeToUnixTimestamp(date), UserId = user.Id }; @@ -131,7 +131,7 @@ public async Task RemoveReminderAsync(ReminderModel reminder) if (result.IsSuccess) throw new Exception($"Failed to remove reminder {reminder.Id}"); - await _eventService.Remove(Miscellaneous.UnixTimeStampToUtcDateTime(reminder.UnixTime), reminder.Id.ToString()); + await _eventService.Remove(Miscellaneous.UnixTimeStampToUtcDateTime(reminder.RemindDate), reminder.Id.ToString()); } public async Task RemoveReminderRangeAsync(string[] ids) diff --git a/src/Huppy/Huppy.Core/Services/Ticket/TicketService.cs b/src/Huppy/Huppy.Core/Services/Ticket/TicketService.cs index cd9d2ec..505df02 100644 --- a/src/Huppy/Huppy.Core/Services/Ticket/TicketService.cs +++ b/src/Huppy/Huppy.Core/Services/Ticket/TicketService.cs @@ -1,7 +1,11 @@ +using System.Reflection.Metadata.Ecma335; using Discord; using Huppy.Core.Interfaces.IRepositories; using Huppy.Core.Interfaces.IServices; using Huppy.Core.Models; +using HuppyService.Service.Protos; +using HuppyService.Service.Protos.Models; +using Microsoft.AspNetCore.Authentication; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Internal; @@ -9,93 +13,89 @@ namespace Huppy.Core.Services.Ticket; public class TicketService : ITicketService { - private readonly ITicketRepository _ticketRepository; - public TicketService(ITicketRepository ticketRepository) + // private readonly ITicketRepository _ticketRepository; + private readonly TicketProto.TicketProtoClient _ticketClient; + public TicketService(TicketProto.TicketProtoClient ticketClient, ITicketRepository ticketRepository) { - _ticketRepository = ticketRepository; + // _ticketRepository = ticketRepository; + _ticketClient = ticketClient; } public async Task GetCountAsync(ulong userId) { - // EFC lazy loading, DB is not called here - var tickets = await _ticketRepository.GetAllAsync(); + var result = await _ticketClient.GetCountAsync(new() { Id = userId }); - // modify database query - return await tickets.Where(ticket => ticket.UserId == userId).CountAsync(); + return result.Number; } - public async Task> GetTicketsAsync() + public async Task> GetTicketsAsync() { - return await _ticketRepository.GetAllAsync(); + var result = await _ticketClient.GetTicketsAsync(new()); + return result.TicketsModels; } - public async Task> GetTicketsAsync(ulong userId) + public async Task> GetTicketsAsync(ulong userId) { - var tickets = await _ticketRepository.GetAllAsync(); - return tickets.Where(ticket => ticket.UserId == userId); + var tickets = await _ticketClient.GetUserTicketsAsync(new() { Id = userId }); + return tickets.TicketsModels; } - public async Task> GetPaginatedTickets(int skip, int take) + public async Task> GetPaginatedTickets(int skip, int take) { - var tickets = (await _ticketRepository.GetAllAsync()) - .Include(ticket => ticket.User) - .OrderBy(ticket => ticket.IsClosed) - .ThenByDescending(ticket => ticket.CreatedDate) - .Skip(skip) - .Take(take) - .ToList(); - - return tickets; + var tickets = await _ticketClient.GetPaginatedTicketsAsync(new() { Skip = skip, Take = take }); + + return tickets.TicketsModels; } - public async Task> GetPaginatedTickets(ulong userId, int skip, int take) + public async Task> GetPaginatedTickets(ulong userId, int skip, int take) { - var tickets = (await _ticketRepository.GetAllAsync()) - .Where(ticket => ticket.UserId == userId) - .OrderBy(ticket => ticket.IsClosed) - .ThenByDescending(ticket => ticket.CreatedDate) - .Skip(skip) - .Take(take) - .ToList(); - - return tickets; + var tickets = await _ticketClient.GetUserPaginatedTicketsAsync(new() { Skip = skip, Take = take, UserId = userId }); + + return tickets.TicketsModels; } - public async Task GetTicketAsync(string ticketId, ulong userId) + public async Task GetTicketAsync(string ticketId, ulong userId) { - var tickets = await _ticketRepository.GetAllAsync(); + var ticket = await _ticketClient.GetTicketAsync(new() { TicketId = ticketId, UserId = userId }); + // var tickets = await _ticketRepository.GetAllAsync(); + return ticket; - return await tickets.FirstOrDefaultAsync(ticket => ticket.Id == ticketId && ticket.UserId == userId); + // return await tickets.FirstOrDefaultAsync(ticket => ticket.Id == ticketId && ticket.UserId == userId); } - public async Task AddTicketAsync(IUser user, string topic, string description) + public async Task AddTicketAsync(IUser user, string topic, string description) { if (string.IsNullOrEmpty(description)) throw new ArgumentException("Ticket description cannot be null"); - Models.Ticket ticket = new() - { - Id = Guid.NewGuid().ToString(), - Topic = topic, - Description = description, - CreatedDate = DateTime.UtcNow, - TicketAnswer = null, - ClosedDate = null, - IsClosed = false, - UserId = user.Id, - }; - - await _ticketRepository.AddAsync(ticket); - await _ticketRepository.SaveChangesAsync(); - - return ticket; + var result = await _ticketClient.AddTicketAsync(new() { UserId = user.Id, Description = description, Topic = topic }); + return result; + + // Models.Ticket ticket = new() + // { + // Id = Guid.NewGuid().ToString(), + // Topic = topic, + // Description = description, + // CreatedDate = DateTime.UtcNow, + // TicketAnswer = null, + // ClosedDate = null, + // IsClosed = false, + // UserId = user.Id, + // }; + + // await _ticketRepository.AddAsync(ticket); + // await _ticketRepository.SaveChangesAsync(); + + // return ticket; } public async Task RemoveTicketAsync(string ticketId) { if (string.IsNullOrEmpty(ticketId)) throw new ArgumentException("Ticket cannot be null or empty"); - await _ticketRepository.RemoveAsync(ticketId); - await _ticketRepository.SaveChangesAsync(); + var result = await _ticketClient.RemoveTicketAsync(new() { Id = ticketId }); + + // await _ticketRepository.RemoveAsync(ticketId); + // await _ticketRepository.SaveChangesAsync(); } public async Task UpdateTicketAsync(string ticketId, string description) @@ -103,14 +103,17 @@ public async Task UpdateTicketAsync(string ticketId, string description) if (string.IsNullOrEmpty(ticketId) || string.IsNullOrEmpty(description)) throw new ArgumentException("Both ticked ID and ticket description cannot be null or empty"); - var ticket = await _ticketRepository.GetAsync(ticketId); + var result = await _ticketClient.UpdateTicketAsync(new() { TicketId = ticketId, Description = description }); + - if (ticket is null) throw new Exception("Ticket doesn't exist"); + // var ticket = await _ticketRepository.GetAsync(ticketId); - ticket.Description = description; + // if (ticket is null) throw new Exception("Ticket doesn't exist"); - await _ticketRepository.UpdateAsync(ticket); - await _ticketRepository.SaveChangesAsync(); + // ticket.Description = description; + + // await _ticketRepository.UpdateAsync(ticket); + // await _ticketRepository.SaveChangesAsync(); } public async Task CloseTicket(string ticketId, string answer) @@ -118,15 +121,17 @@ public async Task CloseTicket(string ticketId, string answer) if (string.IsNullOrEmpty(ticketId)) throw new ArgumentException("Ticked ID cannot be empty"); - var ticket = await _ticketRepository.GetAsync(ticketId); + _ = await _ticketClient.CloseTicketAsync(new() { TicketId = ticketId, Answer = answer }); + + // var ticket = await _ticketRepository.GetAsync(ticketId); - if (ticket is null) throw new Exception("Ticket doesn't exist"); + // if (ticket is null) throw new Exception("Ticket doesn't exist"); - ticket.IsClosed = true; - ticket.TicketAnswer = answer; - ticket.ClosedDate = DateTime.UtcNow; + // ticket.IsClosed = true; + // ticket.TicketAnswer = answer; + // ticket.ClosedDate = DateTime.UtcNow; - await _ticketRepository.UpdateAsync(ticket); - await _ticketRepository.SaveChangesAsync(); + // await _ticketRepository.UpdateAsync(ticket); + // await _ticketRepository.SaveChangesAsync(); } } diff --git a/src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs b/src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs index d08cc68..4ae5b1d 100644 --- a/src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs +++ b/src/HuppyService/HuppyService.Core/Utilities/ReflectionMapper.cs @@ -1,4 +1,5 @@ using HuppyService.Core.Abstraction; +using Microsoft.EntityFrameworkCore.Metadata.Conventions; using System; using System.Collections.Generic; using System.Linq; @@ -31,6 +32,31 @@ public static class ReflectionMapper return result; } + public static ICollection? Map(object[] input) where T :class, new() + { + var result = typeof(List<>).MakeGenericType(typeof(T)); + + var tProps = result.GetType().GetProperties(); + var inputProps = input.GetType().GetProperties().ToDictionary(e => e.Name, e => e); + + foreach (var inputObject in input) + { + T objectToAdd = new(); + foreach (var prop in tProps) + { + inputProps.TryGetValue(prop.Name, out PropertyInfo? matchingProp); + if (matchingProp is null) continue; + var inputPropInstance = matchingProp.GetValue(inputObject, null); + + prop.SetValue(objectToAdd, GetMappedValue(prop.PropertyType, inputPropInstance)); + } + + ((ICollection)result).Add(objectToAdd); + } + + return result as ICollection; + } + public static object? GetMappedValue(Type newValue, object? inputValue) { if (inputValue is null) return default; diff --git a/src/HuppyService/HuppyService.Service/Configuration/AppConfigurator.cs b/src/HuppyService/HuppyService.Service/Configuration/AppConfigurator.cs index 60d9f7e..2e8d65c 100644 --- a/src/HuppyService/HuppyService.Service/Configuration/AppConfigurator.cs +++ b/src/HuppyService/HuppyService.Service/Configuration/AppConfigurator.cs @@ -40,6 +40,7 @@ public void MapGrpcServices() _app.MapGrpcService(); _app.MapGrpcService(); _app.MapGrpcService(); + _app.MapGrpcService(); } } } diff --git a/src/HuppyService/HuppyService.Service/Protos/database.proto b/src/HuppyService/HuppyService.Service/Protos/database.proto index 5798563..adb4b96 100644 --- a/src/HuppyService/HuppyService.Service/Protos/database.proto +++ b/src/HuppyService/HuppyService.Service/Protos/database.proto @@ -35,7 +35,7 @@ message ServerRoomsModel { message ReminderModel { int32 Id = 1; - uint64 UnixTime = 2; + uint64 RemindDate = 2; string Message = 3; uint64 UserId = 4; } diff --git a/src/HuppyService/HuppyService.Service/Protos/ticket.proto b/src/HuppyService/HuppyService.Service/Protos/ticket.proto index b8a6c72..774a3d1 100644 --- a/src/HuppyService/HuppyService.Service/Protos/ticket.proto +++ b/src/HuppyService/HuppyService.Service/Protos/ticket.proto @@ -8,15 +8,15 @@ import "Protos/shared.proto"; package TicketProto; service TicketProto { - rpc GetCountAsync(shared.UserId) returns (shared.Int32); + rpc GetCount(shared.UserId) returns (shared.Int32); rpc GetTickets(shared.Void) returns (TicketModelCollection); rpc GetUserTickets(shared.UserId) returns (TicketModelCollection); rpc GetPaginatedTickets(GetPaginatedTicketsInput) returns (TicketModelCollection); rpc GetUserPaginatedTickets(GetUserPaginatedTicketsInput) returns (TicketModelCollection); rpc GetTicket(GetTicketInput) returns (database.models.TicketModel); rpc AddTicket(AddTicketInput) returns (database.models.TicketModel); - rpc RemoveTicketAsync(shared.StringId) returns (shared.CommonResponse); - rpc UpdateTicketAsync(TicketUpdateInput) returns (shared.CommonResponse); + rpc RemoveTicket(shared.StringId) returns (shared.CommonResponse); + rpc UpdateTicket(TicketUpdateInput) returns (shared.CommonResponse); rpc CloseTicket(CloseTicketInput) returns (shared.CommonResponse); } diff --git a/src/HuppyService/HuppyService.Service/Services/CommandLogService.cs b/src/HuppyService/HuppyService.Service/Services/CommandLogService.cs index 46e6383..b90b022 100644 --- a/src/HuppyService/HuppyService.Service/Services/CommandLogService.cs +++ b/src/HuppyService/HuppyService.Service/Services/CommandLogService.cs @@ -57,19 +57,7 @@ public override async Task GetAverageExecutionTime(Protos.V public override async Task AddCommand(CommandLogModel request, ServerCallContext context) { - var q = ReflectionMapper.Map(request); - - Core.Models.CommandLog commandLog = new() - { - ChannelId = request.ChannelId, - UserId = request.UserId, - CommandName = request.CommandName, - Date = Miscellaneous.UnixTimeStampToUtcDateTime(request.Date), - ErrorMessage = request.ErrorMessage, - ExecutionTimeMs = request.ExecutionTimeMs, - GuildId = request.GuildId, - IsSuccess = request.IsSuccess, - }; + var commandLog = ReflectionMapper.Map(request); var result = await _commandLogRepository.AddAsync(commandLog); await _commandLogRepository.SaveChangesAsync(); diff --git a/src/HuppyService/HuppyService.Service/Services/ReminderService.cs b/src/HuppyService/HuppyService.Service/Services/ReminderService.cs index b958e06..549e386 100644 --- a/src/HuppyService/HuppyService.Service/Services/ReminderService.cs +++ b/src/HuppyService/HuppyService.Service/Services/ReminderService.cs @@ -35,7 +35,7 @@ public override async Task GetSortedUserReminders(Sorte Id = reminder.Id, Message = reminder.Message, UserId = reminder.UserId, - UnixTime = Miscellaneous.DateTimeToUnixTimeStamp(reminder.RemindDate) + RemindDate = Miscellaneous.DateTimeToUnixTimeStamp(reminder.RemindDate) }).ToList()); return result; @@ -58,14 +58,14 @@ public override async Task GetSortedUserReminders(Sorte { Id = reminder.Id, Message = reminder.Message, - UnixTime = Miscellaneous.DateTimeToUnixTimeStamp(reminder.RemindDate), + RemindDate = Miscellaneous.DateTimeToUnixTimeStamp(reminder.RemindDate), UserId = reminder.UserId }; } public override async Task AddReminder(ReminderModel request, ServerCallContext context) { - var reminderDate = Miscellaneous.UnixTimeStampToUtcDateTime(request.UnixTime).ToUniversalTime(); + var reminderDate = Miscellaneous.UnixTimeStampToUtcDateTime(request.RemindDate).ToUniversalTime(); Reminder reminder = new() { Message = request.Message, @@ -80,7 +80,7 @@ public override async Task AddReminder(ReminderModel request, Ser _logger.LogInformation("Added reminder for [{user}] at [{date}] UTC", request.UserId, reminder.RemindDate); - return new ReminderModel() { Id = reminder.Id, Message = reminder.Message, UnixTime = request.UnixTime, UserId = reminder.UserId }; + return new ReminderModel() { Id = reminder.Id, Message = reminder.Message, RemindDate = request.RemindDate, UserId = reminder.UserId }; } public override async Task GetReminderBatch(ReminderBatchInput request, ServerCallContext context) @@ -103,7 +103,7 @@ public override async Task AddReminder(ReminderModel request, Ser Id = reminder.Id, Message = reminder.Message, UserId = reminder.UserId, - UnixTime = Miscellaneous.DateTimeToUnixTimeStamp(reminder.RemindDate) + RemindDate = Miscellaneous.DateTimeToUnixTimeStamp(reminder.RemindDate) }).ToList()); return result; @@ -122,7 +122,7 @@ public override async Task GetUserReminders(Protos.User Id = reminder.Id, Message = reminder.Message, UserId = reminder.UserId, - UnixTime = Miscellaneous.DateTimeToUnixTimeStamp(reminder.RemindDate) + RemindDate = Miscellaneous.DateTimeToUnixTimeStamp(reminder.RemindDate) })); return result; @@ -133,7 +133,7 @@ public override async Task RemoveReminder(ReminderModel request, Reminder reminder = new() { Message = request.Message, - RemindDate = Miscellaneous.UnixTimeStampToUtcDateTime(request.UnixTime), + RemindDate = Miscellaneous.UnixTimeStampToUtcDateTime(request.RemindDate), UserId = request.UserId }; diff --git a/src/HuppyService/HuppyService.Service/Services/TicketService.cs b/src/HuppyService/HuppyService.Service/Services/TicketService.cs index d58d270..7725a21 100644 --- a/src/HuppyService/HuppyService.Service/Services/TicketService.cs +++ b/src/HuppyService/HuppyService.Service/Services/TicketService.cs @@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; using System.Net.Sockets; +using System.Reflection.Metadata.Ecma335; namespace HuppyService.Service.Services { @@ -23,32 +24,41 @@ public override async Task AddTicket(AddTicketInput request, Server { if (string.IsNullOrEmpty(request.Description)) throw new ArgumentException("Ticket description cannot be null"); - Ticket ticket = new() - { - Id = Guid.NewGuid().ToString(), - Topic = request.Topic, - Description = request.Description, - CreatedDate = DateTime.UtcNow, - TicketAnswer = null, - ClosedDate = null, - IsClosed = false, - UserId = request.UserId, - }; + var ticket = ReflectionMapper.Map(request); + + ticket.Id = Guid.NewGuid().ToString(); + ticket.CreatedDate = DateTime.UtcNow; + ticket.TicketAnswer = null; + ticket.IsClosed = false; + ticket.ClosedDate = null; + + //Ticket ticket = new() + //{ + // Id = Guid.NewGuid().ToString(), + // Topic = request.Topic, + // Description = request.Description, + // CreatedDate = DateTime.UtcNow, + // TicketAnswer = null, + // ClosedDate = null, + // IsClosed = false, + // UserId = request.UserId, + //}; await _ticketRepository.AddAsync(ticket); await _ticketRepository.SaveChangesAsync(); - return new TicketModel() - { - Id = ticket.Id, - UserId = ticket.UserId, - Description = ticket.Description, - Topic = ticket.Topic, - IsClosed = ticket.IsClosed, - TicketAnswer = ticket.TicketAnswer, - ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate), - CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate) - }; + return ReflectionMapper.Map(ticket); + //return new TicketModel() + //{ + // Id = ticket.Id, + // UserId = ticket.UserId, + // Description = ticket.Description, + // Topic = ticket.Topic, + // IsClosed = ticket.IsClosed, + // TicketAnswer = ticket.TicketAnswer, + // ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate), + // CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate) + //}; } public override async Task CloseTicket(CloseTicketInput request, ServerCallContext context) @@ -70,7 +80,7 @@ public override async Task CloseTicket(CloseTicketInput request, return new CommonResponse() { IsSuccess = true }; } - public override async Task GetCountAsync(UserId request, ServerCallContext context) + public override async Task GetCount(UserId request, ServerCallContext context) { var tickets = await _ticketRepository.GetAllAsync(); @@ -88,6 +98,12 @@ public override async Task GetPaginatedTickets(GetPaginat .Take(request.Take) .ToList(); + // TODO: use mapper + //var result = new TicketModelCollection(); + //var temp = ReflectionMapper.Map(tickets.ToArray()); + //result.TicketsModels.AddRange(temp); + + var result = new TicketModelCollection(); result.TicketsModels.AddRange(tickets.Select(ticket => new TicketModel { @@ -100,17 +116,37 @@ public override async Task GetPaginatedTickets(GetPaginat ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate), CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate) })); - + return result; } public override async Task GetTicket(GetTicketInput request, ServerCallContext context) { var ticket = await _ticketRepository.GetAsync(request.TicketId); - + if (ticket is null) return null!; - return new TicketModel + return ReflectionMapper.Map(ticket); + //return new TicketModel + //{ + // Id = ticket.Id, + // UserId = ticket.UserId, + // Description = ticket.Description, + // Topic = ticket.Topic, + // IsClosed = ticket.IsClosed, + // TicketAnswer = ticket.TicketAnswer, + // ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate), + // CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate) + //}; + } + + public override async Task GetTickets(Protos.Void request, ServerCallContext context) + { + var ticketsQuery = await _ticketRepository.GetAllAsync(); + var tickets = await ticketsQuery.ToListAsync(); + + var result = new TicketModelCollection(); + result.TicketsModels.AddRange(tickets.Select(ticket => new TicketModel { Id = ticket.Id, UserId = ticket.UserId, @@ -120,32 +156,84 @@ public override async Task GetTicket(GetTicketInput request, Server TicketAnswer = ticket.TicketAnswer, ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate), CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate) - }; - } + })); - public override Task GetTickets(Protos.Void request, ServerCallContext context) - { - return base.GetTickets(request, context); + return result; } - public override Task GetUserPaginatedTickets(GetUserPaginatedTicketsInput request, ServerCallContext context) + public override async Task GetUserPaginatedTickets(GetUserPaginatedTicketsInput request, ServerCallContext context) { - return base.GetUserPaginatedTickets(request, context); + var tickets = await (await _ticketRepository.GetAllAsync()) + .Where(ticket => ticket.UserId == request.UserId) + .OrderBy(ticket => ticket.IsClosed) + .ThenByDescending(ticket => ticket.CreatedDate) + .Skip(request.Skip) + .Take(request.Take) + .ToListAsync(); + + var result = new TicketModelCollection(); + result.TicketsModels.AddRange(tickets.Select(ticket => new TicketModel + { + Id = ticket.Id, + UserId = ticket.UserId, + Description = ticket.Description, + Topic = ticket.Topic, + IsClosed = ticket.IsClosed, + TicketAnswer = ticket.TicketAnswer, + ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate), + CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate) + })); + + return result; } - public override Task GetUserTickets(UserId request, ServerCallContext context) + public override async Task GetUserTickets(UserId request, ServerCallContext context) { - return base.GetUserTickets(request, context); + var tickets = await (await _ticketRepository.GetAllAsync()) + .Where(ticket => ticket.UserId == request.Id) + .ToListAsync(); + + var result = new TicketModelCollection(); + result.TicketsModels.AddRange(tickets.Select(ticket => new TicketModel + { + Id = ticket.Id, + UserId = ticket.UserId, + Description = ticket.Description, + Topic = ticket.Topic, + IsClosed = ticket.IsClosed, + TicketAnswer = ticket.TicketAnswer, + ClosedDate = ticket.ClosedDate is null ? 0 : Miscellaneous.DateTimeToUnixTimeStamp((DateTime)ticket.ClosedDate), + CreatedDate = Miscellaneous.DateTimeToUnixTimeStamp(ticket.CreatedDate) + })); + + return result; } - public override Task RemoveTicketAsync(StringId request, ServerCallContext context) + public override async Task RemoveTicket(StringId request, ServerCallContext context) { - return base.RemoveTicketAsync(request, context); + if (string.IsNullOrEmpty(request.Id)) throw new ArgumentException("Ticket cannot be null or empty"); + + await _ticketRepository.RemoveAsync(request.Id); + await _ticketRepository.SaveChangesAsync(); + + return new() { IsSuccess = true }; } - public override Task UpdateTicketAsync(TicketUpdateInput request, ServerCallContext context) + public override async Task UpdateTicket(TicketUpdateInput request, ServerCallContext context) { - return base.UpdateTicketAsync(request, context); + if (string.IsNullOrEmpty(request.TicketId) || string.IsNullOrEmpty(request.Description)) + throw new ArgumentException("Both ticked ID and ticket description cannot be null or empty"); + + var ticket = await _ticketRepository.GetAsync(request.TicketId); + + if (ticket is null) throw new Exception("Ticket doesn't exist"); + + ticket.Description = request.Description; + + await _ticketRepository.UpdateAsync(ticket); + await _ticketRepository.SaveChangesAsync(); + + return new() { IsSuccess = true }; } } }