コード例 #1
0
        /// <summary>
        /// DELETE /api/products/{id}
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public HttpResponseMessage Delete(int id, ProductModel model)
        {
            var context = this.DbContext;
            var entity = context.Products.Find(id);

            if (entity == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
            }

            if (!this.User.CanDelete(entity))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }

            // create the web event
            var webEvent = new ProductDeletedEvent(entity);

            // delete the entity
            context.Products.Remove(entity);

            // persist changes to the database
            context.SaveChanges();

            // fire the web event
            webEvent.Raise();

            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
コード例 #2
0
        /// <summary>
        /// PUT /api/products/{id}
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public ProductModel Put(int id, ProductModel model)
        {
            var context = this.DbContext;
            var entity = context.Products.Find(id);

            if (entity == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
            }

            if (!this.User.CanUpdate(entity))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }

            // update the entity
            entity.UpdateFrom(model);

            // update Supplier property
            if (entity.Supplier == null
                || entity.Supplier.Id != model.SupplierId)
            {
                entity.Supplier = context.Suppliers.Find(model.SupplierId);
            }

            // update Category property
            if (entity.Category == null
                || entity.Category.Id != model.CategoryId)
            {
                entity.Category = context.Categories.Find(model.CategoryId);
            }

            // persist changes to the database
            context.SaveChanges();

            // fire the web event
            new ProductUpdatedEvent(entity).Raise();

            return selector(entity);
        }
コード例 #3
0
        /// <summary>
        /// POST /api/products
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public HttpResponseMessage Post(ProductModel model)
        {
            var context = this.DbContext;

            if (!this.User.CanCreate<Product>())
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }

            // transform the ProductModel to Product
            var entity = model.TransformTo<Product>();

            // update Supplier property
            if (entity.Supplier == null
                || entity.Supplier.Id != model.SupplierId)
            {
                entity.Supplier = context.Suppliers.Find(model.SupplierId);
            }

            // update Category property
            if (entity.Category == null
                || entity.Category.Id != model.CategoryId)
            {
                entity.Category = context.Categories.Find(model.CategoryId);
            }

            // add the entity
            context.Products.Add(entity);

            // persist changes to the database
            context.SaveChanges();

            // fire the web event
            new ProductCreatedEvent(entity).Raise();

            // create response
            var response = Request.CreateResponse<ProductModel>(HttpStatusCode.Created, selector(entity));
            string uri = Url.Link("Api", new { id = entity.Id });
            response.Headers.Location = new Uri(uri);
            return response;
        }