public void CreateNewVehicle() { var currUser = new ApplicationUser() { }; var newBattery = new Battery() { Status = BatteryStatus.Good, SoftwareVersion = "2020.48.5", Mileage = 30000, HorsePower = 503, KilowattHour = 375, Range = 450, }; var newVehicle = new CreateVehicleModel() { VehicleModel = VehicleModel.Model_3, VehicleType = VehicleType.Sedan, ImageURL = "https://stimg.cardekho.com/images/carexteriorimages/930x620/Tesla/Tesla-Model-3/5100/1558500541732/front-left-side-47.jpg?tr=h-48", DateOfPurchase = new DateTime(2018, 3, 15), Description = "My Tesla Model 3 car", ServiceId = 1, BatteryId = newBattery.Id, }; this.vehicleService.CreateVehicle(currUser.Id, newVehicle); var expect = 1; }
public void CreateVehicle(string userId, CreateVehicleModel vehicle) { if (this.userRepository.All().Any(x => x.Id == userId)) { var currBattery = this.batteryRepository.All().FirstOrDefault(x => x.Id == vehicle.BatteryId.Trim()); var currInsurance = this.insuranceRepository.All().FirstOrDefault(x => x.Id == vehicle.InsuranceId.Trim()); var currService = this.serviceRepository.All().FirstOrDefault(x => x.Id == vehicle.ServiceId); if (currBattery == null || currInsurance == null || currService == null) { return; } var currVehicle = new Vehicle() { VehicleModel = vehicle.VehicleModel, VehicleType = vehicle.VehicleType, ImageURL = vehicle.ImageURL, DateOfPurchase = vehicle.DateOfPurchase, BatteryId = vehicle.BatteryId, Battery = currBattery, UserId = userId, User = this.userRepository.All().FirstOrDefault(x => x.Id == userId), Description = vehicle.Description, InsuranceId = vehicle.InsuranceId, Insurance = currInsurance, ServiceId = vehicle.ServiceId, Service = currService, }; this.vehicleRepository.AddAsync(currVehicle).GetAwaiter().GetResult(); this.vehicleRepository.SaveChangesAsync().GetAwaiter().GetResult(); } }
public async Task <IActionResult> Create(CreateVehicleModel input) { var user = await this.userManager.GetUserAsync(this.User); if (!Enum.IsDefined(typeof(VehicleType), input.VehicleType)) { return(this.NotFound("Vehicle type not exist!")); } else if (!Enum.IsDefined(typeof(VehicleModel), input.VehicleModel)) { return(this.NotFound("Vehicle model not exist!")); } var validDate = DateTime.Compare(input.DateOfPurchase, DateTime.UtcNow); if (validDate > 0) { return(this.ValidationProblem("You set date from future!")); } try { WebRequest webRequest = WebRequest.Create(input.ImageURL); WebResponse webResponse = await webRequest.GetResponseAsync(); } catch //// If exception thrown then couldn't get response from address { return(this.NotFound()); } this.vehicleService.CreateVehicle(user.Id, input); return(this.Redirect("All")); }
public ActionResult CreateVehicle(CreateVehicleModel vehicle) { //var token = Session["BearerToken"].ToString(); var url = ConfigurationManager.AppSettings["HostName"]; if (vehicle.PlateNo != null) { var vehicleRequest = new VehicleDTO() { PlateNo = vehicle.PlateNo, Model = vehicle.Model, Brand = vehicle.Brand, VehicleType = vehicle.VehicleType, ServiceWeight = vehicle.ServiceWeight, DateInTrafficFirstTime = vehicle.DateInTrafficFirstTime, ServiceIsBooked = vehicle.ServiceIsBooked }; string jsonregistercar = JsonConvert.SerializeObject(vehicleRequest); var httpcontent = new StringContent(jsonregistercar, Encoding.UTF8, "application/json"); using (HttpClient client = new HttpClient()) { var response = client.PostAsync(url + "api/vehicle", httpcontent).Result; } } return(View()); }
public async Task <VehiclesResults> Handle(VehicleAddCommand command, CancellationToken cancellationToken) { try { _logger.LogInformation("Creating contact started."); CreateVehicleModel vehicle = new CreateVehicleModel(); CreateVehicleOtherPropertyModel otherVehicleProperty = new CreateVehicleOtherPropertyModel(); vehicle.VehicleTypeId = command.VehicleTypeId; vehicle.Make = command.Make; vehicle.Model = command.Model; vehicle.VehicleOtherProperties = command.VehicleOtherProperties; //call saving of vehicle record await _vehicleService.AddVehicle(vehicle); //check if vehicle has other property if (command.VehicleOtherProperties.Length > 0) { foreach (var rec in command.VehicleOtherProperties) { string[] propertListValues = rec.Split("|"); otherVehicleProperty.VehicleTypePropertyId = Convert.ToInt32(propertListValues[0]); otherVehicleProperty.PropertyValue = propertListValues[1].ToString(); otherVehicleProperty.VehicleId = vehicle.VehicleId; //call saving of other vehicle property await _vehicleService.AddVehicleOtherProperty(otherVehicleProperty); } } return(new VehiclesResults { StatusCode = Convert.ToInt32(HttpStatusCode.OK), Message = Convert.ToString(HttpStatusCode.OK), MessageDetails = ManageVehicleResponseStatus.Created, VehicleId = vehicle.VehicleId }); } catch (Exception ex) { _logger.LogError("Error creating vehicle : {ExceptionMessage}", ex.ToString()); return(new VehiclesResults { StatusCode = Convert.ToInt32(HttpStatusCode.InternalServerError), Message = Convert.ToString(HttpStatusCode.InternalServerError), MessageDetails = ManageVehicleResponseStatus.CreateFailed }); } }
public ActionResult CreateVehicle(CreateVehicleModel model) { try { CompanyFacade.CreateVehicle(model.Vehicle, model.CompanyId); } catch (ArgumentException e) { ViewBag.Message = e.Message; return(View(model)); } return(RedirectToAction("CompanyVehicles", new { companyId = model.CompanyId })); }
public Vehicle Create(CreateVehicleModel model) { var brand = _databaseService.Brands.Find(model.BrandId); var vehicle = _factory.Create(model.RegistrationNumber, brand, model.RegistrationDate); _databaseService.Vehicles.Add(vehicle); _databaseService.Save(); return(vehicle); }
private Vehicle GetOrCreateVehicle() { var vehicle = _database.Vehicles.Where(v => v.RegistrationNumber == _model.VehicleRegistrationNumber).FirstOrDefault(); if (vehicle == null) { var createVehicleModel = new CreateVehicleModel(); createVehicleModel.RegistrationNumber = _model.VehicleRegistrationNumber; createVehicleModel.RegistrationDate = _model.VehicleRegistrationDate; createVehicleModel.BrandId = _model.BrandId; vehicle = _vehicleCommand.Create(createVehicleModel); } return(vehicle); }
public async Task CreateVehicle(CreateVehicleModel vehicle) { var vehicleType = await _typeRepo .GetVehicleTypesByName(vehicle.VehicleType) .SingleOrDefaultAsync(); if (vehicleType == null) { vehicleType = new ExampleVehicleType { Name = vehicle.VehicleType }; await _typeRepo.Add(vehicleType); } var entity = Mapper.Map <ExampleVehicle>(vehicle); entity.VehicleType = vehicleType; await _vehicleRepo.Add(entity); }
public async Task <IActionResult> Create([Bind("Name,Abrv,MakeId")] CreateVehicleModel createVehicleModel) { if (ModelState.IsValid) { try { var vehicleModel = _mapper.Map <CreateVehicleModel, VehicleModel>(createVehicleModel); var numberOfSaves = await _vehicleModelService.CreateVehicleModelAsync(vehicleModel); return(RedirectToAction(nameof(Index), new { makeId = vehicleModel.MakeId })); } catch (Exception) { return(RedirectToAction("Error", "Home")); } } return(View(createVehicleModel)); }
public async Task <VehicleEntity> Create(CreateVehicleModel model) { var vehicle = new VehicleEntity { ModelName = model.ModelName, ModelYear = model.ModelYear, BodyStyle = model.BodyStyle, ManufacturerEntityId = model.ManufacturerEntityId, CreateUserId = model.CreateUserId, CreateDate = DateTime.UtcNow.ToLocalTime(), ModifyUserId = model.ModifyUserId, ModifyDate = DateTime.UtcNow.ToLocalTime(), StatusId = 1 }; _unitOfWork.Add(vehicle); await _unitOfWork.CommitAsync(); return(vehicle); }
public async Task <int> AddVehicle(CreateVehicleModel vehicleModel) { await _createVehicleCommand.Execute(vehicleModel); return(vehicleModel.VehicleId); }
public async Task <int> AddVehicle(CreateVehicleModel vehicleModel) { vehicleModel.VehicleId = 1; return(vehicleModel.VehicleId); }
// Check entity on create public string ValidateVehicleCreating(CreateVehicleModel vehicle, AspNetUser currentUser, IModelService modelService) { if (vehicle.LicenseNumber.Length > 50) { return("Biển số xe phải dưới 50 ký tự."); } if (vehicle.Name.Length > 100) { return("Tên xe phải dưới 100 ký tự.'"); } if (modelService.Get().All(m => m.ID != vehicle.ModelID)) { return("Dòng xe không tồn tại."); } if (vehicle.Year < Models.Constants.MIN_YEAR || vehicle.Year > DateTime.Now.Year) { return("Năm sản xuất không hợp lệ"); } if (currentUser.Garages.All(g => g.ID != vehicle.GarageID)) { return("Garage không tồn tại."); } if (vehicle.VehicleGroupID.HasValue && currentUser.VehicleGroups.All(g => g.ID != vehicle.VehicleGroupID)) { return("Nhóm xe không tồn tại."); } if (!Models.Constants.TRANSMISSION_TYPE.ContainsKey(vehicle.TransmissionType)) { return("Loại hộp số không hợp lệ."); } if (vehicle.FuelType.HasValue && !Models.Constants.FUEL_TYPE.ContainsKey(vehicle.FuelType.Value)) { return("Loại nhiên liệu không hợp lệ"); } if (vehicle.TransmissionDetail != null && vehicle.TransmissionDetail.Length > 100) { return("Chi tiết hộp số phải dưới 100 ký tự."); } if (vehicle.Engine != null && vehicle.Engine.Length > 100) { return("Chi tiết động cơ phải dưới 100 ký tự."); } if (vehicle.Description != null && vehicle.Description.Length > 1000) { return("Mô tả xe phải dưới 1000 ký tự."); } if (!Models.Constants.COLOR.ContainsKey(vehicle.Color)) { return("Mã màu không hợp lệ."); } return(null); }
public async Task <ActionResult> CreateVehicleAPI(CreateVehicleModel newVehicle) { var modelService = this.Service <IModelService>(); var userId = this.User.Identity.GetUserId(); var currentUser = this.Service <IUserService>().Get(userId); var errorMessage = ValidateVehicleCreating(newVehicle, currentUser, modelService); if (errorMessage != null) { Response.StatusCode = 400; return(Json(new { message = errorMessage })); } var newVehicleEntity = this.Mapper.Map <Vehicle>(newVehicle); if (Request.Files.Count < 4 || Request.Files.Count > 10) { Response.StatusCode = 400; return(Json(new { message = "Chỉ được phép upload từ 4 đến 10 hình." })); } // Upload images to cloudinary var cloudinary = new CloudinaryDotNet.Cloudinary(Models.Constants.CLOUDINARY_ACC); var imageList = new List <VehicleImage>(); try { foreach (string fileName in Request.Files) { var file = Request.Files[fileName]; if (file?.ContentLength <= 0) { continue; } // Upload to cloud var uploadResult = cloudinary.Upload(new ImageUploadParams() { File = new FileDescription(file.FileName, file.InputStream) }); // Get the image's id and url imageList.Add(new VehicleImage() { ID = uploadResult.PublicId, URL = uploadResult.Uri.ToString() }); } } catch (Exception ex) { Response.StatusCode = 500; return(Json(new { message = "Upload ảnh thất bại. Vui lòng thử lại sau." })); } var vehicleService = this.Service <IVehicleService>(); await vehicleService.CreateAsync(newVehicleEntity); foreach (var image in imageList) { image.VehicleID = newVehicleEntity.ID; image.Vehicle = newVehicleEntity; } newVehicleEntity.VehicleImages = imageList; await vehicleService.UpdateAsync(newVehicleEntity); Response.StatusCode = 200; return(Json(new { message = "Tạo xe thành công." })); }
public async Task <IActionResult> CreateNewVehicle(CreateVehicleModel vehicle) { await _service.CreateVehicle(vehicle); return(Ok()); }
public IActionResult Create() { var viewModel = new CreateVehicleModel(); return(this.View(viewModel)); }