Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions TickAPI/TickAPI.Tests/Tickets/Services/TicketServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,118 @@ public async Task GetTicketDetailsAsync_WhenTicketDoesNotExistForTheUser_ShouldR
Assert.Equal(StatusCodes.Status404NotFound, res.StatusCode);
Assert.Equal("Ticket with this id doesn't exist for this user", res.ErrorMsg);
}

[Fact]
public async Task GetTicketsForCustomerAsync_WithValidInput_ReturnsSuccessResult()
{
// Arrange
var email = "test@example.com";
var page = 0;
var pageSize = 10;

var tickets = new List<Ticket>
{
new Ticket
{
Type = new TicketType
{
Event = new Event
{
Name = "EventName",
StartDate = new DateTime(2025, 10, 10),
EndDate = new DateTime(2025, 10, 20),
}
}
},
new Ticket
{
Type = new TicketType
{
Event = new Event
{
Name = "EventName2",
StartDate = new DateTime(2025, 11, 10),
EndDate = new DateTime(2025, 11, 20),
}
}
}
};

var paginatedData = new PaginatedData<Ticket>
(
tickets,
page,
pageSize,
false,
false,
new PaginationDetails(0, 2)
);
var mappedData1 = new GetTicketForCustomerDto("EventName", new DateTime(2025, 10, 10), new DateTime(2025, 10, 20));
var mappedData2 = new GetTicketForCustomerDto("EventName2", new DateTime(2025, 11, 10), new DateTime(2025, 11, 20));
var mappedPaginatedData = new PaginatedData<GetTicketForCustomerDto>
(
new List<GetTicketForCustomerDto>{mappedData1, mappedData2},
page,
pageSize,
false,
false,
new PaginationDetails(0, 2)
);

var ticketRepositoryMock = new Mock<ITicketRepository>();
ticketRepositoryMock.Setup(r => r.GetTicketsByCustomerEmail(email)).Returns(tickets.AsQueryable());

var paginationServiceMock = new Mock<IPaginationService>();
paginationServiceMock.Setup(p => p.PaginateAsync(tickets.AsQueryable(), pageSize, page))
.ReturnsAsync(Result<PaginatedData<Ticket>>.Success(paginatedData));

paginationServiceMock.Setup(p => p.MapData(paginatedData, It.IsAny<Func<Ticket, GetTicketForCustomerDto>>()))
.Returns(mappedPaginatedData);
var sut = new TicketService(ticketRepositoryMock.Object, paginationServiceMock.Object);

// Act
var result = await sut.GetTicketsForCustomerAsync(email, page, pageSize);

// Assert
Assert.True(result.IsSuccess);
Assert.Equal(mappedPaginatedData, result.Value);
Assert.Equal(mappedData1, result.Value!.Data[0]);
Assert.Equal(mappedData2, result.Value!.Data[1]);
}

[Fact]
public async Task GetTicketsForCustomerAsync_WhenUserHasNoTickets_ReturnsEmptyPagination()
{
// Arrange
var email = "empty@example.com";
var page = 0;
var pageSize = 10;

var emptyTickets = new List<Ticket>();

var emptyPaginatedData = new PaginatedData<Ticket>(emptyTickets, page, pageSize,
false, false, new PaginationDetails(0, 0));

var paginatedResult = Result<PaginatedData<Ticket>>.Success(emptyPaginatedData);

var mappedEmptyPaginatedData = new PaginatedData<GetTicketForCustomerDto>(new List<GetTicketForCustomerDto>(),
page, pageSize, false, false, new PaginationDetails(0, 0));

var ticketRepositoryMock = new Mock<ITicketRepository>();
ticketRepositoryMock.Setup(r => r.GetTicketsByCustomerEmail(email)).Returns(emptyTickets.AsQueryable());

var paginationServiceMock = new Mock<IPaginationService>();
paginationServiceMock.Setup(p => p.PaginateAsync(emptyTickets.AsQueryable(), pageSize, page)).ReturnsAsync(paginatedResult);
paginationServiceMock.Setup(p => p.MapData(emptyPaginatedData, It.IsAny<Func<Ticket, GetTicketForCustomerDto>>()))
.Returns(mappedEmptyPaginatedData);

var sut = new TicketService(ticketRepositoryMock.Object, paginationServiceMock.Object);

// Act
var result = await sut.GetTicketsForCustomerAsync(email, page, pageSize);

// Assert
Assert.True(result.IsSuccess);
Assert.Empty(result.Value!.Data);
}
}
1 change: 1 addition & 0 deletions TickAPI/TickAPI/Tickets/Abstractions/ITicketRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public interface ITicketRepository
public IQueryable<Ticket> GetAllTicketsByTicketType(TicketType ticketType);
public Task<Result<Ticket>> GetTicketWithDetailsByIdAndEmailAsync(Guid id, string email);
public IQueryable<Ticket> GetTicketsByEventId(Guid eventId);
public IQueryable<Ticket> GetTicketsByCustomerEmail(string email);
}
2 changes: 2 additions & 0 deletions TickAPI/TickAPI/Tickets/Abstractions/ITicketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ITicketService
public Task<Result<GetTicketDetailsResponseDto>> GetTicketDetailsAsync(Guid ticketGuid, string email);
public Task<Result<PaginatedData<GetTicketForResellResponseDto>>> GetTicketsForResellAsync(Guid eventId, int page,
int pageSize);
public Task<Result<PaginatedData<GetTicketForCustomerDto>>> GetTicketsForCustomerAsync(string email, int page,
int pageSize);
}
17 changes: 17 additions & 0 deletions TickAPI/TickAPI/Tickets/Controllers/TicketsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,21 @@ public async Task<ActionResult<PaginatedData<GetTicketForResellResponseDto>>> Ge
}
return result.Value!;
}

