Esempio n. 1
0
        /// <summary>
        /// POST /api/suppliers
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public HttpResponseMessage Post(SupplierModel model)
        {
            var context = this.DbContext;

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

            // transform the SupplierModel to Supplier
            var entity = model.TransformTo <Supplier>();

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

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

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

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

            response.Headers.Location = new Uri(uri);
            return(response);
        }