Exemple #1
0
        // Thực hiện chèn dữ liệu mẫu, 2 sản phẩm
        public static async void InsertProduct()
        {
            using (var context = new ProductsContext())
            {
                // Dùng đối tượng DbSet để thêm
                await context.products.AddAsync(new Product
                {
                    Name     = "Sản phẩm 1",
                    Provider = "Công ty 1"
                });

                // Dùng context để thêm
                await context.AddAsync(new Product()
                {
                    Name     = "Sản phẩm 2",
                    Provider = "Công ty 1"
                });


                // Thực hiện Insert vào DB các dữ liệu đã thêm.
                int rows = await context.SaveChangesAsync();

                Console.WriteLine($"Đã lưu {rows} sản phẩm");
            }
        }
Exemple #2
0
        public async Task <IActionResult> Post([FromBody] ProductModel productModel)
        {
            Product product = productModel;

            var errors = _productValidator.Validate(product);

            if (errors.Any())
            {
                return(BadRequest(errors));
            }

            product.Id = ObjectId.GenerateNewId().AsGuid();

            await _productContext.AddAsync(product);

            await _productContext.SaveChangesAsync();

            _productCreatedPublisher.Publish(new ProductCreatedEvent
            {
                Id          = product.Id.AsObjectId(),
                Title       = product.Title,
                Price       = product.Price,
                Description = product.Description,
                Version     = product.Version
            });

            return(Ok(new ProductModel(product)));
        }
Exemple #3
0
        public async Task <TEntity> AddAsync(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException($"{nameof(AddAsync)} entity must not be null");
            }

            try
            {
                await RepositoryContext.AddAsync(entity);

                await RepositoryContext.SaveChangesAsync();

                return(entity);
            }
            catch (Exception ex)
            {
                throw new Exception($"{nameof(entity)} could not be saved: {ex.Message}");
            }
        }
        public async Task <IActionResult> CreateAsync([FromBody] ProductsDTO products)
        {
            if (products is null || products.Quantity < 0 || string.IsNullOrWhiteSpace(products.Label))
            {
                return(BadRequest());
            }

            var createdProduct = new Products
            {
                Id       = Guid.NewGuid(),
                Quantity = products.Quantity,
                Label    = products.Label
            };

            await _context.AddAsync(createdProduct);

            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetByIdAsync), new { createdProduct.Id }, createdProduct));
        }
Exemple #5
0
        public async Task <Product1> Create(Product1 product1)
        {
            try
            {
                await _products.AddAsync(product1);

                await _products.SaveChangesAsync();

                return(product1);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
            catch (DbUpdateException e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }