public async Task <IActionResult> Add(CreateCarViewModel model, int modelId, int engineId) { if (!this.ModelState.IsValid) { return(this.RedirectToAction("Error", "Home")); } try { var createCarServiceModel = new CreateCarServiceModel() { Id = model.Id, ProductionDate = model.ProductionDate, VIN = model.VIN, Price = model.Price, Color = model.Color, ModelId = modelId, EngineId = engineId }; this.car.Create(createCarServiceModel); await this._carHub.Clients.All.SendAsync("NewOrder"); return(RedirectToAction("All")); } catch (Exception x) { LogExceptionWithMessage(x); ViewBag.Section = "error"; FillViewBagWithDataForSelectInView(); return(View()); } }
public void Create(CreateCarServiceModel model) { if (model.Color == null || model.EngineId == 0 || model.ModelId == 0 || model.VIN == "" || model.Price == 0 || model.ProductionDate.Year < 1991) { throw new ArgumentException("Invalid Input data"); } if (data.Cars.Any(x => x.VIN == model.VIN)) { throw new ArgumentException("Cannot Insert car with Same VIN"); } var car = new Car() { ProductionDate = model.ProductionDate, VIN = model.VIN, Price = model.Price, Color = model.Color, ModelId = model.ModelId, EngineId = model.EngineId }; this.data.Add(car); this.data.SaveChanges(); }