コード例 #1
0
 public ActionResult Create(TruckInspectionViewModel truckInspection)
 {
     if (!ModelState.IsValid)
     {
         _truckInspectionService.PopulateDriversTrucksAndPartsCatalog(truckInspection);
         return(View(truckInspection));
     }
     else
     {
         var truckInspectionReturnedFromServiceLayer = _truckInspectionService.CreateTruckInspection(truckInspection);
         if (truckInspectionReturnedFromServiceLayer.AlertStyle == AlertStyle.Success)
         {
             return(View(_truckInspectionService.PopulateDriversTrucksAndPartsCatalog(truckInspectionReturnedFromServiceLayer)));
         }
         else
         {
             int currentMileageConvertedToInt;
             if (int.TryParse(truckInspectionReturnedFromServiceLayer.CurrentMileage, out currentMileageConvertedToInt))
             {
                 if (currentMileageConvertedToInt <= truckInspectionReturnedFromServiceLayer.LastMileageReported)
                 {
                     ModelState.AddModelError("CurrentMileage", "Mileage entered must be greater than " + truckInspection.LastMileageReported.ToString());
                 }
             }
             _truckInspectionService.PopulateDriversTrucksAndPartsCatalog(truckInspectionReturnedFromServiceLayer);
             return(View(truckInspectionReturnedFromServiceLayer));
         }
     }
 }
コード例 #2
0
        private void PostNewTruckInspectionToDB(TruckInspectionViewModel inspectionFromUser)
        {
            //Insert Inspection data from UI into TruckInspection table and return report ID
            //that we will use as a FK to insert items into PartsInspected table
            string partsInspectedForeignKey = AddMasterDataToTruckInspectionTableAndReturnTruckInspectionId(inspectionFromUser);

            inspectionFromUser.PartsReportedForeignKeyId = partsInspectedForeignKey;

            //Log Comments insertion notification and save the data into DB
            _logger.Info("Inserted inspection report number " + inspectionFromUser.PartsReportedForeignKeyId + " for Truck " + inspectionFromUser.TruckId + " into Table " + truckInspectionTableNameUsedByLogger);
            _truckInspectionContext.Commit();

            //Insert data from General Catalog into TruckInspection Table
            AddCatalogItemsIntoPartsInspectedTable(inspectionFromUser, inspectionFromUser.GeneralCatalog);

            //Insert data from Lights Catalog into TruckInspection Table
            AddCatalogItemsIntoPartsInspectedTable(inspectionFromUser, inspectionFromUser.LightsCatalog);

            //Insert data from Fluids Catalog into TruckInspection Table
            AddCatalogItemsIntoPartsInspectedTable(inspectionFromUser, inspectionFromUser.FluidsCatalog);

            //Log TruckInspection insertion notification and save the data into DB
            _logger.Info("Inserted report number " + inspectionFromUser.PartsReportedForeignKeyId + " into Table " + partsInspectedTableNameUsedByLogger);
            _partsInspectedContext.Commit();
        }
コード例 #3
0
        private DateTime RetrieveDateLatestReportWasSubmittedForThisTruck(TruckInspectionViewModel inspectionFromUser)
        {
            //Set date for tomorrow, so we know that TruckInspection table has no records
            var defaultDate = DateTimeOffset.Now.AddDays(1);

            return(_truckInspectionContext.Collection().Where(d => d.TruckId == inspectionFromUser.TruckId).DefaultIfEmpty().Max(d => d == null ?  defaultDate : d.CreationDate).Date);
        }
コード例 #4
0
        private bool ValidateTruckInspectionData(TruckInspectionViewModel inspectionToValidate)
        {
            if (InspectionReportHasNOTBeenSubmittedForThisTruckToday(inspectionToValidate) && MileageReportedIsGreaterThanPreviousOne(inspectionToValidate))
            {
                return(true);
            }

            return(false);
        }
コード例 #5
0
        public TruckInspectionViewModel PopulateDriversTrucksAndPartsCatalog(TruckInspectionViewModel inspectionToPopulate)
        {
            inspectionToPopulate.DriverList     = ConvertDriverNamesToDropDownListView(_driverContext.Collection().OrderBy(n => n.FirstName).ToList());
            inspectionToPopulate.TruckList      = ConvertTruckNumbersToDropDownListView(_truckContext.Collection().OrderBy(t => t.TruckNumber).Where(s => s.Status == true).ToList());
            inspectionToPopulate.GeneralCatalog = ConvertGeneralCatalogToCheckBoxListView(_partCatalogContext.Collection().Where(g => g.PartCategoryId == generalCategoryId).OrderBy(p => p.PartName).ToList());
            inspectionToPopulate.LightsCatalog  = ConvertLightsCatalogToCheckBoxListView(_partCatalogContext.Collection().Where(l => l.PartCategoryId == lightCategoryId).OrderBy(c => c.PartName).ToList());
            inspectionToPopulate.FluidsCatalog  = ConvertFluidsCatalogToCheckBoxListView(_partCatalogContext.Collection().Where(f => f.PartCategoryId == fluidCategoryId).OrderBy(c => c.PartName).ToList());

            return(inspectionToPopulate);
        }
コード例 #6
0
 private bool InspectionReportHasNOTBeenSubmittedForThisTruckToday(TruckInspectionViewModel InspectionObject)
 {
     //Validate that last report was submitted before today's date
     //Also check that LastTimeAReportWasSubmitted was tomorrow - we set this date in case table has no records
     if (DateTime.Today.Date > InspectionObject.LastTimeAReportWasSubmitted || InspectionObject.LastTimeAReportWasSubmitted == DateTime.Today.AddDays(1).Date)
     {
         return(true);
     }
     return(false);
 }
