/// <summary> /// Create a new item in the database, CREATE /// </summary> /// <param name="value">Object to create</param> public void Create(object value) { CarBrand carBrand = value as CarBrand; Model model = value as Model; Extra extra = value as Extra; ModelExtraswitch modelExtraswitch = value as ModelExtraswitch; if (carBrand != null) { this.carBrandRepository.Create(carBrand); } else if (model != null) { this.modelRepository.Create(model); } else if (extra != null) { this.extraRepository.Create(extra); } else if (modelExtraswitch != null) { this.modelExtraSwitchRepository.Create(modelExtraswitch); } else { throw new NoClassException("There is no such a table.", value); } }
/// <summary> /// Removes an item from the database DELETE /// </summary> /// <param name="mainMenuWaitingKey">Main menu key which defines the table</param> /// <param name="idToDelete">Id of the data to be deleted.</param> public void Delete(string mainMenuWaitingKey, int idToDelete) { if (mainMenuWaitingKey == "0") { CarBrand carBrandToDelete = this.carBrandRepository.ReadAll().FirstOrDefault(x => x.Carbrand_Id == idToDelete); if (carBrandToDelete == null) { throw new NoIdFoundException("There is no such an ID.", carBrandToDelete); } else { this.carBrandRepository.Delete(carBrandToDelete); } } else if (mainMenuWaitingKey == "1") { Model modelToDelete = this.modelRepository.ReadAll().FirstOrDefault(x => x.Model_Id == idToDelete); if (modelToDelete == null) { throw new NoIdFoundException("There is no such an ID.", modelToDelete); } else { this.modelRepository.Delete(modelToDelete); } } else if (mainMenuWaitingKey == "2") { Extra extraToDelete = this.extraRepository.ReadAll().FirstOrDefault(x => x.Extra_Id == idToDelete); if (extraToDelete == null) { throw new NoIdFoundException("There is no such an ID.", extraToDelete); } else { this.extraRepository.Delete(extraToDelete); } } else if (mainMenuWaitingKey == "3") { ModelExtraswitch modelExtraSwitchToDelete = this.modelExtraSwitchRepository.ReadAll().FirstOrDefault(x => x.ModelExtraswitch_Id == idToDelete); if (modelExtraSwitchToDelete == null) { throw new NoIdFoundException("There is no such an ID.", modelExtraSwitchToDelete); } else { this.modelExtraSwitchRepository.Delete(modelExtraSwitchToDelete); } } else { throw new NoClassException("There is no such a table.", mainMenuWaitingKey); } }
public void WhenCreatingModelExtraSwitchData_WithoutError_NoExceptionThrown() { Mock <ICarBrandRepository> carbrandMock = new Mock <ICarBrandRepository>(); Mock <IModelRepository> modelMock = new Mock <IModelRepository>(); Mock <IExtraRepository> extraMock = new Mock <IExtraRepository>(); Mock <IModelExtraSwitchRepository> modelextraMock = new Mock <IModelExtraSwitchRepository>(); Mock <IJava> javaMock = new Mock <IJava>(); CarBrandLogic logic = new CarBrandLogic(carbrandMock.Object, modelMock.Object, extraMock.Object, modelextraMock.Object, javaMock.Object); ModelExtraswitch modelExtraswitch = new ModelExtraswitch() { ModelExtraswitch_Id = 1, Model_Id = 1, Extra_Id = 1 }; // ASSERT Assert.That(() => logic.Create(modelExtraswitch), Throws.Nothing); modelextraMock.Verify(x => x.Create(It.IsAny <ModelExtraswitch>()), Times.Once); }
/// <summary> /// agdf /// </summary> /// <typeparam name="T">gfj</typeparam> /// <param name="key">jsf</param> /// <param name="newValues">gfjs</param> public void Update <T>(int key, object newValues) { try { using (CarShopDataEntities carShopDataEntities = new CarShopDataEntities()) { Type t = typeof(T); // Type a = typeof(object); // Might be used instead of Type t = typeof(T) if (t == typeof(CarBrand)) { CarBrand carBrand = carShopDataEntities.CarBrands.FirstOrDefault(x => x.Carbrand_Id == key); carBrand = newValues as CarBrand; Console.WriteLine($"{carBrand.Carbrand_Name} was updated"); } else if (t == typeof(Extra)) { Extra extra = carShopDataEntities.Extras.FirstOrDefault(x => x.Extra_Id == key); extra = newValues as Extra; Console.WriteLine($"{extra.Extra_Name} was updated"); } else if (t == typeof(Model)) { Model model = carShopDataEntities.Models.FirstOrDefault(x => x.Model_Id == key); model = newValues as Model; Console.WriteLine($"{model.Model_Name} was updated"); } else if (t == typeof(ModelExtraswitch)) { ModelExtraswitch modelExtraswitch = carShopDataEntities.ModelExtraswitches.FirstOrDefault(x => x.ModelExtraswitch_Id == key); modelExtraswitch = newValues as ModelExtraswitch; Console.WriteLine($"ModelExtraSwitch ID:{modelExtraswitch.ModelExtraswitch_Id} was updated"); } carShopDataEntities.SaveChanges(); } } catch (Exception) { throw new Exception(); } }
/// <summary> /// Read or Select function /// </summary> /// <typeparam name="T">Type of the object where</typeparam> /// <param name="key">The key which will be search for.</param> /// <returns>Returns the object with the key</returns> public object Read <T>(int key) { try { using (CarShopDataEntities carShopDataEntities = new CarShopDataEntities()) { Type t = typeof(T); // Type a = typeof(object); // Might be used instead of Type t = typeof(T) if (t == typeof(CarBrand)) { CarBrand carBrand = carShopDataEntities.CarBrands.FirstOrDefault(x => x.Carbrand_Id == key); return(carBrand); } else if (t == typeof(Extra)) { Extra extra = carShopDataEntities.Extras.FirstOrDefault(x => x.Extra_Id == key); return(extra); } else if (t == typeof(Model)) { Model model = carShopDataEntities.Models.FirstOrDefault(x => x.Model_Id == key); return(model); } else if (t == typeof(ModelExtraswitch)) { ModelExtraswitch modelExtraswitch = carShopDataEntities.ModelExtraswitches.FirstOrDefault(x => x.ModelExtraswitch_Id == key); return(modelExtraswitch); } return(null); } } catch (Exception) { throw new Exception(); } }
/// <summary> /// Insert object in the correct table /// </summary> /// <typeparam name="T">Type of the object</typeparam> /// <param name="value">The object which will be inserted into the database.</param> public void Create <T>(object value) { try { using (CarShopDataEntities carShopDataEntities = new CarShopDataEntities()) { Type t = typeof(T); // Type a = typeof(object); // Might be used instead of Type t = typeof(T) if (t == typeof(CarBrand)) { CarBrand carBrand = value as CarBrand; carShopDataEntities.CarBrands.Add(carBrand); } else if (t == typeof(Extra)) { Extra extra = value as Extra; carShopDataEntities.Extras.Add(extra); } else if (t == typeof(Model)) { Model model = value as Model; carShopDataEntities.Models.Add(model); } else if (t == typeof(ModelExtraswitch)) { ModelExtraswitch modelExtraswitch = value as ModelExtraswitch; carShopDataEntities.ModelExtraswitches.Add(modelExtraswitch); } carShopDataEntities.SaveChanges(); } } catch (Exception) { throw new Exception(); } }
/// <summary> /// Removes an item from the database DELETE /// </summary> /// <param name="itemToBeDeleted">The item which wanted to be deleted</param> public void Delete(ModelExtraswitch itemToBeDeleted) { this.carShopDataEntities.ModelExtraswitches.Remove(itemToBeDeleted); this.carShopDataEntities.SaveChanges(); }
/// <summary> /// Create a new item in the database, CREATE /// </summary> /// <param name="newItem">Gives the new item which needs to be inserted.</param> public void Create(ModelExtraswitch newItem) { this.carShopDataEntities.ModelExtraswitches.Add(newItem); this.carShopDataEntities.SaveChanges(); }
/// <summary> /// Get infos for new element /// </summary> /// <param name="mainMenuWaitingKey">Main menu key</param> /// <param name="logic">ILogic interface</param> private static void GetInfosForNewElement(string mainMenuWaitingKey, ILogic logic) { try { // "Car Brand", "Models", "Extras", "Model-Extras" if (mainMenuWaitingKey == "0") { // ICarBrandRepository icarbrandrepo = new CarBrandRepository(); Console.Write("New car brand name: "); var brandname = Console.ReadLine(); Console.Write("\nNew car brand country name: "); var countryname = Console.ReadLine(); Console.Write("\nNew car brand Url: "); var url = Console.ReadLine(); Console.Write("\nNew car brand Foundation Year (number e.g.: 1950): "); var foundationyear = int.Parse(Console.ReadLine()); Console.Write("\nNew car brand Yearly Traffic: "); var yearlytraffic = Console.ReadLine(); CarBrand carBrand = new CarBrand() { Carbrand_Name = brandname, Carbrand_Country_Name = countryname, Carbrand_Url = url, Carbrand_Foundation_Year = foundationyear, Carbrand_Yearly_Traffic = yearlytraffic }; logic.Create(carBrand); } else if (mainMenuWaitingKey == "1") { // IModelRepository modelRepository = new ModelRepository(); Console.Write("New model carbrand ID (number): "); var carbrandid = int.Parse(Console.ReadLine()); Console.Write("New model name: "); var modelname = Console.ReadLine(); Console.Write("New model release day e.g. 2018-11-23: "); var releaseday = DateTime.Parse(Console.ReadLine()); Console.Write("New model engine volume (number) e.g. 1900 : "); var enginevolume = int.Parse(Console.ReadLine()); Console.Write("New model horsepower (number) e.g. 75 : "); var horsepower = int.Parse(Console.ReadLine()); Console.Write("New model base price (number) e.g. 50000: "); var baseprice = int.Parse(Console.ReadLine()); Model model = new Model() { Carbrand_Id = carbrandid, Model_Name = modelname, Model_Release_Day = releaseday, Model_Engine_Volume = enginevolume, Model_Horsepower = horsepower, Model_Base_Price = baseprice }; logic.Create(model); } else if (mainMenuWaitingKey == "2") { // IExtraRepository extraRepository = new ExtraRepository(); Console.Write("New extra caregory name: "); var categoryname = Console.ReadLine(); Console.Write("New extra name: "); var extraname = Console.ReadLine(); Console.Write("New extra price (number) e.g. 3000: "); var extraprice = int.Parse(Console.ReadLine()); Console.Write("New extra color: "); var extracolor = Console.ReadLine(); Console.Write("New extra multiple usage (0 == no, 1 == yes): "); var multipleUsage = int.Parse(Console.ReadLine()); Extra extra = new Extra() { Extra_Category_Name = categoryname, Extra_Name = extraname, Extra_Price = extraprice, Extra_Color = extracolor, Extra_Multiple_Usage = multipleUsage }; logic.Create(extra); } else if (mainMenuWaitingKey == "3") { // IModelExtraSwitchRepository modelExtraSwitchRepository = new ModelExtraSwitchRepository(); Console.Write("New Model_Id (number): "); var modelid = int.Parse(Console.ReadLine()); Console.Write("New Extra_Id (number): "); var extraid = int.Parse(Console.ReadLine()); ModelExtraswitch modelExtraswitch = new ModelExtraswitch() { Model_Id = modelid, Extra_Id = extraid }; logic.Create(modelExtraswitch); } } catch (FormatException exception) { Console.WriteLine("\n\nYou gave wrong format to the previous table data, try again!\n\nException message: " + exception.Message + "\n\n"); } catch (NoClassException exception) { Console.WriteLine(exception.Msg); } }