Beispiel #1
0
        public static void ImportEntitiesJson(MetroDbContext context, string baseDir = @"..\..\..\..\Datasets\")
        {
            const string exportDir = @"..\..\..\ImportResults\";

            string json;

            using (var client = new WebClient())
            {
                json = client.DownloadString("http://drone.sumc.bg/api/v1/metro/all");
            }

            //var stations = DataProcessor.Deserializer.ImportJsonStations(context, json);
            var stations = DataProcessor.Deserializer.ImportJsonStations(context, File.ReadAllText(baseDir + "metro_stations.json"));

            PrintAndExportEntityToFile(stations, exportDir + "Stations_JSON_Import_Log.txt");

            using (var client = new WebClient())
            {
                json = client.DownloadString("http://drone.sumc.bg/api/v1/routes/changes");
            }

            var routes = DataProcessor.Deserializer.ImportJsonRoutes(context, File.ReadAllText(baseDir + "routes.json"));

            PrintAndExportEntityToFile(stations, exportDir + "Routes_JSON_Import_Log.txt");


            //var employees = DataProcessor.Deserializer.ImportEmployees(context, File.ReadAllText(baseDir + "employees.json"));
            //PrintAndExportEntityToFile(employees, exportDir + "Employees.txt");


            //var orders = DataProcessor.Deserializer.ImportOrders(context, File.ReadAllText(baseDir + "orders.xml"));
            //PrintAndExportEntityToFile(orders, exportDir + "Orders.txt");
        }
Beispiel #2
0
        //public static string ExportStantionXml(MetroDbContext context)
        //{
        //    // Export to XML
        //    var metroStations = context.Stations
        //        .Select(e => new
        //        {
        //            id = e.StantionId,
        //            route_id = e.RouteId,
        //            code = e.Code,
        //            point_id = e.PointId,
        //            name = e.Name,
        //            latitude = e.Latitude,
        //            longitude = e.Longitude,
        //        }
        //        )
        //        .OrderBy(o => o.id)
        //        .ToList();


        //    var xmlDoc = new XDocument(new XElement("Stantions"));

        //    foreach (var s in metroStations)
        //    {

        //        var currentStantion = new XElement("Stantion",
        //                                new XElement("id", s.id),
        //                                new XElement("route_id", s.route_id),
        //                                new XElement("code", s.code),
        //                                new XElement("point_id", s.point_id),
        //                                new XElement("name", s.name),
        //                                new XElement("latitude", s.latitude),
        //                                new XElement("longitude", s.longitude)
        //            );
        //        xmlDoc.Root.Add(currentStantion);
        //    }

        //    var result = xmlDoc.ToString();

        //    return result;

        //    //return xmlDoc.ToString();
        //    //throw new NotImplementedException();
        //}

        public static string ExportStantionXml(MetroDbContext context)
        {
            //Export XML using DTO - WIP!!!

            var metroStations = context.Stations
                                .Select(e => new StationDtoExpXml()
            {
                id        = e.StantionId,
                route_id  = e.RouteId,
                code      = e.Code,
                point_id  = e.PointId,
                name      = e.Name,
                latitude  = e.Latitude,
                longitude = e.Longitude,
            }
                                        )
                                .OrderBy(o => o.id)
                                .ToArray();

            var sb = new StringBuilder();

            var serializer = new XmlSerializer(typeof(StationDtoExpXml[]), new XmlRootAttribute("Stantions"));

            serializer.Serialize(new StringWriter(sb), metroStations, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));

            var result = sb.ToString();

            return(result);

            //throw new NotImplementedException();
        }
Beispiel #3
0
        //public static string ExportStantionJson(MetroDbContext context)
        //{
        //    //Export JSON
        //    var metroStation = context.Stations
        //        .Select(e => new
        //        {
        //            id = e.StantionId,
        //            route_id = e.RouteId,
        //            code = e.Code,
        //            point_id = e.PointId,
        //            name = e.Name,
        //            latitude = e.Latitude,
        //            longitude = e.Longitude,
        //        }
        //        )
        //        .OrderBy(o => o.id)
        //        .ToArray();

        //    var result = JsonConvert.SerializeObject(metroStation, Formatting.Indented);

        //    return result;

        //    //throw new NotImplementedException();
        //}

        public static string ExportStantionJson(MetroDbContext context)
        {
            // Export JSON with DTO
            // Include AutMapper?
            var metroStation = context.Stations
                               .Select(e => new StationDtoExpJson()
            {
                id        = e.StantionId,
                route_id  = e.RouteId,
                code      = e.Code,
                point_id  = e.PointId,
                name      = e.Name,
                latitude  = e.Latitude,
                longitude = e.Longitude,
            }
                                       )
                               .OrderBy(o => o.id)
                               .ToArray();

            var result = JsonConvert.SerializeObject(metroStation, Formatting.Indented);

            return(result);

            //throw new NotImplementedException();
        }
Beispiel #4
0
 internal static string ImportJSONData()
 {
     using (var db = new MetroDbContext())
     {
         ImportEntitiesJson(db);
     }
     return("JSON Data Imported");
 }
