コード例 #1
0
    public void TestPatchCourse()
    {
        var course = PatchUpdate.ClassroomPatchUpdate(this.TestCourse.Id);

        Assert.IsNotNull(course, "Course not returned.");
        Assert.AreEqual(this.TestCourse.Id, course.Id, "Wrong course returned.");
    }
コード例 #2
0
        public async Task <bool> UpdateAsync(int id, string val, PatchUpdateItem patchUpdateItem)
        {
            PatchUpdate[] patchUpdates = new PatchUpdate[1];
            if (patchUpdateItem == PatchUpdateItem.Name)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Name", value = val
                };
            }
            if (patchUpdateItem == PatchUpdateItem.Style)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Style", value = val
                };
            }
            if (patchUpdateItem == PatchUpdateItem.Color)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Color", value = val
                };
            }
            if (patchUpdateItem == PatchUpdateItem.Size)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Size", value = val
                };
            }
            if (patchUpdateItem == PatchUpdateItem.Price)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Price", value = string.IsNullOrEmpty(val) ? "0":val
                };
            }
            if (patchUpdateItem == PatchUpdateItem.Quatity)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Quatity", value = string.IsNullOrEmpty(val) ? "0" : val
                };
            }
            if (patchUpdateItem == PatchUpdateItem.Description)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Description", value = val
                };
            }

            var jsonData  = JsonSerializer.Serialize(patchUpdates);
            var modelJson =
                new StringContent(jsonData, Encoding.UTF8, "application/json");

            var response = await _httpClient.PatchAsync("api/Product/" + id, modelJson);

            if (response.IsSuccessStatusCode)
            {
                return(true);
            }
            return(false);
        }
コード例 #3
0
        private List <PatchUpdate> ParsePatchUpdates(XmlNode patchNodee)
        {
            List <PatchUpdate> patchUpdates = new List <PatchUpdate>();

            foreach (XmlNode node in patchNodee.ChildNodes)
            {
                PatchUpdate patchUpdate = new PatchUpdate();
                foreach (XmlNode patchNode in node.ChildNodes)
                {
                    switch (patchNode.Name)
                    {
                    case nameof(patchUpdate.PatchesToUpdate):
                        patchUpdate.PatchesToUpdate = patchNode.InnerText.Trim();
                        break;

                    case nameof(patchUpdate.XPath):
                        patchUpdate.XPath = patchNode.InnerText.Trim();
                        break;

                    case nameof(patchUpdate.Search):
                        XmlAttribute singleReturnAttribute = patchNode.Attributes["single"];
                        if (singleReturnAttribute != null)
                        {
                            patchUpdate.SearchReturnFirst = bool.Parse(singleReturnAttribute.InnerText.Trim());
                            Logging.Editor("Search single attribute found and processed as {0}", LogLevel.Debug, patchUpdate.SearchReturnFirst);
                        }
                        patchUpdate.Search = patchNode.InnerText.Trim();
                        break;

                    case nameof(patchUpdate.Replace):
                        patchUpdate.Replace = patchNode.InnerText.Trim();
                        break;
                    }
                }
                patchUpdates.Add(patchUpdate);
            }
            return(patchUpdates);
        }
コード例 #4
0
        protected async Task Change(ChangeEventArgs e, PatchUpdateItem patchUpdateItem)
        {
            var val = e.Value.ToString();

            PatchUpdate[] patchUpdates = new PatchUpdate[1];
            if (patchUpdateItem == PatchUpdateItem.Title)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Title", value = val
                };
                Item.Title = val;
            }
            if (patchUpdateItem == PatchUpdateItem.Paragraph)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Paragraph", value = val
                };
            }
            if (patchUpdateItem == PatchUpdateItem.Order)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Order", value = val
                };
            }
            if (patchUpdateItem == PatchUpdateItem.Disabled)
            {
                patchUpdates[0] = new PatchUpdate {
                    op = "replace", path = "Disabled", value = val
                };
            }
            var isDone = await Service.UpdateAsync(Item.Id, val, patchUpdates);

            if (!isDone)
            {
                GlobalMsg.SetMessage("Failed to change the name");
            }
        }
