Skip to content

Commit

Permalink
T #98 ticket service (unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Nov 1, 2022
1 parent 07ca81d commit fe07ae4
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 150 deletions.
9 changes: 5 additions & 4 deletions src/Huppy/Huppy.App/Commands/SlashCommands/DevCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -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}```",
Expand Down
15 changes: 9 additions & 6 deletions src/Huppy/Huppy.App/Commands/SlashCommands/TicketCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}`");

Expand Down
5 changes: 5 additions & 0 deletions src/Huppy/Huppy.App/Configuration/ModuleConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public ModuleConfigurator AddGRPCServices()
options.Address = huppyCoreUrl;
});

_services.AddGrpcClient<TicketProto.TicketProtoClient>((services, options) =>
{
options.Address = huppyCoreUrl;
});

return this;
}

Expand Down
13 changes: 7 additions & 6 deletions src/Huppy/Huppy.Core/Interfaces/IServices/ITicketService.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using Discord;
using Huppy.Core.Models;
using HuppyService.Service.Protos.Models;

namespace Huppy.Core.Interfaces.IServices
{
public interface ITicketService
{
Task<int> GetCountAsync(ulong userId);
Task<IEnumerable<Ticket>> GetTicketsAsync();
Task<IEnumerable<Ticket>> GetTicketsAsync(ulong userId);
Task<IEnumerable<Ticket>> GetPaginatedTickets(int skip, int take);
Task<IEnumerable<Ticket>> GetPaginatedTickets(ulong userId, int skip, int take);
Task<Ticket?> GetTicketAsync(string ticketId, ulong userId);
Task<Ticket?> AddTicketAsync(IUser user, string topic, string description);
Task<IList<TicketModel>> GetTicketsAsync();
Task<IList<TicketModel>> GetTicketsAsync(ulong userId);
Task<IList<TicketModel>> GetPaginatedTickets(int skip, int take);
Task<IList<TicketModel>> GetPaginatedTickets(ulong userId, int skip, int take);
Task<TicketModel?> GetTicketAsync(string ticketId, ulong userId);
Task<TicketModel?> AddTicketAsync(IUser user, string topic, string description);
Task RemoveTicketAsync(string ticketId);
Task UpdateTicketAsync(string ticketId, string description);
Task CloseTicket(string ticketId, string answer);
Expand Down
6 changes: 3 additions & 3 deletions src/Huppy/Huppy.Core/Services/Reminder/ReminderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
};

Expand Down Expand Up @@ -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)
Expand Down
137 changes: 71 additions & 66 deletions src/Huppy/Huppy.Core/Services/Ticket/TicketService.cs
Original file line number Diff line number Diff line change
@@ -1,132 +1,137 @@
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;

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<int> 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<IEnumerable<Models.Ticket>> GetTicketsAsync()
public async Task<IList<TicketModel>> GetTicketsAsync()
{
return await _ticketRepository.GetAllAsync();
var result = await _ticketClient.GetTicketsAsync(new());
return result.TicketsModels;
}

public async Task<IEnumerable<Models.Ticket>> GetTicketsAsync(ulong userId)
public async Task<IList<TicketModel>> 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<IEnumerable<Models.Ticket>> GetPaginatedTickets(int skip, int take)
public async Task<IList<TicketModel>> 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<IEnumerable<Models.Ticket>> GetPaginatedTickets(ulong userId, int skip, int take)
public async Task<IList<TicketModel>> 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<Models.Ticket?> GetTicketAsync(string ticketId, ulong userId)
public async Task<TicketModel?> 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<Models.Ticket?> AddTicketAsync(IUser user, string topic, string description)
public async Task<TicketModel?> 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)
{
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)
{
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();
}
}
Loading

0 comments on commit fe07ae4

Please sign in to comment.