Skip to content

Commit

Permalink
Works by checking for each ticket if the corresponding event has passed
Browse files Browse the repository at this point in the history
Make Event.Start/End DateTime?
  • Loading branch information
pizzaburgare committed Apr 26, 2024
1 parent 3800cff commit d6c879f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Nexpo.Tests/Controllers/TicketsControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,18 +1103,18 @@ public async Task sendTicketViaEmailSeveralTicketsNonLoggedIn(){
[Fact]
public async Task TooManyTickets()
{
// Student2 already has 5 tickets
// Student2 already has 5 tickets but not -7
var client = await TestUtils.Login("student2");
var json = new JsonObject
{
{ "eventid", -2 },
{ "eventid", -7 },
{ "photook", true }
};
var payload = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
var response = await client.PostAsync("api/tickets", payload);
// Verify response - Conflict because the student already has a ticket for the event
Assert.True(response.StatusCode.Equals(HttpStatusCode.TooManyRequests),
"Wrong status code. Expected: TooManyRequests. Received: " + response.ToString());
"Wrong status code. Expected: TooManyRequests. Received: " + response);
}
}
}
20 changes: 17 additions & 3 deletions Nexpo/Controllers/Events/TicketsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,23 @@ public async Task<ActionResult> PostTicket(CreateTicketDTO DTO)
}

// Check for max number of registrations
int max = 5;
int noTickets = (await _ticketRepo.GetAllForUser(userId)).Count();
if (noTickets >= max)
int maxNoTickets = 5;
// Assmues no event found or faulty format of date should not be counted
int noTickets = (await _ticketRepo.GetAllForUser(userId)).Count(ticket =>
{
var foundEvent = _eventRepo.Get(ticket.EventId);
if (foundEvent.Result == null)
{
return false;
}
bool isSuccess = DateTime.TryParse(foundEvent.Result.Start, out var eventDateTime);
if (isSuccess)
{
return eventDateTime > DateTime.Now;
}
return false;
});
if (noTickets >= maxNoTickets)
{
return StatusCode(429, "Too many tickets");
}
Expand Down

0 comments on commit d6c879f

Please sign in to comment.