public bool Update(int id, ProductInputDTO inputEntity)
        {
            using IDbConnection connection = new SqlConnection(connectionString);
            connection.Open();
            using IDbTransaction transaction = connection.BeginTransaction();

            try {
                var     updateProductQuery = "Update Products SET Number = @Number, Name = @Name, Category = @Category, Price = @Price WHERE Id = @Id";
                Product product            = mapper.Map <Product>(inputEntity);
                product.Id = id;
                var result = connection.Execute(updateProductQuery, product, transaction) == 1;

                var deleteProductShopsQuery = "DELETE FROM ProductShops WHERE ProductId = @Id";
                connection.Execute(deleteProductShopsQuery, product, transaction);
                AddProductShops(connection, transaction, id, inputEntity.ShopIds);

                var deleteProductWarehousesQuery = "DELETE FROM ProductWarehouses WHERE ProductId = @Id";
                connection.Execute(deleteProductWarehousesQuery, product, transaction);
                AddProductWarehouses(connection, transaction, id, inputEntity.WarehouseIds);

                transaction.Commit();

                return(result);
            } catch {
                transaction.Rollback();
                throw;
            }
        }
Esempio n. 2
0
 public IActionResult PutProduct(int id, ProductInputDTO product)
 {
     if (!repository.Update(id, product))
     {
         return(NotFound());
     }
     return(Ok());
 }
Esempio n. 3
0
        public IActionResult Create(ProductInputDTO input)
        {
            if (ModelState.IsValid)
            {
                var product = _mapper.Map <Product>(input);
                _unitOfWork.Products.Add(product);
                _unitOfWork.Complete();

                return(RedirectToAction("Index"));
            }

            return(View(input));
        }
        public int Add(ProductInputDTO inputEntity)
        {
            using IDbConnection connection = new SqlConnection(connectionString);
            connection.Open();
            using IDbTransaction transaction = connection.BeginTransaction();

            try {
                var insertProductQuery = "INSERT INTO Products (Number, Name, Category, Price) VALUES (@Number, @Name, @Category, @Price); SELECT SCOPE_IDENTITY()";
                var createdProductId   = connection.ExecuteScalar <int>(insertProductQuery, inputEntity, transaction);

                AddProductShops(connection, transaction, createdProductId, inputEntity.ShopIds);
                AddProductWarehouses(connection, transaction, createdProductId, inputEntity.WarehouseIds);

                transaction.Commit();

                return(createdProductId);
            } catch {
                transaction.Rollback();
                throw;
            }
        }
Esempio n. 5
0
 public int PostProduct(ProductInputDTO item)
 {
     return(repository.Add(item));
 }