コード例 #5
0
        private bool ProcessUpdatePatch(PatchUpdate patchUpdate, ZipFile zip, string packageName)
        {
            string patchProcessWD = Path.Combine(WorkingDirectory, packageName, "PatchProcessing");

            if (Directory.Exists(patchProcessWD))
            {
                Directory.Delete(patchProcessWD, true);
            }
            if (!Directory.Exists(patchProcessWD))
            {
                Directory.CreateDirectory(patchProcessWD);
            }

            //locate via zip files list regex search
            List <ZipEntry> matchingZipEntries = new List <ZipEntry>();

            Logging.Editor("Checking for entries that match the regex '{0}'", LogLevel.Debug, patchUpdate.PatchesToUpdate);
            foreach (ZipEntry zipEntry in zip)
            {
                if (Regex.IsMatch(zipEntry.FileName, patchUpdate.PatchesToUpdate))
                {
                    Logging.Editor("Match found: {0}", LogLevel.Debug, zipEntry.FileName);
                    matchingZipEntries.Add(zipEntry);
                }
            }

            Dictionary <string, string> patchNames = new Dictionary <string, string>();

            //for each found, extract, load, xpath, search, replace, update
            foreach (ZipEntry entryMatch in matchingZipEntries)
            {
                //extract text
                //https://stackoverflow.com/a/16187809/3128017
                string xmlText = string.Empty;
                using (MemoryStream stream = new MemoryStream())
                {
                    entryMatch.Extract(stream);
                    stream.Position = 0;
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        xmlText = reader.ReadToEnd();
                    }
                }

                //load to document
                XmlDocument document = XmlUtils.LoadXmlDocument(xmlText, XmlLoadType.FromString);
                if (document == null)
                {
                    Logging.Editor("Failed to load xml document from zip file", LogLevel.Error);
                    return(false);
                }

                //do an xpath search
                XmlNodeList matchingNodes = XmlUtils.GetXmlNodesFromXPath(document, patchUpdate.XPath);
                int         matches       = 0;
                if (matchingNodes != null)
                {
                    matches = matchingNodes.Count;
                }
                Logging.Editor("Matching nodes: {0}", LogLevel.Debug, matches);
                if (matches == 0)
                {
                    Logging.Editor("0 matches, is this correct?", LogLevel.Warning);
                    continue;
                }

                //if regex search match, replace
                bool regexMatch = false;
                foreach (XmlNode matchNode in matchingNodes)
                {
                    string nodeText = matchNode.InnerText.Trim();
                    Logging.Editor("Checking for regex match: Text='{0}', Pattern='{1}'", LogLevel.Info, nodeText, patchUpdate.Search);
                    if (Regex.IsMatch(nodeText, patchUpdate.Search))
                    {
                        regexMatch = true;
                        Logging.Editor("Match found", LogLevel.Info);
                        nodeText = Regex.Replace(nodeText, patchUpdate.Search, patchUpdate.Replace);
                        Logging.Editor("Replaced to '{0}'", LogLevel.Info, nodeText);
                        matchNode.InnerText = nodeText;
                    }
                }

                if (!regexMatch)
                {
                    Logging.Editor("Regex was never matched, is this correct?", LogLevel.Warning);
                    continue;
                }

                //get just the name of the patch xml and save it to PatchProcessing dir
                string patchNameFromZip = Path.GetFileName(entryMatch.FileName);
                document.Save(Path.Combine(patchProcessWD, patchNameFromZip));
                patchNames.Add(entryMatch.FileName, patchNameFromZip);
            }

            //if any files exist in the directory, then they were modified and should be updated
            for (int i = 0; i < patchNames.Count; i++)
            {
                //https://stackoverflow.com/questions/40412340/c-sharp-dictionary-get-item-by-index
                string key   = patchNames.ElementAt(i).Key;   //zip entry string
                string value = patchNames.ElementAt(i).Value; //just filename in PatchProcessing folder
                Logging.Editor("Updating patch {0}", LogLevel.Info, value);
                zip.RemoveEntry(key);
                zip.AddFile(Path.Combine(patchProcessWD, value), "_patch");
            }

            return(true);
        }