private void ValidateProductionDate(CreateUserCarModel model, Car car) { if (car == null || model == null) { return; } var productionDate = new DateTime(model.ProductionDateYear, model.ProductionDateMonth, 1); var beginningOfProduction = new DateTime(car.BeginningOfProductionYear, car.BeginningOfProductionMonth, 1); DateTime endOfProduction; if (car.EndOfProductionYear != null && car.EndOfProductionMonth != null) { endOfProduction = DateTime.UtcNow; } else { endOfProduction = new DateTime((int)car.EndOfProductionYear, (int)car.EndOfProductionMonth, 1); } if (productionDate < beginningOfProduction || productionDate > endOfProduction) { this.ModelState.AddModelError(nameof(model.ProductionDateYear), string.Format (GlobalConstants.USERCAR_PRODUCTIONDATE_INVALID, beginningOfProduction.ToString("MMMM yyyy"), endOfProduction.ToString("MMMM yyyy"))); } }
public async Task <IActionResult> Create(CreateUserCarModel createUserCarModel) { createUserCarModel.OwnerId = userManager.GetUserId(this.User); var car = this.carsService.GetById(createUserCarModel.SelectedCar.Value); var validDateTime = DateTime.TryParse($"{createUserCarModel.ProductionDateYear}/{createUserCarModel.ProductionDateMonth}/01", out DateTime dateTime); if (car == null || !validDateTime) { return(this.BadRequest()); } if (createUserCarModel.Photos == null) { this.ModelState.AddModelError(nameof(createUserCarModel.Photos), GlobalConstants.IMAGE_REQUIRED); } ValidatePhotos(createUserCarModel.Photos, nameof(CreateUserCarModel.Photos)); ValidateProductionDate(createUserCarModel, car); if (!this.ModelState.IsValid) { var makes = this.makesService .GetAll() .To <MakeDTO>(); var models = this.modelsService .GetAllForMake(createUserCarModel.SelectedMake.Value) .To <ModelDTO>(); var generations = this.generationsService .GetAllForModel(createUserCarModel.SelectedModel.Value) .To <GenerationDTO>(); var cars = this.carsService .GetAllForGeneration(createUserCarModel.SelectedGeneration.Value) .To <CarDTO>(); createUserCarModel.MakeList = new SelectList(makes, nameof(MakeDTO.Id), nameof(MakeDTO.Name)); createUserCarModel.ModelList = new SelectList(models, nameof(ModelDTO.Id), nameof(ModelDTO.Name)); createUserCarModel.GenerationList = new SelectList(generations, nameof(GenerationDTO.Id), nameof(GenerationDTO.Name)); createUserCarModel.CarList = new SelectList(cars, nameof(CarDTO.Id), nameof(CarDTO.DisplayText)); return(this.View(createUserCarModel)); } var userCar = Mapper.Map <UserCar>(createUserCarModel); userCar.CarId = car.Id; await this.userCarsService.AddAsync(userCar); await imagesService.UploadImagesAsync(createUserCarModel.Photos.ToList(), GlobalConstants.USERCAR_PHOTO_PATH_TEMPLATE, userCar.Id.ToString()); return(this.RedirectToAction(nameof(All))); }
public IActionResult Create() { var viewModel = new CreateUserCarModel(); var allMakes = this.makesService.GetAll().To <MakeDTO>(); viewModel.MakeList = new SelectList(allMakes, nameof(MakeDTO.Id), nameof(MakeDTO.Name)); return(this.View(viewModel)); }