示例#1
0
        public async Task <TEntity> CreateAsync(TEntity entity)
        {
            var createdEntity = _set.AddAsync(entity);
            await _context.SaveChangesAsync();

            return((await createdEntity).Entity);
        }
示例#2
0
        public async Task <Product> CreateAsync(Product entity)
        {
            var record = await _context.Products.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(record.Entity);
        }
示例#3
0
        public async Task <int> SaveProduct(Guid id, Product product)
        {
            if (id != Guid.Empty)
            {
                product.Id = id;
                _productCatalogContext.Update(product);
            }
            else
            {
                _productCatalogContext.Add(product);
            }

            return(await _productCatalogContext.SaveChangesAsync());
        }
        public async Task <Product> Handle(ProductDeleteDataRequest request, CancellationToken cancellationToken)
        {
            var response = _context.Products.Remove(request.Product).Entity;
            await _context.SaveChangesAsync(cancellationToken);

            return(response);
        }
示例#5
0
        public async Task <Product> Handle(ProductCreateDataRequest request, CancellationToken cancellationToken)
        {
            var response = await _context.Products.AddAsync(request.Product, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(response.Entity);
        }
        public async Task <Guid> Handle(RegisterNewProductCommand request, CancellationToken cancellationToken)
        {
            var newProductID = Guid.NewGuid();

            db.Products.Add(new Product(id: newProductID, name: request.Name, description: request.Description));

            await db.SaveChangesAsync();

            await mediator.Publish(new ProductRegistered { ProductID = newProductID });

            return(newProductID);
        }
        public async Task <int> Handle(CreateCategory request, CancellationToken cancellationToken = default)
        {
            var category = new Category
            {
                Description = request.Description
            };

            _context.Categories.Add(category);
            await _context.SaveChangesAsync();

            return(category.Code);
        }
示例#8
0
        public async Task <IActionResult> Create([Bind("Id,Name,Description,Quantity")] Product product)
        {
            if (ModelState.IsValid)
            {
                var productExists = await _context.Product.FirstOrDefaultAsync(p => p.Name == product.Name);

                if (productExists == null)
                {
                    _context.Add(product);
                    await _context.SaveChangesAsync();

                    await _hubContext.Clients.All.SendAsync("ReceiveNewProduct");

                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    ModelState.AddModelError("Duplicate", "The product name entered has already been used.");
                }
            }
            return(View(product));
        }
        public async Task <Unit> Handle(DeleteProduct request, CancellationToken cancellationToken = default)
        {
            var product = await _context.Products.FindAsync(request.Code);

            if (product == null)
            {
                throw new NotFoundException(nameof(Product), request.Code);
            }

            _context.Products.Remove(product);
            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
示例#10
0
        public async Task <Unit> Handle(DeleteCategory request, CancellationToken cancellationToken = default)
        {
            var category = await _context.Categories.FindAsync(request.Code);

            if (category == null)
            {
                throw new NotFoundException(nameof(Category), request.Code);
            }

            _context.Categories.Remove(category);
            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
示例#11
0
        public async Task <int> Handle(CreateProduct request, CancellationToken cancellationToken = default)
        {
            var category = await _context.Categories.FindAsync(request.CategoryCode);

            if (category == null)
            {
                throw new NotFoundException(nameof(Category), request.CategoryCode);
            }

            var product = _mapper.Map <Product>(request);

            product.Category = category;

            _context.Products.Add(product);
            await _context.SaveChangesAsync();

            return(product.Code);
        }
示例#12
0
        public async Task <Unit> Handle(UpdateProduct request, CancellationToken cancellationToken = default)
        {
            var product = await _context.Products.FindAsync(request.Code);

            if (product == null)
            {
                throw new NotFoundException(nameof(Product), request.Code);
            }

            var category = await _context.Categories.FindAsync(request.CategoryCode);

            if (category == null)
            {
                throw new NotFoundException(nameof(Category), request.CategoryCode);
            }

            _mapper.Map(request, product);
            product.Category = category;

            await _context.SaveChangesAsync();

            return(Unit.Value);
        }