From 42e2c4abe2eeee438ddd5ee1653f47da23e8fe80 Mon Sep 17 00:00:00 2001 From: Jeroen Faas Date: Wed, 12 Jun 2024 01:14:08 +0200 Subject: [PATCH] Pair programming met Kai en Stefhan Phteven --- ProjectB/Client/DepotClient.cs | 139 +++++------------- ProjectB/Program.cs | 6 + ProjectB/ProjectB.csproj | 1 - .../Workflows/EmployeeFlows/AddGuestFlow.cs | 63 ++++++++ .../EmployeeFlows/RemoveGuestFlow.cs | 64 ++++++++ .../Workflows/EmployeeFlows/StartTourFlow.cs | 87 +++++++++++ 6 files changed, 255 insertions(+), 105 deletions(-) create mode 100644 ProjectB/Workflows/EmployeeFlows/AddGuestFlow.cs create mode 100644 ProjectB/Workflows/EmployeeFlows/RemoveGuestFlow.cs create mode 100644 ProjectB/Workflows/EmployeeFlows/StartTourFlow.cs diff --git a/ProjectB/Client/DepotClient.cs b/ProjectB/Client/DepotClient.cs index 8683ad9..3111931 100644 --- a/ProjectB/Client/DepotClient.cs +++ b/ProjectB/Client/DepotClient.cs @@ -5,6 +5,7 @@ using ProjectB.Models; using ProjectB.Services; using ProjectB.Workflows; +using ProjectB.Workflows.EmployeeFlows; using ProjectB.Workflows.GuestFlows; using Spectre.Console; @@ -252,12 +253,7 @@ private void ShowTourMenu(Tour tour) private void BeginStartTour(Tour tour) { - if (tour.Departed) - { - Print("tourAlreadyDeparted"); - Prompts.ShowSpinner("returningToMenu", 2000); - return; - } + var flow = GetFlow(); Print("employeeTourInfo", new() { tour.Start.ToShortTimeString() }); Console.WriteLine(); @@ -267,6 +263,9 @@ private void BeginStartTour(Tour tour) Print("guestList", new() { guestNumber }); Console.WriteLine(); + if (!HandleFlowResult(flow.SetTour(tour))) + return; + if (tour.EmployeeNumber != Employee!.EmployeeNumber) { if (!string.IsNullOrWhiteSpace(tour.EmployeeNumber)) @@ -279,16 +278,13 @@ private void BeginStartTour(Tour tour) } } - tour.EmployeeNumber = Employee.EmployeeNumber; - TourService.SaveChanges(); + if (!HandleFlowResult(flow.SetEmployeeNumber(Employee.EmployeeNumber))) + return; } Print("scanAllTickets"); - List scannedTickets = new List(); - - bool continueScanning = true; - while (scannedTickets.Count != tour.Participants.Count && continueScanning) + while (flow.ScannedTickets.Count != tour.Participants.Count && flow.Scanning) { if (!int.TryParse(Prompts.AskTicketOrEmployeeNumber("ticketNumberOrEmployeeNumber"), out int number)) continue; @@ -304,40 +300,22 @@ private void BeginStartTour(Tour tour) Print("finishedScanningQuestion"); if (Prompts.AskYesNo("confirmYesNo", "yes", "no")) { - continueScanning = false; + flow.Scanning = false; continue; } } - if (GuestService.FindValidGuestById(number.ToString()) == null) - { - Print("invalidTicketNumber"); - continue; - } - - if (scannedTickets.Contains(number)) - { - Print("ticketAlreadyScanned"); - continue; - } + if (!HandleFlowResult(flow.AddGuest(number.ToString()))) + return; - if (!tour.Participants.Contains(number.ToString())) - { - Print("ticketNotInTour"); - continue; - } - - scannedTickets.Add(number); Print("ticketScanned", new() { number.ToString() }); Affirmation(); } Print("finishedScanning"); - tour.Participants = scannedTickets.Select(number => number.ToString()).ToList(); - - continueScanning = true; - while (scannedTickets.Count < tour.Capacity && continueScanning) + flow.Scanning = true; + while (flow.ScannedTickets.Count < tour.Capacity && flow.Scanning) { if (!int.TryParse(Prompts.AskTicketOrEmployeeNumber("ticketNumberOrEmployeeNumber"), out int number)) continue; @@ -347,18 +325,14 @@ private void BeginStartTour(Tour tour) Print("finishedScanningQuestion"); if (Prompts.AskYesNo("confirmYesNo", "yes", "no")) { - continueScanning = false; + flow.Scanning = false; continue; } } - if (scannedTickets.Contains(number)) - { - Print("ticketAlreadyScanned"); - continue; - } + if (!HandleFlowResult(flow.AddGuest(number.ToString(), true))) + return; - scannedTickets.Add(number); Print("extraTicketScanned", new() { number.ToString() }); Affirmation(); } @@ -366,82 +340,47 @@ private void BeginStartTour(Tour tour) Prompts.ShowSpinner("finishedScanningExtraTickets", 2000); Console.Clear(); - tour.Participants = scannedTickets.Select(number => number.ToString()).ToList(); - Print("employeeTourInfo", new() { tour.Start.ToShortTimeString() }); Console.WriteLine(); Print("registeredGuests"); - foreach (var guestNumber in tour.Participants) + foreach (var guestNumber in flow.ScannedTickets) Print("guestList", new() { guestNumber }); Print("confirmStartTour"); - if (!Prompts.AskYesNo("confirmYesNo", "yes", "no")) - { - Prompts.ShowSpinner("returningToMenu", 2000); - return; - } - tour.Departed = true; - TourService.SaveChanges(); + HandleFlowConfirmation(flow, + TGet("confirmStartTour"), + TGet("startTourSuccess"), + TGet("startTourCancelled")); Prompts.ShowSpinner("returningToMenu", 2000); } private void BeginAddGuest(Tour tour) { - if (tour.Departed) - { - Print("tourAlreadyDeparted"); - Prompts.ShowSpinner("returningToMenu", 2000); - return; - } + var flow = GetFlow(); Print("employeeTourInfo", new() { tour.Start.ToShortTimeString() }); Console.WriteLine(); - if (tour.Participants.Count == tour.Capacity) - { - Print("noSpaceInTour"); - Prompts.ShowSpinner("returningToMenu", 2000); - return; - } - - string ticketNumber = Prompts.AskTicketNumber("ticketNumber"); - var guest = GuestService.FindValidGuestById(ticketNumber); - if (guest == null) - { - Print("guestNotFound"); - Prompts.ShowSpinner("returningToMenu", 2000); + if (!HandleFlowResult(flow.SetTour(tour))) return; - } - var currentTour = TourService.GetTourForGuest(guest); - if (currentTour != null) - { - Print("guestAlreadyHasReservation"); - Prompts.ShowSpinner("returningToMenu", 2000); + if (!HandleFlowResult(flow.SetGuest(Prompts.AskTicketNumber("scanGuestTicket")))) return; - } - Print("confirmAddGuest", new() { tour.Start.ToShortTimeString() }); - if (Prompts.AskYesNo("confirmYesNo", "yes", "no")) - { - if (guest != null && tour != null) - TourService.RegisterGuestForTour(guest, tour); - } + HandleFlowConfirmation(flow, + TGet("confirmAddGuest", new() { flow.Guest!.TicketNumber }), + TGet("addSuccess", new() { flow.Guest!.TicketNumber }), + TGet("addCancelled")); Prompts.ShowSpinner("returningToMenu", 2000); } private void BeginRemoveGuest(Tour tour) { - if (tour.Departed) - { - Print("tourAlreadyDeparted"); - Prompts.ShowSpinner("returningToMenu", 2000); - return; - } + var flow = GetFlow(); Print("employeeTourInfo", new() { tour.Start.ToShortTimeString() }); Console.WriteLine(); @@ -451,24 +390,16 @@ private void BeginRemoveGuest(Tour tour) Print("guestList", new() { guestNumber }); Console.WriteLine(); - string ticketNumber = Prompts.AskTicketNumber("ticketNumberToRemove"); - var guest = GuestService.FindValidGuestById(ticketNumber); - if (guest == null || !tour.Participants.Contains(guest.TicketNumber)) - { - Print("guestNotFoundInTour"); - Prompts.ShowSpinner("returningToMenu", 2000); + if (!HandleFlowResult(flow.SetTour(tour))) return; - } - Print("confirmGuestRemoval"); - if (!Prompts.AskYesNo("confirmYesNo", "yes", "no")) - { - Prompts.ShowSpinner("returningToMenu", 2000); + if (!HandleFlowResult(flow.SetGuest(Prompts.AskTicketNumber("scanGuestTicket")))) return; - } - if (guest != null) - TourService.DeleteReservationGuest(guest); + HandleFlowConfirmation(flow, + TGet("confirmGuestRemoval", new() { flow.Guest!.TicketNumber }), + TGet("removeSuccess", new() { flow.Guest!.TicketNumber }), + TGet("removeCancelled")); Prompts.ShowSpinner("returningToMenu", 2000); } diff --git a/ProjectB/Program.cs b/ProjectB/Program.cs index 6fc4a0d..58f9524 100644 --- a/ProjectB/Program.cs +++ b/ProjectB/Program.cs @@ -4,6 +4,7 @@ using ProjectB.Database; using ProjectB.Repositories; using ProjectB.Services; +using ProjectB.Workflows.EmployeeFlows; using ProjectB.Workflows.GuestFlows; using Spectre.Console; @@ -41,6 +42,11 @@ private static IServiceProvider GetServices() .AddTransient() .AddTransient() + .AddTransient() + .AddTransient() + .AddTransient() + + .BuildServiceProvider(); } } diff --git a/ProjectB/ProjectB.csproj b/ProjectB/ProjectB.csproj index 8d00ee8..5d09d09 100644 --- a/ProjectB/ProjectB.csproj +++ b/ProjectB/ProjectB.csproj @@ -77,7 +77,6 @@ - diff --git a/ProjectB/Workflows/EmployeeFlows/AddGuestFlow.cs b/ProjectB/Workflows/EmployeeFlows/AddGuestFlow.cs new file mode 100644 index 0000000..bf7be03 --- /dev/null +++ b/ProjectB/Workflows/EmployeeFlows/AddGuestFlow.cs @@ -0,0 +1,63 @@ +using ProjectB.Database; +using ProjectB.Models; +using ProjectB.Services; + +namespace ProjectB.Workflows.EmployeeFlows +{ + public class AddGuestFlow(IDatabaseContext context, ITourService tourService, IGuestService guestService) : AbstractWorkflow(context) + { + public Guest? Guest { get; private set; } + public Tour? Tour { get; private set; } + + public (bool Success, string MessageKey) SetGuest(string ticketNumber) + { + if (string.IsNullOrWhiteSpace(ticketNumber)) + return (false, "guestIsNull"); + + var guest = guestService.FindValidGuestById(ticketNumber); + + if (guest == null) + return (false, "invalidTicket"); + + if (tourService.GetTourForGuest(guest) != null) + return (false, "alreadyHasReservation"); + + Guest = guest; + + return (true, string.Empty); + } + + public (bool Success, string MessageKey) SetTour(Tour tour) + { + if (tour == null) + return (false, "tourIsNull"); + + if (tour.Participants.Count >= tour.Capacity) + return (false, "tourFull"); + + if (tour.Start < DateTime.Now) + return (false, "tourInPast"); + + if (tour.Departed) + return (false, "tourDeparted"); + + Tour = tour; + + return (true, string.Empty); + } + + public override (bool Success, string MessageKey) Commit() + { + if (Guest == null) + return (false, "guestIsNull"); + + if (Tour == null) + return (false, "tourIsNull"); + + tourService.RegisterGuestForTour(Guest, Tour); + + //No need to call base commit for this due to the service handling the changes + return (true, string.Empty); + } + } +} diff --git a/ProjectB/Workflows/EmployeeFlows/RemoveGuestFlow.cs b/ProjectB/Workflows/EmployeeFlows/RemoveGuestFlow.cs new file mode 100644 index 0000000..e037845 --- /dev/null +++ b/ProjectB/Workflows/EmployeeFlows/RemoveGuestFlow.cs @@ -0,0 +1,64 @@ +using ProjectB.Database; +using ProjectB.Models; +using ProjectB.Services; + +namespace ProjectB.Workflows.EmployeeFlows +{ + public class RemoveGuestFlow(IDatabaseContext context, ITourService tourService, IGuestService guestService) : AbstractWorkflow(context) + { + public Guest? Guest { get; private set; } + public Tour? Tour { get; private set; } + + public (bool Success, string MessageKey) SetGuest(string ticketNumber) + { + if (string.IsNullOrWhiteSpace(ticketNumber)) + return (false, "guestIsNull"); + + var guest = guestService.FindValidGuestById(ticketNumber); + + if (guest == null) + return (false, "guestIsNull"); + + var currentTour = tourService.GetTourForGuest(guest); + if (currentTour == null) + return (false, "noReservationFound"); + + Guest = guest; + + return (true, string.Empty); + } + + public (bool Success, string MessageKey) SetTour(Tour tour) + { + if (tour == null) + return (false, "tourIsNull"); + + if (tour.Participants.Count == 0) + return (false, "tourEmpty"); + + if (tour.Start < DateTime.Now) + return (false, "tourInPast"); + + if (tour.Departed) + return (false, "tourDeparted"); + + Tour = tour; + + return (true, string.Empty); + } + + public override (bool Success, string MessageKey) Commit() + { + if (Guest == null) + return (false, "guestIsNull"); + + if (Tour == null) + return (false, "tourIsNull"); + + tourService.DeleteReservationGuest(Guest); + + //No need to call base commit for this due to the service handling the changes + return (true, string.Empty); + } + } +} diff --git a/ProjectB/Workflows/EmployeeFlows/StartTourFlow.cs b/ProjectB/Workflows/EmployeeFlows/StartTourFlow.cs new file mode 100644 index 0000000..2af0d85 --- /dev/null +++ b/ProjectB/Workflows/EmployeeFlows/StartTourFlow.cs @@ -0,0 +1,87 @@ +using ProjectB.Database; +using ProjectB.Models; +using ProjectB.Services; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace ProjectB.Workflows.EmployeeFlows +{ + public class StartTourFlow(IDatabaseContext context, ITourService tourService, IGuestService guestService, IEmployeeService employeeService) : AbstractWorkflow(context) + { + public Tour? Tour { get; private set; } + public string EmployeeNumber { get; private set; } + public List ScannedTickets { get; private set; } = new List(); + public bool Scanning { get; set; } + + public (bool Success, string MessageKey) AddGuest(string ticketNumber, bool extra = false) + { + if (string.IsNullOrWhiteSpace(ticketNumber)) + return (false, "guestIsNull"); + + var guest = guestService.FindValidGuestById(ticketNumber); + + if (guest == null) + return (false, "invalidTicket"); + + if (ScannedTickets.Contains(ticketNumber)) + return (false, "ticketAlreadyScanned"); + + if (Tour == null) + return (false, "tourIsNull"); + + if (Tour.Participants.Contains(ticketNumber.ToString()) && !extra) + return (false, "ticketNotInTour"); + + ScannedTickets.Add(ticketNumber); + + return (true, string.Empty); + } + + public (bool Success, string MessageKey) SetTour(Tour tour) + { + if (tour == null) + return (false, "tourIsNull"); + + if (tour.Participants.Count >= tour.Capacity) + return (false, "tourFull"); + + if (tour.Start < DateTime.Now) + return (false, "tourInPast"); + + if (tour.Departed) + return (false, "tourDeparted"); + + Tour = tour; + + return (true, string.Empty); + } + + public (bool Success, string MessageKey) SetEmployeeNumber(string employeeNumber) + { + if (string.IsNullOrWhiteSpace(employeeNumber)) + return (false, "employeeNumberIsNull"); + + if (!employeeService.ValidateEmployeeNumber(employeeNumber)) + return (false, "employeeNumberInvalid"); + + EmployeeNumber = employeeNumber; + + return (true, string.Empty); + } + + public override (bool Success, string MessageKey) Commit() + { + if (!ScannedTickets.Any()) + return (false, "noScannedTickets"); + + if (Tour == null) + return (false, "tourIsNull"); + + Tour.Participants.AddRange(ScannedTickets); + Tour.EmployeeNumber = EmployeeNumber; + Tour.Departed = true; + + //No need to call base commit for this due to the service handling the changes + return base.Commit(); + } + } +}