コード例 #7
0
        private bool MileageReportedIsGreaterThanPreviousOne(TruckInspectionViewModel mileageToValidate)
        {
            var intCurrentMileage = Convert.ToInt32(mileageToValidate.CurrentMileage);

            if (intCurrentMileage > mileageToValidate.LastMileageReported)
            {
                return(true);
            }
            return(false);
        }
コード例 #8
0
 private void AddCatalogItemsIntoPartsInspectedTable(TruckInspectionViewModel inspectionObject, List <CheckBoxListViewModel> partsCatalog)
 {
     foreach (CheckBoxListViewModel part in partsCatalog)
     {
         var partInspectedToInsert = new PartsInspected();
         partInspectedToInsert.IsOK = part.IsChecked;
         partInspectedToInsert.TruckInspectionId = inspectionObject.PartsReportedForeignKeyId;
         partInspectedToInsert.PartCatalogId     = part.Id;
         _partsInspectedContext.Insert(partInspectedToInsert);
     }
 }
コード例 #9
0
        public TruckInspectionViewModel CreateTruckInspection(TruckInspectionViewModel inspectionToCreate)
        {
            var inspectionToValidate = CreateANewTruckInspectionObjectWithDataFromDBThatHasToBeValidated(inspectionToCreate);

            if (ValidateTruckInspectionData(inspectionToValidate))
            {
                PostNewTruckInspectionToDB(inspectionToValidate);
                AddSuccessMessage(inspectionToValidate, "Success!!", true);
            }
            else
            {
                if (InspectionReportHasNOTBeenSubmittedForThisTruckToday(inspectionToValidate) == false)
                {
                    AddDangerMessage(inspectionToValidate, "Report already submitted for this truck", true);
                }
            }
            return(inspectionToValidate);
        }
コード例 #10
0
        private string AddMasterDataToTruckInspectionTableAndReturnTruckInspectionId(TruckInspectionViewModel truckInspection)
        {
            var inspectionDataToInsert = new TruckInspection();

            inspectionDataToInsert.DriverId = truckInspection.DriverId;
            inspectionDataToInsert.TruckId  = truckInspection.TruckId;
            var currentMileageConvertedToString = ConvertStringToInt(truckInspection.CurrentMileage);

            //Check that string mileage coming from UI is a valid integer
            if (currentMileageConvertedToString != -1)
            {
                inspectionDataToInsert.Mileage = currentMileageConvertedToString;
            }
            else
            {
                _logger.Warning("There was an error converting CurrentMileage from String to Int in TruckInspectionService");
            }

            inspectionDataToInsert.Comments = truckInspection.Comments;
            _truckInspectionContext.Insert(inspectionDataToInsert);
            return(inspectionDataToInsert.Id);
        }
コード例 #11
0
        private TruckInspectionViewModel CreateANewTruckInspectionObjectWithDataFromDBThatHasToBeValidated(TruckInspectionViewModel inspectionObjectFromView)
        {
            inspectionObjectFromView.LastMileageReported         = RetrieveLastMileageReportedPerTruckAndDriver(inspectionObjectFromView);
            inspectionObjectFromView.LastTimeAReportWasSubmitted = RetrieveDateLatestReportWasSubmittedForThisTruck(inspectionObjectFromView);
            //Read maximum ticket number in TruckInspection Table and add 1 to generate new one
            //inspectionObjectFromView.ReportId = RetrieveLastReportId(inspectionObjectFromView);

            var NewTruckInspectionObject = new TruckInspectionViewModel()
            {
                DriverId                    = inspectionObjectFromView.DriverId,
                TruckId                     = inspectionObjectFromView.TruckId,
                CurrentMileage              = inspectionObjectFromView.CurrentMileage,
                LastMileageReported         = inspectionObjectFromView.LastMileageReported,
                LastTimeAReportWasSubmitted = inspectionObjectFromView.LastTimeAReportWasSubmitted,
                DriverList                  = inspectionObjectFromView.DriverList,
                TruckList                   = inspectionObjectFromView.TruckList,
                GeneralCatalog              = inspectionObjectFromView.GeneralCatalog,
                LightsCatalog               = inspectionObjectFromView.LightsCatalog,
                FluidsCatalog               = inspectionObjectFromView.FluidsCatalog,
                Comments                    = inspectionObjectFromView.Comments
            };

            return(NewTruckInspectionObject);
        }
コード例 #12
0
 private int RetrieveLastMileageReportedPerTruckAndDriver(TruckInspectionViewModel inspectionFromUser)
 {
     return(_truckInspectionContext.Collection().Where(t => t.TruckId == inspectionFromUser.TruckId).DefaultIfEmpty().Max(m => m == null ? 0 : m.Mileage));
 }
コード例 #13
0
 private void AddDangerMessage(TruckInspectionViewModel inspectionCreated, string message, bool dismissable)
 {
     inspectionCreated.AlertStyle  = AlertStyle.Danger;
     inspectionCreated.Message     = message;
     inspectionCreated.Dismissable = dismissable;
 }
コード例 #14
0
 private void AddSuccessMessage(TruckInspectionViewModel inspectionToCreate, string message, bool dismissable)
 {
     inspectionToCreate.AlertStyle  = AlertStyle.Success;
     inspectionToCreate.Message     = message;
     inspectionToCreate.Dismissable = dismissable;
 }