コード例 #1
0
 public DTO.Product CreateProduct(Product product)
 {
     return new DTO.Product()
     {
         Id=product.Id,
         Code = product.Code,
         Created = product.Created,
         Desc = product.Desc,
         Modified = product.Modified,
         ProductCategoryId = product.ProductCategoryId,
         ProductPrices = product.ProductPrices == null ? new List<DTO.ProductPrice>() :
                     product.ProductPrices.Select(p => productPriceFactory.CreateProductPrice(p)).ToList(),
     };
 }
コード例 #2
0
        public RepositoryActionResult<Product> AddProduct(Product newProduct)
        {
            try
            {
                //Adds new product
                _ctx.Products.Add(newProduct);
                //Saves Changes
                var result = _ctx.SaveChanges();

                //Returns Product & Status
                if (result > 0)
                {
                    return new RepositoryActionResult<Product>(newProduct, RepositoryActionStatus.Created);
                }
                else
                {
                    return new RepositoryActionResult<Product>(newProduct, RepositoryActionStatus.NothingModified, null);
                }
            }
            catch (Exception ex)
            {
                return new RepositoryActionResult<Product>(null, RepositoryActionStatus.Error, ex);
            }
        }
コード例 #3
0
        public RepositoryActionResult<Product> UpdateProduct(Product updatedProduct)
        {
            try
            {
                // Only update when product already exists
                var existingProduct = _ctx.Products.FirstOrDefault(b => b.Id == updatedProduct.Id);
                if (existingProduct == null)
                {
                    return new RepositoryActionResult<Product>(updatedProduct, RepositoryActionStatus.NotFound);
                }

                // Change the original entity status to detached; otherwise, we get an error on attach
                // as the entity is already in the dbSet
                // set original entity state to detached
                _ctx.Entry(existingProduct).State = EntityState.Detached;

                // attach & save
                _ctx.Products.Attach(updatedProduct);

                // set the updated entity state to modified, so it gets updated.
                _ctx.Entry(updatedProduct).State = EntityState.Modified;

                // Save Changes
                var result = _ctx.SaveChanges();

                //Returns Book & Status
                if (result > 0)
                {
                    return new RepositoryActionResult<Product>(updatedProduct, RepositoryActionStatus.Updated);
                }
                else
                {
                    return new RepositoryActionResult<Product>(updatedProduct, RepositoryActionStatus.NothingModified, null);
                }

            }
            catch (Exception ex)
            {
                return new RepositoryActionResult<Product>(null, RepositoryActionStatus.Error, ex);
            }
        }