예제 #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);
        }
예제 #2
0
        public override bool AddBlock(ToplistBlock block)
        {
            block.ThrowIfNull(nameof(block));

            int insertIndex = CalculateInsertIndex(block.Number);

            Blocks.Insert(insertIndex, block);

            return(true);
        }
예제 #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));
        }
예제 #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()
                                             )
                                ));
        }
예제 #5
0
        public override bool RemoveBlock(ToplistBlock block)
        {
            block.ThrowIfNull(nameof(block));

            return(Blocks.Remove(block));
        }
예제 #6
0
        public void RemoveBlock(ToplistBlock block)
        {
            block.ThrowIfNull(nameof(block));

            _toplist.RemoveBlock(block);
        }