public Product GetOne(Guid id)
    {
        CustomValidator.ValidateId(id);
        var exists = Exists(id);

        if (exists)
        {
            return(_productMapper.DbToDomain(_context.Products.Find(id)));
        }
        return(null);
    }
        public async Task <List <Product> > GetClientBookedProducts(Guid clientId)
        {
            CustomValidator.ValidateId(clientId);
            await using var context = new MyDbContext(_options);
            var bookedProductDbModels = await(from productDbModel in context.Products
                                              join bookDbModel in context.Books on productDbModel.Id equals bookDbModel.ProductId
                                              select productDbModel).ToListAsync();
            var bookedProducts = (from bookedProductDbModel in bookedProductDbModels
                                  select _productMapper.DbToDomain(bookedProductDbModel)).ToList();

            return(bookedProducts);
        }
        public async Task <Product> GetOne(Guid id)
        {
            CustomValidator.ValidateId(id);
            await using var context = new MyDbContext(_options);
            if (Exists(id))
            {
                var productDbModel = await context.Products.FindAsync(id);

                return(_productMapper.DbToDomain(productDbModel));
            }

            return(null);
        }
示例#4
0
    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
               ));
    }
示例#5
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();
    }