public async Task If_school_agreed_initially__result_has_HasAgreedInitially() { Guid schoolId; var sp = new ServiceProviderBuilder().BuildServiceProvider(ConfigureServices); using (var scope = sp.CreateScope()) { var engine = scope.ServiceProvider.GetRequiredService <ISzlemEngine>(); var command = new RegisterSchool.Command() { Name = "I Liceum Ogólnokształcące", City = "Gdańsk", Address = "Wały Piastowskie 6", ContactData = new[] { new ContactData() { Name = "sekretariat", EmailAddress = EmailAddress.Parse("*****@*****.**"), PhoneNumber = PhoneNumber.Parse("58 301-67-34") }, } }; var result = await engine.Execute(command); result.IsSuccess.Should().BeTrue(); schoolId = result.Value; } using (var scope = sp.CreateScope()) { var engine = scope.ServiceProvider.GetRequiredService <ISzlemEngine>(); var command = new RecordInitialAgreement.Command() { SchoolId = schoolId, AgreeingPersonName = "Andrzej Strzelba" }; var result = await engine.Execute(command); result.IsSuccess.Should().BeTrue(); } using (var scope = sp.CreateScope()) { var engine = scope.ServiceProvider.GetRequiredService <ISzlemEngine>(); var result = await engine.Query(new GetDetails.Query() { SchoolId = schoolId }); result.IsSuccess.Should().BeTrue(); var school = result.Value; school.Should().BeEquivalentTo(new GetDetails.SchoolDetails() { Id = schoolId, Name = "I Liceum Ogólnokształcące", Address = "Wały Piastowskie 6", City = "Gdańsk", HasAgreedInitially = true }, options => options.Excluding(x => x.Events).Excluding(x => x.ContactData)); } }
public async Task <Result <Nothing, Error> > Handle(RecordInitialAgreement.Command request, CancellationToken cancellationToken) { var user = await _userAccessor.GetUser(); var result = await _aggregateStore.Update <SchoolAggregate, SchoolId, Result <Nothing, Error> >( SchoolId.With(request.SchoolId), CommandId.New, (aggregate) => aggregate.RecordInitialAgreement(_clock.GetCurrentInstant(), request, user), cancellationToken); return(result.Unwrap()); }
public void Command_must_be_executed_on_aggregate_with_matching_SchoolId() { var school = new SchoolAggregate(SchoolId.New); var recordingUser = new ApplicationUser(); var command = new RecordInitialAgreement.Command() { SchoolId = Guid.NewGuid() }; Assert.Throws <AggregateMismatchException>(() => school.RecordInitialAgreement(NodaTime.SystemClock.Instance.GetCurrentInstant(), command, recordingUser)); }
public void After_registering_initial_agreement__aggregate_contains_InitialAgreementAchieved_event() { var school = new SchoolAggregate(SchoolId.New); var recordingUser = new ApplicationUser() { Id = Guid.NewGuid() }; school.RegisterSchool( NodaTime.SystemClock.Instance.GetCurrentInstant() - NodaTime.Duration.FromDays(2), new RegisterSchool.Command() { Name = "I Liceum Ogólnokształcące", City = "Gdańsk", Address = "Wały Piastowskie 6", ContactData = new[] { new ContactData() { Name = "sekretariat", EmailAddress = EmailAddress.Parse("*****@*****.**"), PhoneNumber = PhoneNumber.Parse("58 301-67-34") }, } }, new ApplicationUser() { Id = Guid.NewGuid() }); var command = new RecordInitialAgreement.Command() { SchoolId = school.Id.GetGuid(), AgreeingPersonName = "Andrzej Strzelba", AdditionalNotes = "test" }; var result = school.RecordInitialAgreement(NodaTime.SystemClock.Instance.GetCurrentInstant(), command, recordingUser); result.IsSuccess.Should().BeTrue(); school.HasAgreedInitially.Should().BeTrue(); var uncommittedEvent = Assert.Single(school.UncommittedEvents, @event => @event.AggregateEvent is InitialAgreementAchieved); var @event = Assert.IsType <InitialAgreementAchieved>(uncommittedEvent.AggregateEvent); @event.Should().BeEquivalentTo(new InitialAgreementAchieved(recordingUser.Id, "Andrzej Strzelba", "test")); }
public void SchoolId_must_point_to_existing_school() { var school = new SchoolAggregate(SchoolId.New); var recordingUser = new ApplicationUser(); var command = new RecordInitialAgreement.Command() { SchoolId = school.Id.GetGuid(), AgreeingPersonName = "Andrzej Strzelba" }; var result = school.RecordInitialAgreement(NodaTime.SystemClock.Instance.GetCurrentInstant(), command, recordingUser); Assert.False(result.IsSuccess); var error = Assert.IsType <Error.ResourceNotFound>(result.Error); Assert.Equal(Messages.School_not_found, error.Message); }
public Result <Nothing, Error> RecordInitialAgreement(Instant now, RecordInitialAgreement.Command command, ApplicationUser recordingUser) { Guard.Against.Null(command, nameof(command)); Guard.Against.Null(recordingUser, nameof(recordingUser)); ValidateIdMatchOrThrow(command.SchoolId); return(Validate(new RecordInitialAgreement.Validator(), command) .Ensure( _ => this.IsNew == false, new Error.ResourceNotFound(Messages.School_not_found)) .Tap( _ => Emit(new InitialAgreementAchieved( recordingUserId: recordingUser.Id, agreeingPersonName: command.AgreeingPersonName, additionalNotes: command.AdditionalNotes)) )); }
public async Task <IActionResult> RecordInitialAgreement(RecordInitialAgreement.Command command) { var result = await _engine.Execute(command); return(result.MatchToActionResult(success => Ok())); }