public async Task <ActionResult> Add(VehicleModelViewModel model) { try { var result = await _modelRepo.GetModelByName(model.Description); if (result != null) { return(BadRequest("Bilmodellen existerar redan i systemet!")); } var newModel = new VehicleModel { Description = model.Description }; _modelRepo.Add(newModel); if (await _modelRepo.SaveAllAsync()) { return(StatusCode(201, newModel)); } return(StatusCode(500, "Det gick inte att spara ner ny bilmodell")); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
/// <summary> /// Add / Update Vehicle Model /// </summary> public VehicleModel AddUpdateVehicleModel(VehicleModel vehicleModel) { VehicleModel dbVersion = vehicleModelRepository.Find(vehicleModel.VehicleModelId); if (vehicleModelRepository.VehicleModelCodeDuplicationCheck(vehicleModel)) { throw new CaresException(Resources.FleetPool.VehicleModel.VehicleModelCodeDuplicationError); } if (dbVersion != null) { UpdateCompanyProperties(vehicleModel, dbVersion); vehicleModelRepository.Update(dbVersion); } else { dbVersion = new VehicleModel(); SetCompanyProperties(vehicleModel, dbVersion); vehicleModelRepository.Add(dbVersion); } vehicleModelRepository.SaveChanges(); // To Load the proprties return(vehicleModelRepository.Find(dbVersion.VehicleModelId)); }