示例#1
0
        public void ApiCreateAttribute(string apiKey, Rock.Core.DTO.Attribute Attribute)
        {
            using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope())
            {
                Rock.CMS.UserService userService = new Rock.CMS.UserService();
                Rock.CMS.User        user        = userService.Queryable().Where(u => u.ApiKey == apiKey).FirstOrDefault();

                if (user != null)
                {
                    uow.objectContext.Configuration.ProxyCreationEnabled = false;
                    Rock.Core.AttributeService AttributeService  = new Rock.Core.AttributeService();
                    Rock.Core.Attribute        existingAttribute = new Rock.Core.Attribute();
                    AttributeService.Add(existingAttribute, user.PersonId);
                    uow.objectContext.Entry(existingAttribute).CurrentValues.SetValues(Attribute);

                    if (existingAttribute.IsValid)
                    {
                        AttributeService.Save(existingAttribute, user.PersonId);
                    }
                    else
                    {
                        throw new WebFaultException <string>(existingAttribute.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
示例#2
0
        public void UpdateAttribute(string id, Rock.Core.DTO.Attribute Attribute)
        {
            var currentUser = Rock.CMS.UserService.GetCurrentUser();

            if (currentUser == null)
            {
                throw new WebFaultException <string>("Must be logged in", System.Net.HttpStatusCode.Forbidden);
            }

            using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope())
            {
                uow.objectContext.Configuration.ProxyCreationEnabled = false;
                Rock.Core.AttributeService AttributeService  = new Rock.Core.AttributeService();
                Rock.Core.Attribute        existingAttribute = AttributeService.Get(int.Parse(id));
                if (existingAttribute.Authorized("Edit", currentUser))
                {
                    uow.objectContext.Entry(existingAttribute).CurrentValues.SetValues(Attribute);

                    if (existingAttribute.IsValid)
                    {
                        AttributeService.Save(existingAttribute, currentUser.PersonId);
                    }
                    else
                    {
                        throw new WebFaultException <string>(existingAttribute.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    throw new WebFaultException <string>("Not Authorized to Edit this Attribute", System.Net.HttpStatusCode.Forbidden);
                }
            }
        }
示例#3
0
        private void btnAttrGo_Click(object sender, EventArgs e)
        {
            Rock.Core.DTO.Attribute attribute = new Core.DTO.Attribute();
            attribute.Key          = tbAttrKey.Text;
            attribute.Name         = tbAttrName.Text;
            attribute.Category     = tbAttrCategory.Text;
            attribute.Description  = tbAttrDescription.Text;
            attribute.DefaultValue = tbAttrValue.Text;
            attribute.Required     = cbAttrRequired.Checked;

            WebClient proxy = new System.Net.WebClient();

            proxy.Headers["Content-type"] = "application/json";
            MemoryStream ms = new MemoryStream();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Rock.Core.DTO.Attribute));

            serializer.WriteObject(ms, attribute);

            try
            {
                byte[] data = proxy.UploadData(string.Format("http://localhost:6229/RockWeb/REST/Core/Attribute/{0}", APIKEY),
                                               "POST", ms.ToArray());
                Stream stream = new MemoryStream(data);
                Rock.Core.DTO.Attribute updatedAttribute = serializer.ReadObject(stream) as Rock.Core.DTO.Attribute;

                MessageBox.Show(string.Format("Attribute Created.  ID: {0}", updatedAttribute.Id), "Created");
            }
            catch (WebException ex)
            {
                //string response = (ex.Response
                using (Stream data = ex.Response.GetResponseStream())
                {
                    string text = new StreamReader(data).ReadToEnd();

                    MessageBox.Show(string.Format("Response: {0}\n{1}",
                                                  ((System.Net.HttpWebResponse)ex.Response).StatusDescription, text), "Error");
                }
            }
        }