[AuthorizeWithPolicy(AuthPolicies.CustomerPolicy)]
[HttpGet]
public async Task<ActionResult<PaginatedData<GetTicketForCustomerDto>>> GetTicketsForCustomer([FromQuery] int pageSize, [FromQuery] int page)
{
var emailResult = _claimsService.GetEmailFromClaims(User.Claims);
if (emailResult.IsError)
{
return StatusCode(emailResult.StatusCode, emailResult.ErrorMsg);
}
var tickets = await _ticketService.GetTicketsForCustomerAsync(emailResult.Value!, page, pageSize);
if (tickets.IsError)
{
return StatusCode(tickets.StatusCode, tickets.ErrorMsg);
}
return Ok(tickets.Value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TickAPI.Tickets.DTOs.Response;

public record GetTicketForCustomerDto
(
string EventName,
DateTime EventStartDate,
DateTime EventEndDate
);
9 changes: 9 additions & 0 deletions TickAPI/TickAPI/Tickets/Repositories/TicketRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public IQueryable<Ticket> GetTicketsByEventId(Guid eventId)
.Include(t => t.Type.Event)
.Where(t => t.Type.Event.Id == eventId);
}

public IQueryable<Ticket> GetTicketsByCustomerEmail(string email)
{
return _tickApiDbContext.Tickets
.Include(t => t.Owner)
.Include(t => t.Type)
.Include(t =>t.Type.Event)
.Where(t => t.Owner.Email == email);
}

public async Task<Result<Ticket>> GetTicketWithDetailsByIdAndEmailAsync(Guid id, string email)
{
Expand Down
15 changes: 15 additions & 0 deletions TickAPI/TickAPI/Tickets/Services/TicketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public async Task<Result<PaginatedData<GetTicketForResellResponseDto>>> GetTicke
t => new GetTicketForResellResponseDto(t.Id, t.Type.Price, t.Type.Currency, t.Type.Description, t.Seats));
return Result<PaginatedData<GetTicketForResellResponseDto>>.Success(paginatedResult);
}
//TODO: Maybe apply some filtering over here?
public async Task<Result<PaginatedData<GetTicketForCustomerDto>>> GetTicketsForCustomerAsync(string email, int page, int pageSize)
{
var customerTickets = _ticketRepository.GetTicketsByCustomerEmail(email);
var paginatedCustomerTickets = await _paginationService.PaginateAsync(customerTickets, pageSize, page);
if (paginatedCustomerTickets.IsError)
{
return Result<PaginatedData<GetTicketForCustomerDto>>.PropagateError(paginatedCustomerTickets);
}

var paginatedResult = _paginationService.MapData(paginatedCustomerTickets.Value!,
t => new GetTicketForCustomerDto(t.Type.Event.Name, t.Type.Event.StartDate, t.Type.Event.EndDate));

return Result<PaginatedData<GetTicketForCustomerDto>>.Success(paginatedResult);
}

public async Task<Result<GetTicketDetailsResponseDto>> GetTicketDetailsAsync(Guid ticketGuid, string email)
{
Expand Down
Loading