private static Result <User> BuildUser(UserRegistrationDTO userRegistrationDTO, byte[] randomByteArray) { var hashingServiceMock = new Mock <IHashingService>(); var ageResult = Age.Create(userRegistrationDTO.DateOfBirth); var lookingForGenderResult = Gender.Create(userRegistrationDTO.LookingFor); var matchPreference = UserMatchPreference.Create(lookingForGenderResult.Value, userRegistrationDTO.MinAge, userRegistrationDTO.MaxAge, userRegistrationDTO.Distance); var phoneNumberResult = UserPhoneNumber.Create(userRegistrationDTO.PhoneNumber, userRegistrationDTO.CountryCode); var genderResult = Gender.Create(userRegistrationDTO.Gender); var loctionResult = Location.Create(userRegistrationDTO.Latitude, userRegistrationDTO.Longitude); hashingServiceMock.Setup(x => x.GenerateSalt()).Returns(randomByteArray); hashingServiceMock.Setup(x => x.GetHash(userRegistrationDTO.Password, It.IsAny <byte[]>())).Returns(randomByteArray); var passwordSalt = hashingServiceMock.Object.GenerateSalt(); var passwordHash = hashingServiceMock.Object.GetHash(userRegistrationDTO.Password, passwordSalt); var passwordResult = Password.Create(passwordHash, passwordSalt); var result = UserBuilder.Builder() .WithName(userRegistrationDTO.FullName) .WithPhoneNumber(phoneNumberResult.Value) .WithPassword(passwordResult.Value) .WithGender(genderResult.Value) .WithMatchPreference(matchPreference.Value) .WithAge(ageResult.Value) .WithBucketList(userRegistrationDTO.BucketList) .WithFunAndInterestingThings(userRegistrationDTO.FunAndInteresting) .WithSchool(userRegistrationDTO.School) .WithProfession(userRegistrationDTO.Profession) .WithLocation(loctionResult.Value) .Build(); return(result); }
public async Task Create(string firstName, string lastName, int age, int suffixId) { var firstNameResult = Name.Create(firstName); var lastNameResult = Name.Create(lastName); var ageResult = Age.Create(age); Maybe <Suffix> maybeSuffix = Suffix.FromId(suffixId); await Result .Combine(firstNameResult, lastNameResult, ageResult) .Bind(() => PersonName.Create(firstNameResult.Value, lastNameResult.Value, maybeSuffix.Value)) .Bind(personName => Person.Create(personName, ageResult.Value)) .Tap(person => _repository.Save(person)) .Tap(() => _unitOfWork.CommitAsync()) .Tap(() => _logger.LogInformation($"Person was stored to db: {firstName} {lastName}")) .OnFailure(error => _logger.LogError(error)); /* * var firstNameResult = Name.Create(firstName); * var lastNameResult = Name.Create(lastName); * var ageResult = Age.Create(age); * var personName = PersonName.Create(firstNameResult.Value, lastNameResult.Value); * var personResult = Person.Create(personName.Value, ageResult.Value); * * _repository.Save(personResult.Value); * await _unitOfWork.CommitAsync(); */ }
public void Create_Should_Return_Failed_AgeResult_If_Age_Is_Not_18_From_Todays_Date() { var todaysDateOfbirth = DateTime.Now.AddYears(-18).AddDays(1); var result = Age.Create(todaysDateOfbirth); result.IsSuccessed.Should().BeFalse(); result.Value.Should().BeNull(); }
public void Create_Should_Return_Success_AgeResult_If_Age_Is_18_From_Todays_Date() { var todaysDateOfbirth = DateTime.Now.AddYears(-18); var result = Age.Create(todaysDateOfbirth); result.IsSuccessed.Should().BeTrue(); result.Value.CurrentAge.Should().BeGreaterOrEqualTo(18); }
public void Create_Should_Return_Failed_AgeResult_If_DateOfBirth_Is_InValid() { var dateOfBirth = DateTime.Now.AddDays(-1); var result = Age.Create(dateOfBirth); result.IsSuccessed.Should().BeFalse(); result.Value.Should().BeNull(); }
public void Create_Should_Return_Success_AgeResult_If_DateOfBirth_Is_Valid(int year, int month, int day) { var dateOfBirth = new DateTime(year, month, day); var result = Age.Create(dateOfBirth); result.IsSuccessed.Should().BeTrue(); result.Value.CurrentAge.Should().BeGreaterOrEqualTo(18); }
public async Task <Result <User> > CreateUserAsync(UserRegistrationDTO userRegistrationDTO) { if (string.IsNullOrWhiteSpace(userRegistrationDTO.Password)) { return(Result.Fail <User>(Password_Is_Required)); } var ageResult = Age.Create(userRegistrationDTO.DateOfBirth); var lookingForGenderResult = Gender.Create(userRegistrationDTO.LookingFor); var matchPreference = UserMatchPreference.Create(lookingForGenderResult.Value, userRegistrationDTO.MinAge, userRegistrationDTO.MaxAge, userRegistrationDTO.Distance); var phoneNumberResult = UserPhoneNumber.Create(userRegistrationDTO.PhoneNumber, userRegistrationDTO.CountryCode); var genderResult = Gender.Create(userRegistrationDTO.Gender); var passwordSalt = _hashingService.GenerateSalt(); var passwordHash = _hashingService.GetHash(userRegistrationDTO.Password, passwordSalt); var passwordResult = Password.Create(passwordHash, passwordSalt); var locationResult = Location.Create(userRegistrationDTO.Latitude, userRegistrationDTO.Longitude); var finalResult = Result.Combine(ageResult, lookingForGenderResult, matchPreference, phoneNumberResult, genderResult, passwordResult, locationResult); if (!finalResult.IsSuccessed) { return(Result.Fail <User>(finalResult.GetErrorString())); } var userResult = UserBuilder.Builder() .WithName(userRegistrationDTO.FullName) .WithPhoneNumber(phoneNumberResult.Value) .WithPassword(passwordResult.Value) .WithGender(genderResult.Value) .WithMatchPreference(matchPreference.Value) .WithAge(ageResult.Value) .WithBucketList(userRegistrationDTO.BucketList) .WithFunAndInterestingThings(userRegistrationDTO.FunAndInteresting) .WithSchool(userRegistrationDTO.School) .WithProfession(userRegistrationDTO.Profession) .WithLocation(locationResult.Value) .Build(); var setUserInterestsResult = await _userDomainService.SetUserInterests(userResult.Value, userRegistrationDTO.InterestIds); if (!setUserInterestsResult.IsSuccessed) { return(Result.Fail <User>(setUserInterestsResult.GetErrorString())); } return(userResult); }
public Option <Student> AddNewStudent(string firstName, string lastName, int age, string gender) { var optionName = Name.Create(firstName, lastName); var optionAge = Age.Create(age); var optionGender = Gender.Create(gender); AddStudentApply <Name, Age, Gender, Option <Student> > method = Add; return(method.Curry() .Apply(optionName) .Apply(optionAge) .Apply(optionGender)); }
public async Task UpdateAge(int personId, int personAge) { await _repository.GetByIdAsync(personId) .ToResult($"Person was not found for ID: {personId}") .Bind(person => Age .Create(personAge) .Tap(age => person.UpdateAge(age))) .Tap(() => _unitOfWork.CommitAsync()) .Tap(() => _logger.LogInformation($"Person age updated to {personAge}")) .OnFailure(error => _logger.LogError(error)); /* * var maybePerson = await _repository.GetById(personId); * var ageResult = Age.Create(personAge); * maybePerson.Value.UpdateAge(ageResult.Value); * * await _unitOfWork.CommitAsync(); */ }
public async Task <RetirementReportDto> RetirementReportForAsync(RetirementReportRequestDto requestDto) { var emergencyFundSpec = new EmergencyFundSpec(requestDto.EmergencyFund); var personList = requestDto.Persons.ToList(); if (personList.Count == 2) { emergencyFundSpec = emergencyFundSpec.SplitInTwo(); } var person = personList.Select(p => PersonStatus(p, emergencyFundSpec)); var spendingStepInputs = new List <SpendingStep> { new SpendingStep(DateTime.Now.Date, Money.Create(requestDto.Spending)) }; spendingStepInputs.AddRange(requestDto.SpendingSteps.Select(dto => new SpendingStep(dto.Date ?? person.First().Dob.AddYears(Convert.ToInt32(dto.Age)), Money.Create(dto.Amount)))); var retirementReport = await _retirementCalculator.ReportForTargetAgeAsync(person, spendingStepInputs, Age.Create(requestDto.TargetRetirementAge)); return(new RetirementReportDto(retirementReport)); }