From d6c879f3f47a7c134f522fdb3741601af1e77526 Mon Sep 17 00:00:00 2001 From: pizzaburgare <72048430+pizzaburgare@users.noreply.github.com> Date: Fri, 26 Apr 2024 11:22:41 +0200 Subject: [PATCH] Works by checking for each ticket if the corresponding event has passed Make Event.Start/End DateTime? --- .../Controllers/TicketsControllerTest.cs | 6 +++--- Nexpo/Controllers/Events/TicketsController.cs | 20 ++++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Nexpo.Tests/Controllers/TicketsControllerTest.cs b/Nexpo.Tests/Controllers/TicketsControllerTest.cs index a78405d..34d7239 100644 --- a/Nexpo.Tests/Controllers/TicketsControllerTest.cs +++ b/Nexpo.Tests/Controllers/TicketsControllerTest.cs @@ -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); } } } diff --git a/Nexpo/Controllers/Events/TicketsController.cs b/Nexpo/Controllers/Events/TicketsController.cs index 648962f..46fb202 100644 --- a/Nexpo/Controllers/Events/TicketsController.cs +++ b/Nexpo/Controllers/Events/TicketsController.cs @@ -75,9 +75,23 @@ public async Task 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"); }