public async Task <IActionResult> EditMaintenanceJob([FromForm] WorkshopManagementEditViewModel inputModel)
        {
            if (ModelState.IsValid)
            {
                return(await _resiliencyHelper.ExecuteResilient(async() =>
                {
                    string dateTimeString = inputModel.Date.ToString("yyyy-MM-dd");

                    try
                    {
                        // update maintenance job
                        var startTime = inputModel.Date.Add(inputModel.StartTime.TimeOfDay);
                        var endTime = inputModel.Date.Add(inputModel.EndTime.TimeOfDay);
                        var vehicle = await _workshopManagementAPI.GetVehicleByLicenseNumber(inputModel.SelectedVehicleLicenseNumber);
                        var customer = await _workshopManagementAPI.GetCustomerById(vehicle.OwnerId);

                        var updateMaintenanceJobCommand = new UpdateMaintenanceJob(Guid.NewGuid(),
                                                                                   inputModel.Id,
                                                                                   startTime,
                                                                                   endTime,
                                                                                   (customer.CustomerId, customer.Name, customer.TelephoneNumber),
                                                                                   (vehicle.LicenseNumber, vehicle.Brand, vehicle.Type),
                                                                                   inputModel.Description);

                        await _workshopManagementAPI.UpdateMaintenanceJob(
                            dateTimeString,
                            updateMaintenanceJobCommand.JobId.ToString(),
                            updateMaintenanceJobCommand);
                    }
                    catch (ApiException ex)
                    {
                        if (ex.StatusCode == HttpStatusCode.Conflict)
                        {
                            // add errormessage from API exception to model
                            var content = await ex.GetContentAsAsync <BusinessRuleViolation>();
                            inputModel.Error = content.ErrorMessage;

                            // repopulate list of available vehicles in the model
                            inputModel.Vehicles = await GetAvailableVehiclesList();

                            // back to New view
                            return View("Edit", inputModel);
                        }
                    }

                    return RedirectToAction("Index", new { planningDate = dateTimeString });
                }, View("Offline", new WorkshopManagementOfflineViewModel())));
            }
            else
            {
                inputModel.Vehicles = await GetAvailableVehiclesList();

                return(View("Edit", inputModel));
            }
        }
 public static void UpdatedMaintenanceJobShouldFallWithinOneBusinessDay(
     this WorkshopPlanning planning, UpdateMaintenanceJob command)
 {
     if (command.StartTime.Date != command.EndTime.Date)
     {
         throw new BusinessRuleViolationException("Start-time and end-time of a Maintenance Job must be within a 1 day.");
     }
 }
Esempio n. 3
0
 public static MaintenanceJobUpdated MapToMaintenanceJobUpdated(this UpdateMaintenanceJob source) => new MaintenanceJobUpdated(
     Guid.NewGuid(),
     source.JobId,
     source.StartTime,
     source.EndTime,
     source.CustomerInfo,
     source.VehicleInfo,
     source.Description
     );
 public static void NumberOfParallelMaintenanceJobsOnAVehicleIsOne(
     this WorkshopPlanning planning, UpdateMaintenanceJob command)
 {
     if (planning.Jobs.Any(j => j.Id != command.JobId &&
                           j.Vehicle.Id == command.VehicleInfo.LicenseNumber &&
                           (j.StartTime >= command.StartTime && j.StartTime <= command.EndTime ||
                            j.EndTime >= command.StartTime && j.EndTime <= command.EndTime)))
     {
         throw new BusinessRuleViolationException($"Only 1 maintenance job can be executed on a vehicle during a certain time-slot.");
     }
 }
 public static void NumberOfParallelMaintenanceJobsMustNotExceedAvailableWorkStations(
     this WorkshopPlanning planning, UpdateMaintenanceJob command)
 {
     if (planning.Jobs.Count(j =>
                             (j.Id != command.JobId) &&
                             ((j.StartTime >= command.StartTime && j.StartTime <= command.EndTime) ||
                              (j.EndTime >= command.StartTime && j.EndTime <= command.EndTime))) >= AVAILABLE_WORKSTATIONS)
     {
         throw new BusinessRuleViolationException($"Maintenancejob overlaps with more than {AVAILABLE_WORKSTATIONS} other jobs.");
     }
 }
Esempio n. 6
0
        public void UpdateMaintenanceJob(UpdateMaintenanceJob command)
        {
            // check business rules
            this.UpdatedMaintenanceJobShouldFallWithinOneBusinessDay(command);
            this.NumberOfParallelMaintenanceJobsMustNotExceedAvailableWorkStations(command);
            this.NumberOfParallelMaintenanceJobsOnAVehicleIsOne(command);

            // handle event
            MaintenanceJobUpdated e = command.MapToMaintenanceJobUpdated();

            RaiseEvent(e);
        }
Esempio n. 7
0
        public async Task <WorkshopPlanning> HandleCommandAsync(DateTime planningDate, UpdateMaintenanceJob command)
        {
            // get or create workshop-planning
            WorkshopPlanning planning = await planningRepository.GetWorkshopPlanningAsync(planningDate);

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

            // handle command
            planning.UpdateMaintenanceJob(command);

            // persist
            IEnumerable <Event> events = planning.GetEvents();
            await planningRepository.SaveWorkshopPlanningAsync(
                planning.Id, planning.OriginalVersion, planning.Version, events);

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

            // return result
            return(planning);
        }
Esempio n. 8
0
 public async Task UpdateMaintenanceJob(string planningDate, string jobId, UpdateMaintenanceJob command)
 {
     await _restClient.UpdateMaintenanceJob(planningDate, jobId, command);
 }
Esempio n. 9
0
        public async Task <IActionResult> UpdateMaintenanceJobAsync(DateTime planningDate, Guid jobId, [FromBody] UpdateMaintenanceJob command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // handle command
                    WorkshopPlanning planning = await
                                                updateMaintenanceJobCommandHandler.HandleCommandAsync(planningDate, command);

                    // handle result
                    if (planning == null)
                    {
                        return(NotFound());
                    }

                    // return result
                    return(Ok());
                }
                return(BadRequest());
            }
            catch (ConcurrencyException)
            {
                string errorMessage = "Unable to save changes. " +
                                      "Try again, and if the problem persists " +
                                      "see your system administrator.";
                Log.Error(errorMessage);
                ModelState.AddModelError("ErrorMessage", errorMessage);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }