예제 #1
0
        public async Task<Product> PostAsync(Product resource)
        {
            // resource validation is happening in the ValidateResourceAttribute defined on the service contract

            try
            {
                await m_repository.AddAsync(resource);
            }
            catch (ArgumentException ex) // invalid input arguments passed to the repository
            {
                throw new HttpResourceFaultException(new[] { ex.Message });
            }

            Product addedResource = m_repository.GetAll().FirstOrDefault(p => p.ID == resource.ID);

            var responseHeaders = new Dictionary<string, string>
            {
                { // Location header with a fully qualified GET URL pointing to the resource by its ID
                    m_context.Response.HeaderNames.Location,
                    m_context.GetUrl<ISampleService>(null, m => m.GetById(resource.ID), null, UriSegments.CreateFromHttpRequest(m_context.Request))
                }
            };

            return Result.ObjectWithResponseStatus(addedResource, HttpStatusCode.Created, "Product added", responseHeaders);
        }
예제 #2
0
        public virtual void Update(Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product");
            }

            if (product.ID <= 0)
            {
                throw new ArgumentException("Product ID must be greater than 0", "product");
            }

            lock (syncRoot)
            {
                Product storedProduct;

                if (!productStore.TryGetValue(product.ID, out storedProduct))
                {
                    throw new InvalidOperationException("Product could not be updated because it cannot be found");
                }

                if (!productStore.TryUpdate(product.ID, product, storedProduct))
                {
                    throw new InvalidOperationException("There was a concurrency problem updating the product");
                }
            }
        }
예제 #3
0
        public async Task<Product> PutAsync(int id, Product resource)
        {
            // resource validation is happening in the ValidateResourceAttribute defined on the service contract

            try
            {
                resource.ID = id;
                await m_repository.UpdateAsync(resource);
            }
            catch (ArgumentException ex) // invalid input arguments passed to the repository
            {
                throw new HttpResourceFaultException(new[] { ex.Message });
            }

            Product updatedResource = m_repository.GetAll().FirstOrDefault(p => p.ID == resource.ID);

            return Result.ObjectWithResponseStatus(updatedResource, HttpStatusCode.OK, "Product updated");
        }
예제 #4
0
        public virtual void Add(Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product");
            }

            if (product.ID != 0)
            {
                throw new ArgumentException("A new product must not have an ID", "product");
            }

            lock (syncRoot)
            {
                product.ID = productStore.Keys.DefaultIfEmpty().Max(key => key) + 1;

                if (!productStore.TryAdd(product.ID, product))
                {
                    throw new InvalidOperationException("Duplicate product ID provided");
                }

                product.Added = DateTime.UtcNow;
            }
        }
예제 #5
0
        private static Product Clone(Product product)
        {
            if (product == null)
            {
                return null;
            }

            return new Product
            {
                ID = product.ID,
                Name = product.Name,
                Price = product.Price,
                Added = product.Added,
                InStock = product.InStock
            };
        }
예제 #6
0
 public Task UpdateAsync(Product product)
 {
     return Task.Factory.StartNew(p => Update((Product) p), product);
 }
예제 #7
0
 public Task AddAsync(Product product)
 {
     return Task.Factory.StartNew(p => Add((Product) p), product);
 }