TestController.cs 8.09 KB
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System;
using System.Linq;
using System.Collections.Generic;
using MapsDb.Models;

namespace Maps.Controllers
{
    public class TestController : Controller
    {
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            Curl curl = new Curl();
            string result = await curl.SendRequest();
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(result);
            XmlElement xRoot = xDoc.DocumentElement;
            if (xRoot.Name != "osm") {
                return this.Content(result, "text/xml");
            }
            int nodes, ways, relations;
            nodes = ways = relations = 0;
            foreach (XmlNode child in xRoot) {
                switch(child.Name) {
                    case "node":
                        nodes++;
                        break;
                    case "way":
                        ways++;
                        break;
                    case "relation":
                        relations++;
                        break;
                }
            }
            string answer = "";
            answer += "Nodes count: " + nodes + "\n";
            answer += "Ways count: " + ways + "\n";
            answer += "Relations count: " + relations;
            return this.Content(answer);
        }

        [HttpGet]
        public async Task<IActionResult> Create()
        {
            string filename = "base.xml";
            int trackId = 1;
            XDocument doc = new XDocument(
                new XElement("library",
                    new XElement("track",
                        new XAttribute("id", trackId++),
                        new XAttribute("genre", "Rap"),
                        new XAttribute("time", "3:24"),
                        new XElement("name", "Who We Be RMX (feat. 2Pac)"),
                        new XElement("artist", "DMX"),
                        new XElement("album", "The Dogz Mixtape: Who's Next?!")
                    ),
                    new XElement("track",
                        new XAttribute("id", trackId++),
                        new XAttribute("genre", "Rap"),
                        new XAttribute("time", "5:06"),
                        new XElement("name", "Angel (ft. Regina Bell)"),
                        new XElement("artist", "DMX"),
                        new XElement("album", "...And Then There Was X")
                    ),
                    new XElement("track",
                        new XAttribute("id", trackId++),
                        new XAttribute("genre", "Break Beat"),
                        new XAttribute("time", "6:16"),
                        new XElement("name", "Dreaming Your Dreams"),
                        new XElement("artist", "Hybrid"),
                        new XElement("album", "Wide Angle")
                    ),
                    new XElement("track",
                        new XAttribute("id", trackId++),
                        new XAttribute("genre", "Break Beat"),
                        new XAttribute("time", "9:38"),
                        new XElement("name", "Finished Symphony"),
                        new XElement("artist", "Hybrid"),
                        new XElement("album", "Wide Angle")
                    )
                )
            );
            FileStream stream = new FileStream(filename, FileMode.Append);
            doc.Save(stream);
            return Content(doc.ToString(), "text/xml");
        }

        public async Task<IActionResult> Update()
        {
            string filename = "base.xml";
            FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
            XDocument xDoc = XDocument.Load(stream);
            stream.Dispose();
            stream = new FileStream(filename, FileMode.Truncate, FileAccess.ReadWrite);
            foreach (XElement el in xDoc.Root.Elements("track"))
            {
                int id = Int32.Parse(el.Attribute("id").Value);
                    el.SetAttributeValue("id", id + 5);
            }
            xDoc.Save(stream);
            return Content(xDoc.ToString(), "text/xml");
        }

        public async Task<IActionResult> Add()
        {
            string filename = "base.xml";
            FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
            XDocument doc = XDocument.Load(stream);
            stream.Dispose();
            stream = new FileStream(filename, FileMode.Truncate, FileAccess.ReadWrite);            
            int maxId = doc.Root.Elements("track").Max(t => Int32.Parse(t.Attribute("id").Value));
            XElement track = new XElement("track",
                new XAttribute("id", ++maxId),
                new XAttribute("genre", "Break Beat"),
                new XAttribute("time", "5:35"),
                new XElement("name", "Higher Than A Skyscraper"),
                new XElement("artist", "Hybrid"),
                new XElement("album", "Morning Sci-Fi")
            );
            doc.Root.Add(track);
            doc.Save(stream);
            return Content(doc.ToString(), "text/xml");
        }

        public async Task<IActionResult> Delete()
        {
            string filename = "base.xml";
            FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
            XDocument doc = XDocument.Load(stream);
            stream.Dispose();
            stream = new FileStream(filename, FileMode.Truncate, FileAccess.ReadWrite);
            // IEnumerable<XElement> tracks = doc.Root.Descendants("track").Where(
            //     t => t.Element("artist").Value == "Hybrid"
            // ).ToList();
            // foreach (XElement t in tracks)
            // {
            //     t.Remove();
            // }
            doc.Root.Descendants("track").Where(
                t => t.Element("artist").Value == "Hybrid"
            ).Remove();
            doc.Save(stream);
            return Content(doc.ToString(), "text/xml");            
        }

        [HttpGet]
        public async Task<IActionResult> Relation()
        {
            Curl curl = new Curl();
            string result = await curl.GetRelation(2143112);
            XDocument doc = XDocument.Parse(result);
            IEnumerable<XElement> relations = doc.Root.Elements("relation");
            List<int> relation_ids = new List<int>();
            foreach (XElement relation in relations) {
                XElement tag = (from t in relation.Elements("tag")
                                where t.Attribute("k").Value == "ref" select t).SingleOrDefault();
                if (tag != null) {
                    string name = tag.Attribute("v").Value;
                    if (name.Length == 4) {
                        relation_ids.Add(Int32.Parse(relation.Attribute("id").Value));
                    }
                }
            }
            List<int> answer = new List<int>();
            foreach (int relation_id in relation_ids) {
                string relationXml = await curl.GetRelation(relation_id);
                answer.Add(this.SaveRelation(relationXml));
            }
            // IEnumerable<XElement> tags = from t in doc.Root.Elements("relation").Elements("tag")
            //                                 where t.Attribute("k").Value == "ref" select t;
            // foreach (XElement tag in tags) {
            //     string value = tag.Attribute("v").Value;
            //     System.Console.WriteLine(tag.ToString());
            //     if(value.Length == 4) {
            //         answer.Add(value);
            //     }
            // }
            // answer.Sort();
            return this.Content(string.Join("\n", answer));
        }

        protected int SaveRelation(string relation)
        {
            XDocument doc = XDocument.Parse(relation);
            XElement tag = (from t in doc.Root.Element("relation").Elements("tag")
                            where t.Attribute("k").Value == "ref" select t).Single();
            int index = Int32.Parse(tag.Attribute("v").Value.Substring(2));
            return index;
        }
    }
}