public async Task <WorkshopPlanning> HandleCommandAsync(DateTime planningDate, FinishMaintenanceJob command)
        {
            // get planning
            WorkshopPlanning planning = await _planningRepo.GetWorkshopPlanningAsync(planningDate);

            if (planning == null)
            {
                return(null);
            }

            // handle command
            IEnumerable <Event> events = planning.FinishMaintenanceJob(command);

            // persist
            await _planningRepo.SaveWorkshopPlanningAsync(planning, events);

            // publish event
            foreach (var e in events)
            {
                await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");
            }

            // return result
            return(planning);
        }
Пример #2
0
        public void Finish_MaintenanceJob_Should_Finish_A_New_MaintenanceJob()
        {
            // arrange
            DateTime date = DateTime.Today;
            string   workshopPlanningId = date.ToString("yyyy-MM-dd");
            Guid     jobId              = Guid.NewGuid();
            DateTime startTime          = date.AddHours(8);
            DateTime endTime            = date.AddHours(11);
            DateTime actualStartTime    = date.AddHours(9);
            DateTime actualEndTime      = date.AddHours(12);
            var      initializingEvents = new Event[] {
                new WorkshopPlanningCreatedEventBuilder()
                .WithDate(date)
                .Build(),
                new MaintenanceJobPlannedEventBuilder()
                .WithStartTime(startTime)
                .WithEndTime(endTime)
                .WithJobId(jobId)
                .Build()
            };
            WorkshopPlanning sut = new WorkshopPlanning(date, initializingEvents);

            FinishMaintenanceJob command = new FinishMaintenanceJobCommandBuilder()
                                           .WithJobId(jobId)
                                           .WithActualStartTime(actualStartTime)
                                           .WithActualEndTime(actualEndTime)
                                           .Build();

            // act
            sut.FinishMaintenanceJob(command);
            IEnumerable <Event> events = sut.GetEvents();

            // assert
            Assert.NotNull(sut);
            Assert.NotNull(sut.Id);
            Assert.Equal(workshopPlanningId, sut.Id);
            Assert.Equal(2, sut.OriginalVersion);
            Assert.Equal(3, sut.Version);
            Assert.Collection(sut.Jobs,
                              item0 =>
            {
                Assert.Equal(command.JobId, item0.Id);
                Assert.Equal(startTime, item0.StartTime);
                Assert.Equal(endTime, item0.EndTime);
                Assert.Equal(command.StartTime, item0.ActualStartTime);
                Assert.Equal(command.Notes, item0.Notes);
            }
                              );
            Assert.Collection(events, item0 => Assert.IsAssignableFrom <MaintenanceJobFinished>(item0));
        }
Пример #3
0
        public void Finish_An_Already_Finished_MaintenanceJob_Should_Throw_Exception()
        {
            // arrange
            DateTime date               = DateTime.Today;
            Guid     jobId              = Guid.NewGuid();
            DateTime startTime          = date.AddHours(8);
            DateTime endTime            = date.AddHours(11);
            DateTime actualStartTime    = date.AddHours(9);
            DateTime actualEndTime      = date.AddHours(12);
            string   notes              = "Ok";
            var      initializingEvents = new Event[] {
                new WorkshopPlanningCreatedEventBuilder()
                .WithDate(date)
                .Build(),
                new MaintenanceJobPlannedEventBuilder()
                .WithStartTime(startTime)
                .WithEndTime(endTime)
                .WithJobId(jobId)
                .Build(),
                new MaintenanceJobFinishedEventBuilder()
                .WithJobId(jobId)
                .WithActualStartTime(actualStartTime)
                .WithActualEndTime(actualEndTime)
                .WithNotes(notes)
                .Build()
            };
            WorkshopPlanning sut = new WorkshopPlanning(initializingEvents);

            FinishMaintenanceJob command = new FinishMaintenanceJobCommandBuilder()
                                           .WithJobId(jobId)
                                           .WithActualStartTime(actualStartTime)
                                           .WithActualEndTime(actualEndTime)
                                           .WithNotes(notes)
                                           .Build();

            // act
            var thrownException =
                Assert.Throws <BusinessRuleViolationException>(() =>
                                                               sut.FinishMaintenanceJob(command));

            // assert
            Assert.Equal("An already finished job can not be finished.",
                         thrownException.Message);
        }
Пример #4
0
        public void Finish_MaintenanceJob_Should_Finish_A_New_MaintenanceJob()
        {
            // arrange
            DateTime date               = DateTime.Today;
            Guid     jobId              = Guid.NewGuid();
            DateTime startTime          = date.AddHours(8);
            DateTime endTime            = date.AddHours(11);
            DateTime actualStartTime    = date.AddHours(9);
            DateTime actualEndTime      = date.AddHours(12);
            var      customerInfo       = ("customer id", "customer name", "123456780");
            var      vehicleInfo        = ("AB-123-C", "Volkswagen", "Tiguan");
            string   description        = "Job description";
            var      initializingEvents = new Event[] {
                new WorkshopPlanningCreated(Guid.NewGuid(), date),
                new MaintenanceJobPlanned(Guid.NewGuid(), jobId, startTime, endTime,
                                          customerInfo, vehicleInfo, description)
            };
            WorkshopPlanning sut = new WorkshopPlanning(initializingEvents);

            FinishMaintenanceJob command = new FinishMaintenanceJob(Guid.NewGuid(), jobId,
                                                                    date.AddHours(9), date.AddHours(12), "Notes");

            // act
            List <Event> events = new List <Event>(sut.FinishMaintenanceJob(command));

            // assert
            Assert.NotNull(sut);
            Assert.NotNull(sut.Id);
            Assert.Equal(date, sut.Date);
            Assert.Equal(2, sut.OriginalVersion);
            Assert.Equal(3, sut.Version);
            Assert.Collection(sut.Jobs,
                              item0 => {
                Assert.Equal(command.JobId, item0.Id);
                Assert.Equal(startTime, item0.StartTime);
                Assert.Equal(endTime, item0.EndTime);
                Assert.Equal(command.StartTime, item0.ActualStartTime);
                Assert.Equal(command.Notes, item0.Notes);
            }
                              );
            Assert.Collection(events, item0 => Assert.IsAssignableFrom <MaintenanceJobFinished>(item0));
        }
        public async Task <IActionResult> FinishMaintenanceJobAsync(DateTime date, Guid jobId, [FromBody] FinishMaintenanceJob command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // get planning
                    WorkshopPlanning planning = await _planningRepo.GetWorkshopPlanningAsync(date);

                    if (planning == null)
                    {
                        return(NotFound());
                    }

                    // handle command
                    IEnumerable <Event> events = planning.FinishMaintenanceJob(command);

                    // persist
                    await _planningRepo.SaveWorkshopPlanningAsync(planning, events);

                    // publish event
                    foreach (var e in events)
                    {
                        await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");
                    }

                    // return result
                    return(Ok());
                }
                return(BadRequest());
            }
            catch (ConcurrencyException)
            {
                //Log the error (uncomment ex variable name and write a log.
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }