예제 #1
0
    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 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 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();
        }
    }
예제 #4
0
    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();
        }
    }
예제 #5
0
    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 async Task CheckBooksForExpiration()
        {
            await using var context = new MyDbContext(_options);
            var bookDbModels = await context.Books.ToListAsync();

            var books         = (from bookDbModel in bookDbModels select _bookMapper.DbToDomain(bookDbModel)).ToList();
            var booksToRemove = new List <Book>();

            foreach (var book in books)
            {
                var isExpired = book.IsExpired();
                if (!isExpired && context.Products.Any(e => e.Id.Equals(book.ProductId)))
                {
                    continue;
                }

                var productDbModel = await context.Products.FindAsync(book.ProductId);

                if (productDbModel != null)
                {
                    var product = _productMapper.DbToDomain(productDbModel);
                    product.ChangeStatus(ProductStatus.OnSale);
                    DetachService.Detach <ProductDbModel>(context, product.Id);
                    var enState = context.Products.Update(_productMapper.DomainToDb(product));
                    enState.State = EntityState.Modified;
                }

                booksToRemove.Add(book);
            }

            foreach (var book in booksToRemove)
            {
                DetachService.Detach <BookDbModel>(context, book.Id);
                context.Books.Remove(_bookMapper.DomainToDb(book));
            }

            await context.SaveChangesAsync();
        }