Exemplo n.º 1
0
 public static void NumberOfParallelMaintenanceJobsMustNotExceedAvailableWorkStations(
     this WorkshopCalendarAggregateRoot calendar, PlanMaintenanceJob command)
 {
     if (calendar.Jobs?.Count(j => j.PlannedTimeslot.OverlapsWith(command.StartTime, command.EndTime)) >= AVAILABLE_WORKSTATIONS)
     {
         throw new BusinessRuleViolationException($"Maintenancejob overlaps with more than {AVAILABLE_WORKSTATIONS} other jobs.");
     }
 }
Exemplo n.º 2
0
 public static void NumberOfParallelMaintenanceJobsOnAVehicleMustNotExceedOne(
     this WorkshopCalendarAggregateRoot calendar, PlanMaintenanceJob command)
 {
     if (calendar.Jobs != null && calendar.Jobs.Any(j => j.Vehicle.Id == command.VehicleInfo.LicenseNumber &&
                                                    j.PlannedTimeslot.OverlapsWith(command.StartTime, command.EndTime)))
     {
         throw new BusinessRuleViolationException($"Only 1 maintenance job can be executed on a vehicle during a certain time-slot.");
     }
 }
        public async Task <bool> HandleCommandAsync(DateTime calendarDate, PlanMaintenanceJob command)
        {
            bool isJobPlannedSuccessfully = false;

            // get or create workshop-Calendar
            //(WorkshopCalendar existingCalendar, IEnumerable<WorkshopCalendarEvent> existingEvents)
            var(existingCalendar, existingEvents) = await _calendarRepo.GetWorkshopCalendarAsync(calendarDate);

            WorkshopCalendarAggregateRoot workshopCalendar;

            if (existingCalendar == null)
            {
                workshopCalendar = WorkshopCalendarAggregateRoot.Create(calendarDate);
            }
            else
            {
                //TODO: Map existingEvents to the root event so we can check the overlapping
                //var activeEvents = existingEvents?.Where(e => e.ActualEndDateTime != null)?.ToList();
                //if (activeEvents.Any())
                //{
                //    events.Concat(activeEvents);
                //}
                workshopCalendar = new WorkshopCalendarAggregateRoot(calendarDate, new List <Event>());
            }

            // handle command
            //TODO while planning for a new maintenance job, system should create new GUID for the new JOB
            workshopCalendar.PlanMaintenanceJob(command);

            IEnumerable <Event> events = workshopCalendar.GetEvents();

            // persist
            //TODO we shouldn't use events while save to database events should used only between context
            //we should use Aggregate root instance values while validated by business rules
            isJobPlannedSuccessfully = await _calendarRepo.SaveWorkshopCalendarAsync(workshopCalendar.Id, workshopCalendar.OriginalVersion, workshopCalendar.Version, events);

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

            // return result
            return(isJobPlannedSuccessfully);
        }
        public async Task <bool> HandleCommandAsync(DateTime calendarDate, FinishMaintenanceJob command)
        {
            bool isJobFinishedSuccessfully = false;

            // get or create workshop-Calendar
            var(existingCalendar, existingEvents) = await _calendarRepo.GetWorkshopCalendarAsync(calendarDate);

            WorkshopCalendarAggregateRoot workshopCalendar;

            if (existingCalendar == null)
            {
                workshopCalendar = WorkshopCalendarAggregateRoot.Create(calendarDate);
            }
            else
            {
                //TODO: Map existingEvents to the root event so we can check the overlapping
                workshopCalendar = new WorkshopCalendarAggregateRoot(calendarDate);
            }

            // handle command
            workshopCalendar.FinishMaintenanceJob(command);

            // persist
            //TODO we shouldn't use events while save to database events should used only between context
            //we should use Aggregate root instance values while validated by business rules
            IEnumerable <Event> events = workshopCalendar.GetEvents();

            isJobFinishedSuccessfully = await _calendarRepo.SaveWorkshopCalendarAsync(workshopCalendar.Id, workshopCalendar.OriginalVersion, workshopCalendar.Version, events);

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

            // return result
            return(isJobFinishedSuccessfully);
        }