Ejemplo n.º 1
0
        public IHttpActionResult Post(Proxies.Secret secret)
        {
            if (Attribute.GetCustomAttributes(this.GetType(), typeof(Attributes.PostSupportedAttribute), false) == null)
            {
                return(ResponseMessage(new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.NotImplemented)));
            }

            if (secret == null)
            {
                return(BadRequest());
            }

            try
            {
                Secret secretExisting = null;
                Secret secNew         = null;

                using (SqlConnection conn = new SqlConnection(Startup.ConnectionString))
                {
                    conn.Open();
                    using (var trans = conn.BeginTransaction())
                    {
                        secretExisting = Secret.GetByID <Secret>(conn, trans, secret.ID);
                        if (secretExisting != null)
                        {
                            Secret.Delete <Secret>(conn, trans, secretExisting);
                        }

                        #region New item
                        secNew = new Secret()
                        {
                            ID = secret.ID,
                            CertificateThumbprint = secret.CertificateThumbprint,
                            PlainTextValue        = secret.PlainTextValue
                        };
                        Secret.Insert(conn, trans, secNew);
                        #endregion

                        trans.Commit();
                    }
                }

                if (secretExisting == null)
                {
                    return(Created("", secNew));
                }
                else
                {
                    return(Ok(secNew));
                }
            }
            catch (System.Exception exce)
            {
                log.ErrorFormat("Unhandled exception processing Secret POST: {0:S}", exce.ToString());
                return(InternalServerError(exce));
            }
        }