/// <inheritdoc /> public bool AddUrl(string key, string url) { _context.Add(new MappedUrl() { Key = key, Url = url }); return(_context.SaveChanges() == 1); }
private long Create() { var product = new Product() { Name = "Test Product Name", Description = "Test Product Description", Price = 20, Quantity = 1 }; _db.Products.Add(product); _db.SaveChanges(); return(product.Id); }
public ActionResult<Department> Edit(string title) { var department = _db.Departments.Find(Id); if (department == null) { return NotFound(); } department.Title = title; _db.SaveChanges(); return department; }
public void Delete(Product element) { using (StorageDbContext context = new StorageDbContext()) { context.Configuration.AutoDetectChangesEnabled = false; using (var transaction = context.Database.BeginTransaction()) { try { ConteinerRepository conteiner = new ConteinerRepository(); conteiner.DeleteAll(element); IngredientsForProductRepository recept = new IngredientsForProductRepository(); recept.Delete(element); context.Configuration.ValidateOnSaveEnabled = false; context.Products.Attach(element); context.Entry(element).State = EntityState.Deleted; context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw new InvalidOperationException("Помилка видалення."); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } } }
public void Create(Client client) { using (StorageDbContext dbContext = new StorageDbContext()) { using (var transaction = dbContext.Database.BeginTransaction()) { try { Client newEntryClient = dbContext.Clients.FirstOrDefault(n => n.Name == client.Name); if (newEntryClient != null) { throw new Exception("Даний клієнт вже існує."); } dbContext.Clients.Add(client); dbContext.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } } }
public void Edit(Ingredient newIngredient) { using (StorageDbContext context = new StorageDbContext()) { context.Configuration.AutoDetectChangesEnabled = false; using (var transaction = context.Database.BeginTransaction()) { try { Ingredient ingredientForChanging = context.Ingredients.First(n => n.Id == newIngredient.Id); ingredientForChanging.Name = newIngredient.Name; context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw new InvalidOperationException("Помилка редагування."); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } } }
public void Edit(Client newClient) { using (StorageDbContext dbContext = new StorageDbContext()) { using (var transaction = dbContext.Database.BeginTransaction()) { try { if (newClient.Name == null || newClient.Name == "") { throw new Exception("Не вказано ім'я клієнта."); } dbContext.Clients.First(n => n.Id == newClient.Id).Name = newClient.Name; dbContext.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } } }
public void Remove(Tare tareForRemoving) { using (StorageDbContext dbContext = new StorageDbContext()) { using (var transaction = dbContext.Database.BeginTransaction()) { try { if (tareForRemoving.Name == null || tareForRemoving.Name == "") { throw new Exception("Не вказано назву тари."); } if (tareForRemoving.Amount <= 0) { throw new Exception("Не вказано кількість тари."); } dbContext.Tares.First(n => n.Name == tareForRemoving.Name).Amount -= tareForRemoving.Amount; dbContext.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } } }
public void DeleteById(int id) { using (StorageDbContext context = new StorageDbContext()) { using (var transaction = context.Database.BeginTransaction()) { try { context.Configuration.ValidateOnSaveEnabled = false; context.Configuration.AutoDetectChangesEnabled = false; var clientForDeleting = context.Clients.First(element => element.Id == id); context.Clients.Attach(clientForDeleting); context.Entry(clientForDeleting).State = EntityState.Deleted; context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } } }
public void Create(Tare tare) { using (StorageDbContext dbContext = new StorageDbContext()) { using (var transaction = dbContext.Database.BeginTransaction()) { try { if (dbContext.Tares.FirstOrDefault(n => n.Name == tare.Name) != null) { throw new Exception("Така тара вже існує."); } dbContext.Tares.Add(tare); dbContext.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } } }
public void Edit(string newPassStr) { using (StorageDbContext dbContext = new StorageDbContext()) { using (var transaction = dbContext.Database.BeginTransaction()) { try { if (newPassStr.Length < 4) { throw new Exception("Пароль занадто короткий. Він має бути не менше 4 символів."); } dbContext.Securities.FirstOrDefault().Pass = Security.GetHashPass(newPassStr); dbContext.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } } }
public void Remove(Conteiner newConteiner) { using (var context = new StorageDbContext()) { var recept = context.IngredientsForProducts.Where(element => element.ProductId == newConteiner.ProductId); foreach (var oneIngredientOfRecept in recept) { foreach (var onePackage in context.Packages) { if (oneIngredientOfRecept.IngredientId == onePackage.IngredientId) { var weight = oneIngredientOfRecept.Weight * newConteiner.Weight * newConteiner.Amount; if (weight <= onePackage.Weight) { onePackage.Weight -= weight; } else { throw new ArgumentOutOfRangeException(); } } } } context.SaveChanges(); } }
public bool DeleteSession(AuthSession session) { if (session == null) { return(false); } _context.Sessions.Remove(session); return(_context.SaveChanges() == 1); }
public ActionResult <Department> Create(string title) { var department = new Department { Title = title, }; _db.Add(department); _db.SaveChanges(); return(department); }
public void Create(List <IngredientsForProduct> newIngredientsForProduct) { using (StorageDbContext context = new StorageDbContext()) { context.Configuration.AutoDetectChangesEnabled = false; foreach (var element in newIngredientsForProduct ?? throw new ArgumentNullException()) { context.IngredientsForProducts.Add(element); } context.ChangeTracker.DetectChanges(); context.SaveChanges(); } }
public void Create(Product newProduct, Dictionary <Ingredient, double> receipt) { using (StorageDbContext context = new StorageDbContext()) { context.Configuration.AutoDetectChangesEnabled = false; using (var transaction = context.Database.BeginTransaction()) { try { var product = context.Products.Where(element => element.Name == newProduct.Name); if (!product.Any()) { context.Products.Add(newProduct); context.ChangeTracker.DetectChanges(); context.SaveChanges(); var currentProduct = context.Products.FirstOrDefault(buffProduct => buffProduct.Name == newProduct.Name); foreach (var element in receipt) { context.IngredientsForProducts.Add(new IngredientsForProduct(currentProduct, element.Key, element.Value)); } context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } else { throw new ArgumentException("Цей продукт вже існує."); } } catch (Exception e) { transaction.Rollback(); throw new ArgumentException(e.Message); } } } }
public void Create(Ingredient newIngredient) { using (StorageDbContext context = new StorageDbContext()) { var ingredient = context.Ingredients.FirstOrDefault(element => element.Name == newIngredient.Name); if (ingredient == null) { context.Ingredients.Add(newIngredient); context.SaveChanges(); } else { throw new ArgumentException($"Цей інгредієнт вже існує."); } } }
public void Delete(Ingredient ingredient) { using (StorageDbContext context = new StorageDbContext()) { context.Configuration.AutoDetectChangesEnabled = false; using (var transaction = context.Database.BeginTransaction()) { try { PackageRepository package = new PackageRepository(); ProductRepository productRepository = new ProductRepository(); IngredientsForProductRepository ingredientsForProductRepository = new IngredientsForProductRepository(); package.Delete(ingredient); var allReceiptsWithEntryIngredients = ingredientsForProductRepository.GetDataSource().Where(n => n.IngredientId == ingredient.Id).ToList(); if (allReceiptsWithEntryIngredients != null) { foreach (var oneIngredientForProductInReceipt in allReceiptsWithEntryIngredients) { Product productWithEntryIngredientInReceipt = productRepository.GetDataSource().First(n => n.Id == oneIngredientForProductInReceipt.ProductId); productRepository.Delete(productWithEntryIngredientInReceipt); } } context.Configuration.ValidateOnSaveEnabled = false; context.Ingredients.Attach(ingredient); context.Entry(ingredient).State = EntityState.Deleted; context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw new InvalidOperationException("Помилка видалення."); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } } }
public ActionResult DoUpload(IFormFile file) { using (var stream = file.OpenReadStream()) { var xs = new XmlSerializer(typeof(StorageModel)); var storage = (StorageModel)xs.Deserialize(stream); using (var db = new StorageDbContext()) { var dbs = new DbStorage() { Name = storage.Name, Capacity = storage.Capacity, Address = storage.Address, StorageType = storage.StorageType, }; dbs.Items = new Collection <DbItem>(); foreach (var item in storage.Items) { dbs.Items.Add(new DbItem() { Name = item.Name, Price = item.Price, Units = item.Units, Manufacturer = item.Manufacturer }); } dbs.Workers = new Collection <DbWorkers>(); foreach (var worker in storage.Workers) { dbs.Workers.Add(new DbWorkers() { Name = worker.Name, Position = worker.Position, Experience = worker.Experience, }); } db.StorageFacilities.Add(dbs); db.SaveChanges(); } return(View(storage)); } }
public static void Seed(StorageDbContext context) { var listCountry = new List <SearchProvider> { new SearchProvider { Id = 1, Name = "Google" }, new SearchProvider { Id = 2, Name = "Bing" }, new SearchProvider { Id = 3, Name = "Yandex" } }; context.Providers.AddRange(listCountry); context.SaveChanges(); }
public void Provider_Repository_Create() { //Arrange var c = new SearchProvider { Name = "Yahoo" }; //Act var result = _repository.Add(c); _databaseDbContext.SaveChanges(); var lst = _repository.GetAll().ToList(); //Assert Assert.AreEqual(4, lst.Count); Assert.AreEqual("Yahoo", lst.Last().Name); }
public void Add(Tare tare) { using (StorageDbContext dbContext = new StorageDbContext()) { using (var transaction = dbContext.Database.BeginTransaction()) { try { dbContext.Tares.First(n => n.Name == tare.Name).Amount += tare.Amount; dbContext.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } } }
public void Edit(IngredientsForProduct newIngredientsForProduct) { using (StorageDbContext context = new StorageDbContext()) { using (var transaction = context.Database.BeginTransaction()) { try { context.IngredientsForProducts.First(n => n.Id == newIngredientsForProduct.Id).IngredientId = newIngredientsForProduct.IngredientId; context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } } }
public ActionResult <User> Create(string fullname, int departmentId) { var department = _db.Departments.Find(departmentId); if (department == null) { return(BadRequest()); } var user = new User { FullName = fullname, DepartmentId = departmentId, }; _db.Users.Add(user); _db.SaveChanges(); return(user); }
public void Add(int ingredientId, int typeEvent, double weight, DateTime dateTime) { using (var context = new StorageDbContext()) { using (var transaction = context.Database.BeginTransaction()) { try { context.IngredientStatistics.Add( new IngredStatElement(ingredientId, typeEvent, weight, dateTime) ); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); } } } }
public void Edit(Product newProduct, Dictionary <Ingredient, double> newReceipt) { using (StorageDbContext context = new StorageDbContext()) { context.Configuration.AutoDetectChangesEnabled = false; using (var transaction = context.Database.BeginTransaction()) { try { IngredientsForProductRepository ingredientsForProductRepository = new IngredientsForProductRepository(); var currentProduct = context.Products.FirstOrDefault(element => element.Id == newProduct.Id); if (currentProduct == null) { throw new ArgumentNullException("Цей продукт не існує"); } currentProduct.Name = newProduct.Name; ingredientsForProductRepository.Delete(currentProduct); foreach (var element in newReceipt) { context.IngredientsForProducts.Add(new IngredientsForProduct(currentProduct, element.Key, element.Value)); } context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception e) { transaction.Rollback(); throw new ArgumentException(e.Message); } } } }
public void Remove(int index, DateTime dateRemove, int typeEvent, int amount = 1) { using (StorageDbContext context = new StorageDbContext()) { using (var transaction = context.Database.BeginTransaction()) { try { context.Configuration.AutoDetectChangesEnabled = false; context.Conteiners.Find(index).Amount -= amount; var res = context.Conteiners.Find(index); if (res.Amount < 0) { throw new ArgumentOutOfRangeException(); } if (res.Amount == 0) { var someConteiner = context.Conteiners.Find(index); context.Configuration.ValidateOnSaveEnabled = false; context.Conteiners.Attach(someConteiner); context.Entry(someConteiner).State = EntityState.Deleted; } context.ProductStatistics.Add(new ProdStatElement(res.ProductId, typeEvent, res.Weight * amount, dateRemove)); context.ChangeTracker.DetectChanges(); context.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw new ArgumentException(); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } } }
public void Delete(Product product) { using (StorageDbContext context = new StorageDbContext()) { try { context.Configuration.ValidateOnSaveEnabled = false; context.Configuration.AutoDetectChangesEnabled = false; var recept = context.IngredientsForProducts.Where(element => element.ProductId == product.Id); foreach (var elementOfRecept in recept) { context.IngredientsForProducts.Attach(elementOfRecept); context.Entry(elementOfRecept).State = EntityState.Deleted; } context.ChangeTracker.DetectChanges(); context.SaveChanges(); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } }
public void Remove(DateTime date1, DateTime date2) { using (var context = new StorageDbContext()) { try { context.Configuration.ValidateOnSaveEnabled = false; context.Configuration.AutoDetectChangesEnabled = false; var listOfStatistics = context.IngredientStatistics.Where(element => element.Date <= date2 && element.Date >= date1); foreach (var someConteiner in listOfStatistics) { context.IngredientStatistics.Attach(someConteiner); context.Entry(someConteiner).State = EntityState.Deleted; } context.ChangeTracker.DetectChanges(); context.SaveChanges(); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } }
public void DeleteByWeight(Conteiner oneConteiner) { using (StorageDbContext context = new StorageDbContext()) { try { context.Configuration.ValidateOnSaveEnabled = false; context.Configuration.AutoDetectChangesEnabled = false; var listOfConteiner = context.IngredientsForProducts.Where(element => element.ProductId == oneConteiner.ProductId & element.Weight == oneConteiner.Weight); foreach (var someConteiner in listOfConteiner) { context.IngredientsForProducts.Attach(someConteiner); context.Entry(someConteiner).State = EntityState.Deleted; } context.ChangeTracker.DetectChanges(); context.SaveChanges(); } finally { context.Configuration.ValidateOnSaveEnabled = true; } } }