-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
268 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 7 additions & 6 deletions
13
src/Huppy/Huppy.Core/Interfaces/IServices/ITicketService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.