예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int id)
        {
            ViewBag.MenuId = "menu-cantine";
            ViewBag.Datum  = DateTime.Now.ToString("dddd, d.M.yyyy");

            // Hier das Model aufbauen
            MensaViewModel model = new MensaViewModel();

            string daten = "";

            var url = $"http://openmensa.org/api/v2/canteens/{id}";

            // download mensa daten
            try
            {
                daten = GetResult(url);
            }
            catch (Exception ex)
            {
                daten         = ""; // Fehlerfall
                ViewBag.Datum = ex.Message.ToString();
                return(View(model));
            }

            JToken mensa = JToken.Parse(daten); // Text als JSON parsen

            model.Name    = (string)mensa["name"];
            model.Address = (string)mensa["address"];



            url = $"http://openmensa.org/api/v2/canteens/{id}/meals";

            // download mensa daten
            try
            {
                daten = GetResult(url);
            }
            catch (Exception ex)
            {
                daten         = ""; // Fehlerfall
                ViewBag.Datum = ex.Message.ToString();
            }

            // Wenn Inhalt in daten vorhanden
            if (daten != "")
            {
                JToken alleElemente = JToken.Parse(daten); // Text als JSON parsen

                // Neue Liste erstellen
                model.Tage = new List <MensaView_Tag>();

                // alle Elemente auslesen
                foreach (JToken Element in alleElemente)
                {
                    // Tage auslesen
                    var tag = new MensaView_Tag
                    {
                        date   = (string)Element["date"],
                        closed = (string)Element["closed"],
                        meals  = new List <MensaView_Meal>()
                    };
                    // Meals auslesen
                    foreach (JToken Meal in Element["meals"])
                    {
                        // Notizen
                        List <string> tmp_notes = new List <string>();
                        foreach (string value in Meal["notes"].Values())
                        {
                            tmp_notes.Add((string)value);
                        }

                        var meal = new MensaView_Meal
                        {
                            id       = (int)Meal["id"],
                            name     = (string)Meal["name"],
                            category = (string)Meal["category"],
                            prices   = new MensaView_Prices {
                                price_student   = parsePrice((string)Meal["prices"]["students"]),
                                price_employees = parsePrice((string)Meal["prices"]["employees"]),
                                price_others    = parsePrice((string)Meal["prices"]["others"]),
                                price_pupils    = parsePrice((string)Meal["prices"]["pupils"])
                            },
                            notes = tmp_notes
                        };
                        tag.meals.Add(meal);
                    }
                    model.Tage.Add(tag);
                }
            }

            // Vorschau

            JToken allElements = JRaw.Parse(daten); // Text als JSON parsen

            model.Vorschautage = new List <VorschauView_Tag>();

            foreach (JToken Element in allElements)
            {
                if (allElements.First != Element)
                {
                    var str  = (string)Element["date"];
                    var date = DateTime.Parse(str);



                    // Vorschautage auslesen
                    var Vorschautag = new VorschauView_Tag
                    {
                        Datum  = date.ToString("dd.MM.yyyy"),
                        closed = (bool)Element["closed"],
                        Name   = date.DayOfWeek.ToString(),

                        Gerichte = new List <VorschauView_Gerichte>()
                    };

                    // Meals auslesen
                    foreach (JToken gericht in Element["meals"])
                    {
                        // Notizen
                        List <string> tmp_notes = new List <string>();
                        foreach (string value in gericht["notes"].Values())
                        {
                            tmp_notes.Add((string)value);
                        }

                        var Tagesgericht = new VorschauView_Gerichte
                        {
                            Name             = (string)gericht["name"],
                            Kategorie        = (string)gericht["category"],
                            PreisStudent     = (string)gericht["prices"]["students"],
                            PreisMitarbeiter = (string)gericht["prices"]["employees"],
                            PreisGaeste      = (string)gericht["prices"]["others"],
                            Notizen          = tmp_notes
                        };


                        Vorschautag.Gerichte.Add(Tagesgericht);
                    }

                    model.Vorschautage.Add(Vorschautag);
                }
            }

            return(View(model));
        }
예제 #2
0
        /// <summary>
        /// Aktueller Mensaspeiseplan
        /// </summary>
        /// <returns></returns>
        public PartialViewResult Mensa(string location)
        {
            var model = new InfoscreenModel();


            // TODO: Abfrage des Mensaplans
            var m = new MensaViewModel();

            var daten = "";

            // download mensa daten
            // Speiseplan das aktuellen Tages
            try
            {
                if (location.Equals("FK 10") || location.Equals("FK 11"))
                {
                    daten = GetResult("http://openmensa.org/api/v2/canteens/142/meals");
                }
                else
                {
                    daten = GetResult("http://openmensa.org/api/v2/canteens/141/meals");
                }
            }
            catch (Exception ex)
            {
                daten         = ""; // Fehlerfall
                ViewBag.Datum = ex.Message.ToString();
            }

            // Wenn Inhalt in daten vorhanden
            if (daten != "")
            {
                var alleElemente = JToken.Parse(daten); // Text als JSON parsen

                // Neue Liste erstellen
                m.Tage = new List <MensaView_Tag>();

                // alle Elemente auslesen
                foreach (var Element in alleElemente)
                {
                    // Tage auslesen
                    var tag = new MensaView_Tag
                    {
                        date   = (string)Element["date"],
                        closed = (string)Element["closed"],
                        meals  = new List <MensaView_Meal>()
                    };

                    // Meals auslesen
                    foreach (var Meal in Element["meals"])
                    {
                        var tmp_notes = new List <string>();
                        foreach (string value in Meal["notes"].Values())
                        {
                            tmp_notes.Add((string)value);
                        }

                        var pri = new MensaView_Prices
                        {
                            price_student   = parsePrice((string)Meal["prices"]["students"]),
                            price_employees = parsePrice((string)Meal["prices"]["employees"]),
                            price_others    = parsePrice((string)Meal["prices"]["others"]),
                            price_pupils    = parsePrice((string)Meal["prices"]["pupils"])
                        };

                        var meal = new MensaView_Meal
                        {
                            id       = (int)Meal["id"],
                            name     = (string)Meal["name"],
                            category = (string)Meal["category"],
                            prices   = pri,
                            notes    = tmp_notes
                        };
                        tag.meals.Add(meal);
                    }

                    m.Tage.Add(tag);
                }

                model.SpeiseplanHeute = m.Tage.FirstOrDefault();
            }

            // wenn es keine Daten gibt, dann doch lieber MVG aufrufen
            if (model.SpeiseplanHeute == null)
            {
                return(MVG(location));
            }

            // 3. Model an den View übergeben
            return(PartialView("_Mensa", model));
        }