/// <summary> /// Registers the single pilot for a contest. /// </summary> /// <param name="pilotToRegister">The pilot reg.</param> /// <returns></returns> public async Task <Result <PilotRegistration> > CreateAsync(PilotRegistration pilotToRegister) { if (pilotToRegister == null) { return(Error <PilotRegistration>(null, $"{nameof(pilotToRegister)} cannot be null")); } if (!string.IsNullOrEmpty(pilotToRegister.Id)) { return(Error <PilotRegistration>(null, "The Id already has a value")); } var allPilotRegistrations = (await GetAll <PilotRegistration>())?.ToList() ?? new List <PilotRegistration>(); if (allPilotRegistrations == null) { allPilotRegistrations = new List <PilotRegistration>(); } // Assign a new GUID for the ID pilotToRegister.Id = Guid.NewGuid().ToString(); allPilotRegistrations.Add(pilotToRegister); try { var success = await WriteAll(allPilotRegistrations); } catch (Exception ex) { return(Error <PilotRegistration>(null, ex)); } return(Success((allPilotRegistrations.Where(p => p.Id == pilotToRegister.Id)).First(), nameof(CreateAsync))); }
/// <summary> /// Uns the register pilot for contest. /// </summary> /// <param name="pilotToUnregister">The pilot to unregister.</param> /// <returns></returns> public async Task <Result <bool> > UnRegisterPilotForContestAsync(PilotRegistration pilotToUnregister) { var validationResult = ValidatePilotRegistration(pilotToUnregister, true); if (!validationResult.IsValid) { return(Error <bool>(false, $"The {nameof(pilotToUnregister)} parameter is not valid.")); } try { var result = await this.pilotRegistrationRepository.DeleteAsync(pilotToUnregister.Id); if (result.IsFaulted || !result.Value) { return(Error <bool>(false, $"An error occured while unregistering a pilot.")); } return(Success(result.Value, nameof(RegisterPilotsForContestAsync))); } catch (Exception ex) { return(Error <bool>(false, ex)); } }
public async Task PilotRegistrationStorageCmdInteractor_RegisterSinglePilotForContestAsync_BadRegistrationParameter() { PilotRegistration pilotRegistration = GenerateValidPilotRegistration(1).First(); pilotRegistration.ContestId = string.Empty; mockPilotRegistrationRepository.Setup(prr => prr.CreateAsync(It.IsAny <PilotRegistration>())).Returns(Task.FromResult(new Result <PilotRegistration>(pilotRegistration))); var prci = new PilotRegistrationStorageCmdInteractor(mockPilotRegistrationRepository.Object, mockLogger.Object); var result = await prci.RegisterSinglePilotForContestAsync(pilotRegistration); // Invalid Contest Id should fail Assert.IsTrue(result.IsFaulted); pilotRegistration.ContestId = null; mockPilotRegistrationRepository.Setup(prr => prr.CreateAsync(It.IsAny <PilotRegistration>())).Returns(Task.FromResult(new Result <PilotRegistration>(pilotRegistration))); prci = new PilotRegistrationStorageCmdInteractor(mockPilotRegistrationRepository.Object, mockLogger.Object); result = await prci.RegisterSinglePilotForContestAsync(pilotRegistration); // Null Contest Id should fail Assert.IsTrue(result.IsFaulted); pilotRegistration.ContestId = "1"; pilotRegistration.AirframesSignedOff = false; mockPilotRegistrationRepository.Setup(prr => prr.CreateAsync(It.IsAny <PilotRegistration>())).Returns(Task.FromResult(new Result <PilotRegistration>(pilotRegistration))); result = await prci.RegisterSinglePilotForContestAsync(pilotRegistration); // It should be ok to register a pilot w/ a failed airframe sign-off Assert.IsFalse(result.IsFaulted); pilotRegistration.AirframesSignedOff = true; pilotRegistration.AirframeRegistrationNumbers = null; mockPilotRegistrationRepository.Setup(prr => prr.CreateAsync(It.IsAny <PilotRegistration>())).Returns(Task.FromResult(new Result <PilotRegistration>(pilotRegistration))); result = await prci.RegisterSinglePilotForContestAsync(pilotRegistration); // It should be ok to not have any airframes registered Assert.IsFalse(result.IsFaulted); pilotRegistration.AirframeRegistrationNumbers = new List <string> { "1", "2" }; pilotRegistration.IsPaid = false; mockPilotRegistrationRepository.Setup(prr => prr.CreateAsync(It.IsAny <PilotRegistration>())).Returns(Task.FromResult(new Result <PilotRegistration>(pilotRegistration))); result = await prci.RegisterSinglePilotForContestAsync(pilotRegistration); // It should be ok to have a pilot register that is not paid Assert.IsFalse(result.IsFaulted); pilotRegistration.PilotId = string.Empty; mockPilotRegistrationRepository.Setup(prr => prr.CreateAsync(It.IsAny <PilotRegistration>())).Returns(Task.FromResult(new Result <PilotRegistration>(pilotRegistration))); result = await prci.RegisterSinglePilotForContestAsync(pilotRegistration); // Empty Pilot ID should fail. Assert.IsTrue(result.IsFaulted); pilotRegistration.PilotId = null; mockPilotRegistrationRepository.Setup(prr => prr.CreateAsync(It.IsAny <PilotRegistration>())).Returns(Task.FromResult(new Result <PilotRegistration>(pilotRegistration))); result = await prci.RegisterSinglePilotForContestAsync(pilotRegistration); // Null Pilot ID should fail. Assert.IsTrue(result.IsFaulted); }
public async Task PilotRegistrationStorageCmdInteractor_RegisterSinglePilotForContestAsync_RepositoryException() { PilotRegistration pilotRegistration = GenerateValidPilotRegistration(1).First(); mockPilotRegistrationRepository.Setup(prr => prr.CreateAsync(It.IsAny <PilotRegistration>())).ThrowsAsync(new Exception()); var prci = new PilotRegistrationStorageCmdInteractor(mockPilotRegistrationRepository.Object, mockLogger.Object); var result = await prci.RegisterSinglePilotForContestAsync(pilotRegistration); Assert.IsTrue(result.IsFaulted); }
/// <summary> /// Validates the pilot registration. /// </summary> /// <param name="pilotRegistration">The pilot registration.</param> /// <returns></returns> public static ValidationResult ValidatePilotRegistration(PilotRegistration pilotRegistration) { if (pilotRegistration == null) { return(new ValidationResult(false, "The pilot registration submitted cannot be null")); } if (string.IsNullOrEmpty(pilotRegistration.PilotId)) { return(new ValidationResult(false, "The pilot registration's pilot id submitted cannot be null or empty")); } if (string.IsNullOrEmpty(pilotRegistration.ContestId)) { return(new ValidationResult(false, "The pilot registration's contest id submitted cannot be null or empty")); } return(new ValidationResult(true)); }
/// <summary> /// Validates the pilot registration. /// </summary> /// <param name="pilotRegistration">The pilot registration.</param> /// <returns></returns> private static ValidationResult ValidatePilotRegistration(PilotRegistration pilotRegistration, bool isUnregister = false) { if (pilotRegistration == null) { return(new ValidationResult(false, "Pilot registration cannot be null.")); } if (string.IsNullOrEmpty(pilotRegistration.PilotId)) { return(new ValidationResult(false, "Pilot Id cannot be null or empty.")); } if (string.IsNullOrEmpty(pilotRegistration.ContestId)) { return(new ValidationResult(false, "Contest Id cannot be null or empty.")); } return(new ValidationResult(true)); }
/// <summary> /// Saves the and continue. /// </summary> /// <returns></returns> public async Task SaveAndContinue() { // This might be a new pilot registration, handle it. if (isNewPilot) { var pilotToAdd = new Pilot(this.FirstName, this.LastName, string.Empty, this.AmaNumber, this.Airframe); try { // Create the pilot var createPilotResult = await this.pilotCmdInteractor.Value.CreateNewPilotAsync(pilotToAdd); if (createPilotResult.IsFaulted) { base.Alert($"{nameof(EditPilotRegistrationPageViewModel)}:{nameof(SaveAndContinue)} - Failed to add new pilot."); return; } // Update the contest to make sure the pilot roster is update w/ the new pilot... this.contest.PilotRoster.Add(createPilotResult.Value.Id); // Be sure to add the new pilot id back into the viewmodel pilotId = createPilotResult.Value.Id; } catch (Exception ex) { App.Logger.LogException(ex); base.Alert($"An error occured creating a new pilot for the registration."); return; } var updateContestResult = await this.contestCmdIntr.Value.UpdateContestAsync(contest); if (updateContestResult.IsFaulted) { base.Alert($"{nameof(EditPilotRegistrationPageViewModel)}:{nameof(SaveAndContinue)} - Failed to update the contest."); return; } } else { // Save any changes to the pilot object var storedPilotsResult = await pilotQueryInteractor.Value.GetAllPilotsAsync(); if (storedPilotsResult.IsFaulted) { base.Alert($"{nameof(EditPilotRegistrationPageViewModel)}:{nameof(SaveAndContinue)} - Failed to save changes to the pilot."); return; } var storedPilotToUpdate = storedPilotsResult.Value.Where(p => p.Id == pilotId).FirstOrDefault(); if (storedPilotToUpdate == null) { return; } //TODO: Throw an error message here... storedPilotToUpdate.FirstName = this.FirstName; storedPilotToUpdate.LastName = this.LastName; storedPilotToUpdate.StandardsBodyId = this.AmaNumber; storedPilotToUpdate.Airframe = this.Airframe; var updatePilotResult = await pilotCmdInteractor.Value.UpdatePilotAsync(storedPilotToUpdate); if (updatePilotResult.IsFaulted) { base.Alert($"{nameof(EditPilotRegistrationPageViewModel)}:{nameof(SaveAndContinue)} - Failed to update the pilot."); return; } } // Save the registration var registrationToSave = new PilotRegistration { ContestId = this.contest.Id, Id = registrationId, IsPaid = IsPaid, AirframesSignedOff = AirframesSignedOff, PilotId = pilotId }; try { var registerPilotResult = await registrationCmdInteractor.Value.RegisterSinglePilotForContestAsync(registrationToSave); if (registerPilotResult.IsFaulted) { base.Alert($"{nameof(EditPilotRegistrationPageViewModel)}:{nameof(SaveAndContinue)} - Failed to save the registration."); return; } } catch { base.Alert("An error occured creating a new pilot registration."); } this.NavigationService.Navigate(typeof(PilotRegistrationPage), this.contest); }
/// <summary> /// Registers the single pilot for contest. /// </summary> /// <param name="pilotToRegister">The pilot registration.</param> /// <returns></returns> public async Task <Result <PilotRegistration> > RegisterSinglePilotForContestAsync(PilotRegistration pilotToRegister) { var validationResult = ValidatePilotRegistration(pilotToRegister); if (!validationResult.IsValid) { return(Error <PilotRegistration>(null, validationResult.Message)); } try { var result = await this.pilotRegistrationRepository.CreateAsync(pilotToRegister); if (result.IsFaulted || result.Value == null) { return(Error <PilotRegistration>(null, $"An error occured while registering pilots.")); } return(Success(result.Value, nameof(RegisterPilotsForContestAsync))); } catch (Exception ex) { return(Error <PilotRegistration>(null, ex)); } }