public void CheckBooksForExpiration() { var books = (from bookDbModel in _context.Books.ToList() select _bookMapper.DbToDomain(bookDbModel)).ToList(); var booksToRemove = new List <Book>(); foreach (var book in books) { var result = book.IsExpired(); if (!result) { return; } var productDbModel = _context.Products.Find(book.ProductId); CustomValidator.ValidateObject(productDbModel); var product = _productMapper.DbToDomain(productDbModel); product.ChangeStatus(ProductStatus.OnSale); DetachService.Detach <ProductDbModel>(_context, product.Id); _context.Products.Update(_productMapper.DomainToDb(product)); booksToRemove.Add(book); } foreach (var book in booksToRemove) { DetachService.Detach <BookDbModel>(_context, book.Id); _context.Books.Remove(_bookMapper.DomainToDb(book)); } _context.SaveChanges(); }
public Customer DbToDomain(CustomerDbModel item) { CustomValidator.ValidateObject(item); var entityName = nameof(Customer); var idColumnName = nameof(CustomerDbModel.Id); var cashBackPercent = _propertyGetter.GetProperty <int>(entityName, nameof(VipCustomer.CashBackPercent), idColumnName, item.Id); if (cashBackPercent == 0) { return(new Customer(item.Id, item.Name, item.Balance)); } var discountPercent = _propertyGetter.GetProperty <int>(entityName, nameof(VipCustomer.DiscountPercent), idColumnName, item.Id); if (cashBackPercent != 0 && discountPercent == 0) { var reliable = (ReliableCustomerDbModel)item; return(new ReliableCustomer(reliable.Id, reliable.Name, reliable.Balance, reliable.CashBack, cashBackPercent)); } var vip = (VipCustomerDbModel)item; return(new VipCustomer(vip.Id, vip.Name, vip.Balance, vip.CashBack, cashBackPercent, vip.Points)); }
public PurchaseInfo(Product product, Purchase purchase) { CustomValidator.ValidateObject(product); CustomValidator.ValidateObject(purchase); ProductId = product.Id; ProductName = product.Name; PurchaseCreationTime = purchase.CreationTime.ToLocalTime(); PriceAtPurchaseMoment = purchase.PriceAtPurchaseMoment; }
public BookInfo(Product product, Book book) { CustomValidator.ValidateObject(product); CustomValidator.ValidateObject(book); ProductId = product.Id; ProductName = product.Name; BookCreationTime = book.CreationTime.ToLocalTime(); BookExpirationTime = book.ExpirationTime.ToLocalTime(); BookReserveDaysCount = book.ReserveDaysCount; }
public async Task UpdateOne(Product item) { CustomValidator.ValidateObject(item); await using var context = new MyDbContext(_options); if (Exists(item.Id)) { var enState = context.Products.Update(_productMapper.DomainToDb(item)); enState.State = EntityState.Modified; await context.SaveChangesAsync(); } }
public async Task AddOne(Purchase item) { CustomValidator.ValidateObject(item); await using var context = new MyDbContext(_options); if (!Exists(item.Id)) { var enState = await context.Purchases.AddAsync(_purchaseMapper.DomainToDb(item)); enState.State = EntityState.Added; await context.SaveChangesAsync(); } }
public void AddOne(Book item) { CustomValidator.ValidateObject(item); var exists = Exists(item.Id); if (!exists) { var enState = _context.Books.Add(_bookMapper.DomainToDb(item)); enState.State = EntityState.Added; _context.SaveChanges(); } }
public Category DbToDomain(CategoryDbModel item) { CustomValidator.ValidateObject(item); var categoryDbModel = _context.Categories.Where(e => e.Id.Equals(item.Id)) .Include(e => e.ProductDbModels).ThenInclude(e => e.StoreProductRelation) .ThenInclude(e => e.StoreDbModel).FirstOrDefault(); return(new Category( (from productDbModel in categoryDbModel.ProductDbModels select _productMapper.DbToDomain(productDbModel)).ToList(), categoryDbModel.Id, categoryDbModel.Name)); }
public void AddReliableCustomer(ReliableCustomer customer) { CustomValidator.ValidateObject(customer); var exists = Exists(customer.Id); if (!exists) { var enState = _context.ReliableCustomers.Add(new ReliableCustomerDbModel(customer.Id, customer.Name, customer.Balance, customer.CashBack, customer.CashBackPercent)); enState.State = EntityState.Added; _context.SaveChanges(); } }
public void DeleteCategoryFromStore(Category category, Guid storeId) { CustomValidator.ValidateObject(category); CustomValidator.ValidateId(storeId); if (_context.StoreCategoryRelation .Any(e => e.CategoryDbModelId.Equals(category.Id) && e.StoreDbModelId.Equals(storeId))) { var storeCatRelToDel = _context.StoreCategoryRelation.First(e => e.CategoryDbModelId.Equals(category.Id) && e.StoreDbModelId.Equals(storeId)); _context.StoreCategoryRelation.Remove(storeCatRelToDel); _context.SaveChanges(); } }
public async Task DeleteProductFromStore(Product product, Guid storeId) { CustomValidator.ValidateObject(product); CustomValidator.ValidateId(storeId); await using var context = new MyDbContext(_options); var storeProdRelToRemove = await context.StoreProductRelation.FirstAsync(e => e.ProductDbModelId.Equals(product.Id) && e.StoreDbModelId.Equals(storeId)); var enState = context.StoreProductRelation.Remove(storeProdRelToRemove); enState.State = EntityState.Deleted; await context.SaveChangesAsync(); }
public void DeleteProductFromStore(Product product, Guid storeId) { CustomValidator.ValidateObject(product); CustomValidator.ValidateId(storeId); if (_context.StoreProductRelation .Any(e => e.ProductDbModelId.Equals(product.Id) && e.StoreDbModelId.Equals(storeId))) { var storeProdToDel = _context.StoreProductRelation.First(e => e.ProductDbModelId.Equals(product.Id) && e.StoreDbModelId.Equals(storeId)); _context.StoreProductRelation.Remove(storeProdToDel); _context.SaveChanges(); } }
public void UpdateOne(Customer item) { CustomValidator.ValidateObject(item); var exists = Exists(item.Id); if (exists) { DetachService.Detach <CustomerDbModel>(_context, item.Id); var enState = _context.Customers.Update(_customerMapper.DomainToDb(item)); enState.State = EntityState.Modified; _context.SaveChanges(); } }
public Mall DbToDomain(MallDbModel item) { CustomValidator.ValidateObject(item); var mallDbModel = _context.Malls.Find(item.Id); _context.Entry(mallDbModel).Collection(st => st.StoreDbModels).Load(); return(new Mall ( (from storeDbModel in mallDbModel.StoreDbModels select _storeMapper.DbToDomain(storeDbModel)).ToList(), item.Id, item.Name, item.Location )); }
public void AddCategoryToStore(Category category, Guid storeId) { CustomValidator.ValidateObject(category); CustomValidator.ValidateId(storeId); var storeCatRel = new StoreCategoryDbModel(storeId, category.Id); if (!_context.StoreCategoryRelation .Any(e => e.CategoryDbModelId.Equals(storeCatRel.CategoryDbModelId) && e.StoreDbModelId.Equals(storeCatRel.StoreDbModelId))) { _context.StoreCategoryRelation.Add(storeCatRel); _context.SaveChanges(); } }
public void AddOne(Category item) { CustomValidator.ValidateObject(item); var exists = Exists(item.Id); if (!exists) { var hasSameName = _context.Categories.Any(cat => cat.Name.ToLower().Equals(item.Name.ToLower())); if (hasSameName) { return; } var enState = _context.Categories.Add(_categoryMapper.DomainToDb(item)); enState.State = EntityState.Added; _context.SaveChanges(); } }
public Store DbToDomain(StoreDbModel item) { CustomValidator.ValidateObject(item); var storeDbModel = _context.Stores.Where(e => e.Id.Equals(item.Id)) .Include(e => e.StoreCategoryRelation) .ThenInclude(e => e.CategoryDbModel) .Include(e => e.StoreProductRelation) .ThenInclude(e => e.ProductDbModel).FirstOrDefault(); return(new Store( (from categoryDbModel in storeDbModel.CategoryDbModels select _categoryMapper.DbToDomainStoreSpecificProducts(categoryDbModel, item.Id)).ToList(), item.Id, item.Name, item.Location, item.Profit)); }
public async Task UpdateOne(Store item) { CustomValidator.ValidateObject(item); await using var context = new MyDbContext(_options); _storeMapper = new StoreMapper(context); if (Exists(item.Id)) { var exists = await HasSameNameAndLocationAsync(item); if (!exists) { var enState = context.Stores.Update(_storeMapper.DomainToDb(item)); enState.State = EntityState.Modified; await context.SaveChangesAsync(); } } }
public void AddOne(Mall item) { CustomValidator.ValidateObject(item); var exists = Exists(item.Id); if (!exists) { var mallWithTheSameNameExists = _context.Malls.Any(m => m.Name.Equals(item.Name) && m.Location.Equals(item.Location)); if (mallWithTheSameNameExists) { return; } var enState = _context.Malls.Add(_mallMapper.DomainToDb(item)); enState.State = EntityState.Added; _context.SaveChanges(); } }
public void AddProductToStore(Product product, Guid storeId) { CustomValidator.ValidateObject(product); CustomValidator.ValidateId(storeId); var storeProdRel = new StoreProductDbModel(storeId, product.Id); if (!Exists(product.Id)) { AddOne(product); } if (!_context.StoreProductRelation .Any(e => e.ProductDbModelId.Equals(storeProdRel.ProductDbModelId) && e.StoreDbModelId.Equals(storeProdRel.StoreDbModelId))) { _context.StoreProductRelation.Add(storeProdRel); _context.SaveChanges(); } }
public async Task AddOne(Category item) { CustomValidator.ValidateObject(item); await using var context = new MyDbContext(_options); _categoryMapper = new CategoryMapper(context); if (!Exists(item.Id)) { var exists = await HasSameNameAsync(item); if (!exists) { var enState = await context.Categories.AddAsync(_categoryMapper.DomainToDb(item)); enState.State = EntityState.Added; await context.SaveChangesAsync(); } } }
public void UpdateOne(Category item) { CustomValidator.ValidateObject(item); var exists = Exists(item.Id); if (exists) { var hasSameName = _context.Categories.Any(cat => cat.Name.ToLower().Equals(item.Name.ToLower())); if (hasSameName) { return; } DetachService.Detach <CategoryDbModel>(_context, item.Id); var enState = _context.Categories.Update(_categoryMapper.DomainToDb(item)); enState.State = EntityState.Modified; _context.SaveChanges(); } }
public void UpdateOne(Mall item) { CustomValidator.ValidateObject(item); var exists = Exists(item.Id); if (exists) { var mallWithTheSameNameExists = _context.Malls.Any(m => m.Name.Equals(item.Name) && m.Location.Equals(item.Location)); if (mallWithTheSameNameExists) { return; } DetachService.Detach <MallDbModel>(_context, item.Id); var enState = _context.Malls.Update(_mallMapper.DomainToDb(item)); enState.State = EntityState.Modified; _context.SaveChanges(); } }
public async Task AddProductToStore(Product product, Guid storeId) { CustomValidator.ValidateObject(product); CustomValidator.ValidateId(storeId); await using var context = new MyDbContext(_options); if (!Exists(product.Id)) { await AddOne(product); } var storeProdRelExists = await StoreProductRelationExists(storeId, product.Id); if (!storeProdRelExists) { var storeProdRel = new StoreProductDbModel(storeId, product.Id); await context.StoreProductRelation.AddAsync(storeProdRel); } await context.SaveChangesAsync(); }
public void AddOne(Store item) { CustomValidator.ValidateObject(item); var exists = Exists(item.Id); if (!exists) { var storeWithTheSameLocationAndNameExists = _context.Stores.Any(st => st.Location.Equals(item.Location) && st.Name.Equals(item.Name) && !st.Id.Equals(item.Id) && st.MallId == null); if (storeWithTheSameLocationAndNameExists) { return; } var enState = _context.Stores.Add(_storeMapper.DomainToDb(item)); enState.State = EntityState.Added; _context.SaveChanges(); } }
public void UpdateOne(Store item) { CustomValidator.ValidateObject(item); var exists = Exists(item.Id); if (exists) { var storeWithTheSameLocationAndNameExists = _context.Stores.Any(st => st.Location.Equals(item.Location) && st.Name.Equals(item.Name) && !st.Id.Equals(item.Id) && st.MallId == null); if (storeWithTheSameLocationAndNameExists) { return; } DetachService.Detach <StoreDbModel>(_context, item.Id); var enState = _context.Stores.Update(_storeMapper.DomainToDb(item)); enState.State = EntityState.Modified; _context.SaveChanges(); } }
public PurchaseDbModel DomainToDb(Purchase item) { CustomValidator.ValidateObject(item); return(new PurchaseDbModel(item.Id, item.ClientId, item.ProductId, item.CreationTime, item.PriceAtPurchaseMoment)); }
public CategoryDbModel DomainToDb(Category item) { CustomValidator.ValidateObject(item); return(new CategoryDbModel(item.Id, item.Name)); }
public BookDbModel DomainToDb(Book item) { CustomValidator.ValidateObject(item); return(new BookDbModel(item.Id, item.CustomerId, item.ProductId, item.CreationTime, item.ExpirationTime, item.ReserveDaysCount)); }
public Category(List <Product> products, Guid id, string name) : this(id, name) { CustomValidator.ValidateObject(products); _products = products; }