public async Task <IActionResult> Create(VehicleViewModel vmodel) { if (this.ModelState.IsValid) { if (User.IsInRole("Client")) { var path = string.Empty; var client = _clientRepository.GetClientByUserEmail(User.Identity.Name); var vehicle = _converterHelper.ToVehicle(vmodel, path, true); vehicle.ClientId = client.Id; await _vehicleRepository.CreateAsync(vehicle); return(RedirectToAction("MyVehicles", "Clients")); } else { var path = string.Empty; var cli = await _clientRepository.GetByIdAsync(vmodel.ClientId); var vehicle = _converterHelper.ToVehicle(vmodel, path, true); vehicle.ClientId = cli.Id; await _vehicleRepository.CreateAsync(vehicle); return(RedirectToAction(nameof(Index))); } } return(View(vmodel)); }
public async Task <IActionResult> Create(VehicleViewModel view) { if (ModelState.IsValid) { var path = string.Empty; if (view.ImageFile != null && view.ImageFile.Length > 0) { var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images\\Vehicles", file); using (var stream = new FileStream(path, FileMode.Create)) { await view.ImageFile.CopyToAsync(stream); } path = $"~/images/Vehicles/{file}"; } var vehicle = this.ToVehicle(view, path); vehicle.User = await _userHelper.GetUserByEmailAsync(this.User.Identity.Name); await _vehicleRepository.CreateAsync(vehicle); return(RedirectToAction(nameof(Index))); } return(View(view)); }
public async Task <ActionResult> CreateRegistration(int workshopId, [FromBody] RegistrationCreateDto registrationCreateDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Workshop workshop = await _workshopRepo.GetWorkshopById(workshopId); if (workshop == null) { return(NotFound(new { error = $"workshop with id {workshop} could not be found" })); } Vehicle vehicle = _mapper.Map <Vehicle>(registrationCreateDto.Vehicle); int createdVehicleId = await _vehicleRepo.CreateAsync(vehicle); Registration registration = _mapper.Map <Registration>(registrationCreateDto); registration.VehicleID = createdVehicleId; registration.DateRegistered = DateTime.Now; User user = await _userManager.GetUserAsync(HttpContext.User); registration.UserID = user.Id; int createdRegistrationId = await _registrationRepo.CreateAsync(registration); return(CreatedAtAction(nameof(GetWorkshopSingleRegistration), new { workshopId = workshopId, registrationId = createdRegistrationId }, registrationCreateDto)); }
public async Task <ActionResult> VehiclesCreate(EditContractorVehiclesViewModel model) { if (ModelState.IsValid) { Vehicle vehicle = (Vehicle)model.GetVehicle(); vehicle.ContractorID = model.ContractorID; try { vehicle.CreationDate = DateTime.UtcNow; vehicle.CreationUser = System.Web.HttpContext.Current.User.Identity.Name; await vehicleRepository.CreateAsync(vehicle); return(RedirectToAction("Vehicles", "Contractors", new { id = model.ContractorID })); } catch (Exception e) { var errors = string.Join(",", e.Message); ModelState.AddModelError(string.Empty, errors); } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> CreateVehicleAsync([FromBody] Client.VehicleCreationInfo creationInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (creationInfo == null) { var error = ErrorResponsesService.BodyIsMissing(nameof(creationInfo)); return(BadRequest(error)); } var modelCreationInfo = Converter.VehicleCreationInfoConverter.Convert(creationInfo); var modelVehicle = await repository.CreateAsync(modelCreationInfo, cancellationToken).ConfigureAwait(false); var clientVehicle = Converter.VehicleConverter.Convert(modelVehicle); return(CreatedAtRoute("GetVehicleRoute", new { id = clientVehicle.Id }, clientVehicle)); }
public async Task <Option <Exception, Guid> > Handle(Commands.VehicleCreateCommand request, CancellationToken cancellationToken) { var allReadInParking = await _repository.GetByPlateAsync(request.Plate); if (allReadInParking.IsSuccess) { return(new DuplicateException("Já existe um carro com a placa informada no estacionamento")); } var createCallBack = await _repository.CreateAsync(Mapper.Map <Vehicle>(request)); if (createCallBack.IsFailure) { return(createCallBack.Failure); } return(createCallBack.Success.Id); }
public async Task <IActionResult> Create(VehicleViewModel model) { if (ModelState.IsValid) { try { var path = string.Empty; if (model.ImageFile != null) { path = await _imageHelper.UploadImageAsync(model.ImageFile, "Vehicles"); } var vehicle = _converterHelper.ToVehicle(model, path, true); //TODO: Change for the logged user vehicle.User = await _userHelper.GetUserByEmailAsync(this.User.Identity.Name);//the name is the username which in this case is an email await _vehicleRepository.CreateAsync(vehicle); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "There is already a Licence Plate with that number."); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } return(View(model)); }
public async Task <TEntity> CreateAysnc(TEntity entity) { return(await _vehicleRepository.CreateAsync(entity)); }
public async Task CreateAsync(IVehicle vehicle) { await VehicleRepository.CreateAsync(vehicle); }
public async Task <IActionResult> AddVehicle(AddVehicleViewModel model) { if (ModelState.IsValid) { var user = await _userHelper.GetUserByEmailAsync(this.User.Identity.Name); var vehicle = _converterHelper.ToNewVehicle(model, user); try { if (model.ModelName != null) { if (await _brandRepository.ModelNameExistsAsync(model.ModelName)) { ModelState.AddModelError(string.Empty, $"There is already a model registered with {model.ModelName}, please chose from list of models"); model.Brands = await _vehicleRepository.GetComboSoloBrand(model).ToAsyncEnumerable().ToList(); model.Models = _vehicleRepository.GetComboModels(model.BrandId); model.Fuels = _vehicleRepository.GetComboFuels(); model.Colors = _vehicleRepository.GetComboColors(); return(View(model)); } if (await _brandRepository.ExistsAsync(model.BrandId)) { var newModel = new BrandModel { ModelName = model.ModelName, BrandId = model.BrandId, }; try { await _brandRepository.AddModelFromNewVehicleAsync(newModel); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, $"There is already a model registered with the name {newModel.ModelName}, please chose another from the list"); model.Brands = await _vehicleRepository.GetComboSoloBrand(model).ToAsyncEnumerable().ToList(); model.Models = _vehicleRepository.GetComboModels(model.BrandId); model.Fuels = _vehicleRepository.GetComboFuels(); model.Colors = _vehicleRepository.GetComboColors(); return(View(model)); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); model.Brands = await _vehicleRepository.GetComboSoloBrand(model).ToAsyncEnumerable().ToList(); model.Models = _vehicleRepository.GetComboModels(model.BrandId); model.Fuels = _vehicleRepository.GetComboFuels(); model.Colors = _vehicleRepository.GetComboColors(); return(View(model)); } } var modelId = await _brandRepository.GetModelIdAsync(model.ModelName); model.ModelId = modelId; var vehicle1 = _converterHelper.ToNewVehicle(model, user); try { await _vehicleRepository.CreateAsync(vehicle1); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, $"There is already a Car registered with the Licence PLate number {vehicle.LicencePlate}"); model.Brands = _vehicleRepository.GetComboBrands(); model.Models = _vehicleRepository.GetComboModels(model.BrandId); model.Fuels = _vehicleRepository.GetComboFuels(); model.Colors = _vehicleRepository.GetComboColors(); return(View(model)); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); model.Brands = _vehicleRepository.GetComboBrands(); model.Models = _vehicleRepository.GetComboModels(model.BrandId); model.Fuels = _vehicleRepository.GetComboFuels(); model.Colors = _vehicleRepository.GetComboColors(); return(View(model)); } } } else { ModelState.AddModelError(string.Empty, "The Brand chosen is no longer available, please contact the company for more information"); model.Brands = _vehicleRepository.GetComboBrands(); model.Models = _vehicleRepository.GetComboModels(model.BrandId); model.Fuels = _vehicleRepository.GetComboFuels(); model.Colors = _vehicleRepository.GetComboColors(); return(View(model)); } } await _vehicleRepository.CreateAsync(vehicle); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { if (ModelState.IsValid) { ModelState.AddModelError(string.Empty, $"There is already a Car registered with the licence Plate {vehicle}, please insert another"); model.Brands = _vehicleRepository.GetComboBrands(); model.Models = _vehicleRepository.GetComboModels(model.BrandId); model.Fuels = _vehicleRepository.GetComboFuels(); model.Colors = _vehicleRepository.GetComboColors(); return(View(model)); } } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); model.Brands = _vehicleRepository.GetComboBrands(); model.Models = _vehicleRepository.GetComboModels(model.BrandId); model.Fuels = _vehicleRepository.GetComboFuels(); model.Colors = _vehicleRepository.GetComboColors(); return(View(model)); } } } return(View(model)); }