Skip to content

Commit

Permalink
Merge pull request #159 from careerfairsystems/MaxTicketCheck
Browse files Browse the repository at this point in the history
Fixed check for number of tickets when booking
  • Loading branch information
LeoFjatstrom authored May 7, 2024
2 parents b3dc6b7 + 74768c3 commit 3fbd20c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Nexpo.Tests/Controllers/TicketsControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,5 +1100,22 @@ public async Task sendTicketViaEmailSeveralTicketsNonLoggedIn(){
Assert.True(response.StatusCode.Equals(HttpStatusCode.Unauthorized),"Wrong status code. Expected: Unauthorized. Received: " + response.StatusCode.ToString());
}

[Fact]
public async Task TooManyTickets()
{
// Student2 has 5 tickets already but not for the event with the id -7,
// Therefore booking event -7 should return "too many requests" because the user has too many tickets
var client = await TestUtils.Login("student2");
var json = new JsonObject
{
{ "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);
}
}
}
24 changes: 24 additions & 0 deletions Nexpo/Controllers/Events/TicketsController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -72,6 +73,29 @@ public async Task<ActionResult> PostTicket(CreateTicketDTO DTO)
{
return Conflict();
}

// Check for max number of registrations
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");
}


var ticket = new Ticket
{
Expand Down

0 comments on commit 3fbd20c

Please sign in to comment.