Esempio n. 1
0
        private static ToplistBlock ConvertXElementToBlock(XElement xmlBlock)
        {
            xmlBlock.ThrowIfNull(nameof(xmlBlock));

            string title  = XDocumentParser.GetAttributeValue(xmlBlock, nameof(ToplistBlock.Title));
            int    number = XDocumentParser.GetAttributeValue <int>(xmlBlock,
                                                                    nameof(ToplistBlock.Number));

            XElement?itemsXml = XDocumentParser.FindSubelement(xmlBlock,
                                                               nameof(ToplistBlock.Items));

            if (itemsXml is null)
            {
                throw new ArgumentException(
                          "Invalid strcuture of XML document: cannot find toplist items block.",
                          nameof(xmlBlock)
                          );
            }

            var result = new ToplistBlock(title, number);

            result.UpdateItems(
                XDocumentParser.FindSubelements(itemsXml)
                .Select(xmlItem => ConvertXElementToItem(xmlItem, result))
                .ToList()
                );

            return(result);
        }
Esempio n. 2
0
        public override bool AddBlock(ToplistBlock block)
        {
            block.ThrowIfNull(nameof(block));

            int insertIndex = CalculateInsertIndex(block.Number);

            Blocks.Insert(insertIndex, block);

            return(true);
        }
Esempio n. 3
0
        private static ToplistItem ConvertXElementToItem(XElement xmlItem, ToplistBlock parentBlock)
        {
            xmlItem.ThrowIfNull(nameof(xmlItem));

            string name           = XDocumentParser.GetAttributeValue(xmlItem, nameof(ToplistItem.Name));
            int    parsedPosition = XDocumentParser.GetAttributeValue <int>(
                xmlItem, nameof(ToplistItem.Position)
                );
            int?position = parsedPosition == -1 ? (int?)null : parsedPosition;

            return(new ToplistItem(name, position, parentBlock));
        }
Esempio n. 4
0
        private static XElement ConvertBlockToXElement(ToplistBlock block)
        {
            block.ThrowIfNull(nameof(block));

            return(new XElement("Block",
                                new XAttribute(nameof(ToplistBlock.Title), block.Title),
                                new XAttribute(nameof(ToplistBlock.Number), block.Number),

                                new XElement(nameof(block.Items),
                                             block.Items.Select(ConvertItemToXElement).ToArray()
                                             )
                                ));
        }
Esempio n. 5
0
        public override bool RemoveBlock(ToplistBlock block)
        {
            block.ThrowIfNull(nameof(block));

            return(Blocks.Remove(block));
        }
Esempio n. 6
0
        public void RemoveBlock(ToplistBlock block)
        {
            block.ThrowIfNull(nameof(block));

            _toplist.RemoveBlock(block);
        }