Beispiel #5
0
 internal static string ExportXmlData()
 {
     using (var db = new MetroDbContext())
     {
         ExportEntitiesXml(db);
     }
     return("XML Data Exported");
 }
Beispiel #6
0
        public static void ImportEntitiesXml(MetroDbContext context, string baseDir = @"..\..\..\..\Datasets\")
        {
            const string exportDir = @"..\..\..\ImportResults\";

            var station = DataProcessor.Deserializer.ImportXmlStantions(context, File.ReadAllText(baseDir + "stations.xml"));

            PrintAndExportEntityToFile(station, exportDir + "Stations_XML_Import_Log.txt");
        }
Beispiel #7
0
        private static void ExportEntitiesXml(MetroDbContext context)
        {
            const string exportDir = @"..\..\..\ExportResults\";

            var xmlOutput = DataProcessor.Serializer.ExportStantionXml(context);

            Console.WriteLine(xmlOutput);
            File.WriteAllText(exportDir + "Stations.xml", xmlOutput);
        }
Beispiel #8
0
 internal static string ShowPoints()
 {
     using (var db = new MetroDbContext())
     {
         QueryPoints(db);
     }
     Console.ReadKey();
     return("Show Points");
 }
Beispiel #9
0
        private static void QueryRoutes(MetroDbContext context)
        {
            var routes = context.Routes;

            Console.WriteLine($"Id | Type| ExtId| RId  | LId | LName| Route Name");
            Console.WriteLine($"----------------------------------------------------------");
            foreach (var route in routes)
            {
                Console.WriteLine($"{route.Id.ToString("D2")} | {route.Type.ToString("D3")} | {route.ExtId.ToString("D2")} | {route.RouteId.ToString("D4")} | {route.LineId.ToString("D3")} | {route.LineName.ToString("D4")} | {route.RouteName}");
            }
        }
Beispiel #10
0
        private static void QueryStations(MetroDbContext context)
        {
            var stations = context.Stations;

            Console.WriteLine($"Id | Code | Latitude  | Longitude | PId | RId | SId | Name");
            Console.WriteLine($"----------------------------------------------------------");
            foreach (var station in stations)
            {
                Console.WriteLine($"{station.Id.ToString("D2")} | {station.Code} | {station.Latitude} | {station.Longitude} | {station.PointId} | {station.RouteId.ToString("D3")} | {station.StantionId.ToString("D3")} | {station.Name}");
            }
        }
Beispiel #11
0
            internal static string ResetDatabase()
            {
                using (var db = new MetroDbContext())
                {
                    db.Database.EnsureDeleted();

                    //db.Database.Migrate();

                    db.Database.EnsureCreated();
                }

                return("Database Reset Successfull");
            }
Beispiel #12
0
        private static void QueryPoints(MetroDbContext context)
        {
            var points = context.Points;

            Console.WriteLine($"Id | StopCode| VehicleType| Stop  | StopName");
            Console.WriteLine($"----------------------------------------------------------");
            foreach (var point in points)
            {
                if (point.StopCode != 0)
                {
                    Console.WriteLine($"{point.Id.ToString("D3")}|   {point.StopCode.ToString("D4")}  |      {point.VehicleType}      | {point.stop.ToString("D5")}| {point.StopName}");
                }
            }
        }
Beispiel #13
0
        private static void ExportEntitiesJson(MetroDbContext context)
        {
            const string exportDir = @"..\..\..\ExportResults\";

            var jsonOutput = DataProcessor.Serializer.ExportStantionJson(context);

            Console.WriteLine(jsonOutput);
            File.WriteAllText(exportDir + "Stations.json", jsonOutput);

            //    var jsonOutput = DataProcessor.Serializer.ExportOrdersByEmployee(context, "Avery Rush", "ToGo");
            //    Console.WriteLine(jsonOutput);
            //    File.WriteAllText(exportDir + "OrdersByEmployee.json", jsonOutput);

            //    var xmlOutput = DataProcessor.Serializer.ExportCategoryStatistics(context, "Chicken,Drinks,Toys");
            //    Console.WriteLine(xmlOutput);
            //    File.WriteAllText(exportDir + "CategoryStatistics.xml", xmlOutput);
            //}
        }
Beispiel #14
0
        public static string ImportXmlStantions(MetroDbContext context, string xmlString)
        {
            // Import Stations from XML
            StringBuilder sb = new StringBuilder();

            var xmlDoc = XDocument.Parse(xmlString);

            var elements = xmlDoc.Root.Elements();

            var stantions = new List <Station>();

            foreach (var e in elements)
            {
                int     stantionId = int.Parse(e.Element("id").Value);
                int     route_id   = int.Parse(e.Element("route_id").Value);
                int     code       = int.Parse(e.Element("code").Value);
                int     point_id   = int.Parse(e.Element("point_id").Value);
                string  name       = e.Element("name").Value;
                decimal latitude   = decimal.Parse(e.Element("latitude").Value);
                decimal longitude  = decimal.Parse(e.Element("longitude").Value);

                var stantion = new Station()
                {
                    StantionId = stantionId,
                    RouteId    = route_id,
                    Code       = code,
                    PointId    = point_id,
                    Name       = name,
                    Latitude   = latitude,
                    Longitude  = longitude
                };

                stantions.Add(stantion);
                sb.AppendLine(string.Format(SuccessMessage, stantion.Name));
            }

            context.Stations.AddRange(stantions);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
Beispiel #15
0
        public static string ImportJsonStations(MetroDbContext context, string jsonString)
        {
            //Import Stations from JSON with DTO and AutoMapper
            StringBuilder sb = new StringBuilder();

            var deserializedStations = JsonConvert.DeserializeObject <StationDtoImp[]>(jsonString);

            var validStations = new List <Station>();

            foreach (var stationDto in deserializedStations)
            {
                if (!IsValid(stationDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var stationAlreadyExists = validStations.Any(s => s.Name == stationDto.Name);

                if (stationAlreadyExists)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var station = Mapper.Map <Station>(stationDto);

                validStations.Add(station);

                sb.AppendLine(string.Format(SuccessMessage, stationDto.Name));
                //Console.WriteLine($"{stationDto.Name} {stationDto.Longitude} {stationDto.Latitude}");
            }

            context.Stations.AddRange(validStations);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
Beispiel #16
0
        public static string ImportJsonRoutes(MetroDbContext context, string jsonString)
        {
            //Import Stations from JSON with DTO
            StringBuilder sb = new StringBuilder();

            //var deserializedRoutes = JsonConvert.DeserializeObject<RouteDtoImp[]>(jsonString);
            var deserializedRoutes = JsonConvert.DeserializeObject <List <RouteDtoImp> >(jsonString);

            var validRoutes = new List <Route>();

            foreach (var routeDto in deserializedRoutes)
            {
                if (!IsValid(routeDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var routeAlreadyExists = validRoutes.Any(s => s.RouteName == routeDto.RouteName);

                if (routeAlreadyExists)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                //var route = Mapper.Map<Route>(routeDto);

                var impPoints = routeDto.Points;

                var validPoints = new List <Point>();

                foreach (var pointDtoImp in impPoints)
                {
                    var PointToAdd = new Point
                    {
                        stop        = pointDtoImp.stop,
                        StopCode    = pointDtoImp.StopCode,
                        StopName    = pointDtoImp.StopName,
                        Latitude    = pointDtoImp.Latitude,
                        Longitude   = pointDtoImp.Longitude,
                        VehicleType = pointDtoImp.VehicleType,
                    };
                    validPoints.Add(PointToAdd);
                }


                var route = new Route
                {
                    Id        = routeDto.Id,
                    RouteId   = routeDto.RouteId,
                    Type      = routeDto.Type,
                    RouteName = routeDto.RouteName,
                    LineId    = routeDto.LineId,
                    ExtId     = routeDto.ExtId,
                    LineName  = routeDto.LineName,
                    Points    = validPoints
                };



                validRoutes.Add(route);

                sb.AppendLine(string.Format(SuccessMessage, routeDto.RouteName));
                //Console.WriteLine($"{stationDto.Name} {stationDto.Longitude} {stationDto.Latitude}");
            }

            context.Routes.AddRange(validRoutes);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
Beispiel #17
0
 private static void ResetDatabase(MetroDbContext context)
 {
     context.Database.EnsureDeleted();
     context.Database.EnsureCreated();
 }
Beispiel #18
0
        public static void Main(string[] args)
        {
            var context = new MetroDbContext();

            //ResetDatabase(context);

            //Console.WriteLine("Database Reset.");

            Mapper.Initialize(cfg => cfg.AddProfile <MetroProfile>());

            //ImportEntities(context);

            //ExportEntities(context);

            //BonusTask(context);

            string wellcome = File.ReadAllText("..\\..\\..\\Menu.txt");

            while (true)
            {
                Console.WriteLine(wellcome);
                Console.Write("Make your choice: ");

                string command = Console.ReadLine();
                Console.WriteLine(command != "0" ? "Loading..." : string.Empty);
                string output = string.Empty;

                try
                {
                    switch (command)
                    {
                    case "0":
                        Environment.Exit(0);
                        break;

                    case "1":
                        output = DbInitializer.ResetDatabase();
                        break;

                    case "2":
                        output = DbInitializer.ImportJSONData();
                        break;

                    case "3":
                        output = DbInitializer.ImportXMLData();
                        break;

                    case "4":
                        output = DbInitializer.ExportJsonData();
                        break;

                    case "5":
                        output = DbInitializer.ExportXmlData();
                        break;

                    case "6":
                        output = DbInitializer.ShowStations();
                        break;

                    case "7":
                        output = DbInitializer.ShowRoutes();
                        break;

                    case "8":
                        output = DbInitializer.ShowPoints();
                        break;

                    default:
                        throw new InvalidOperationException("Invalid command!");
                    }

                    Console.Clear();
                    Console.WriteLine(output);
                    Console.WriteLine();
                    Console.WriteLine(new string('=', 15));
                }
                catch (Exception ex)
                {
                    Console.Clear();
                    Console.WriteLine(ex.GetBaseException().Message);
                }
            }
        }