Пример #1
0
        /// <summary>
        /// Клик по кнопке применения фильтра для поиска
        /// </summary>
        private void filterApply_Button_Click(object sender, RoutedEventArgs e)
        {
            while (itemsGetter.IsBusy)
            {
            }

            filterApply_Button.Content   = "...";
            filterApply_Button.IsEnabled = false;

            KGCategory type  = (KGCategory)optionType.SelectedItem;
            decimal    price = (decimal)eventPrice.Value;

            KGItem.ORating rating = KGItem.Str2Rating(eventRating.SelectionBoxItem.ToString());
            int            radius = (int)eventRadius.Value;
            var            date   = eventDate.SelectedDate.Value.Date;

            itemsGetter.RunWorkerAsync(new Tuple <KGCategory, DateTime, decimal, KGItem.ORating, int>
                                           (type, date, price, rating, radius));
        }
Пример #2
0
        /// <summary>
        /// Получает список вариантов по заданному фильтру.
        /// </summary>
        /// <param name="filter">Фильтр для поиска</param>
        public static List <KGItem> GetOptions(KGFilter filter)
        {
            var results = new List <KGItem>();

            if (filter.Category.Type == KGCategory.CType.Event)
            {
                // Ищем событие...
                string raw = KGAPI.Get(
                    string.Format(
                        "https://kudago.com/public-api/v1.2/events/?actual_since={1}&actual_until={2}&fields=title,description,place,age_restriction,price,id&categories={0}&lat={3}&lon={4}&radius={5}&page_size=" + RESULTS_MAX,
                        filter.Category.Slug,
                        Utilites.Date2Unix(filter.Date),
                        Utilites.Date2Unix(filter.Date.AddHours(24)),
                        myLocation.X.ToString(new CultureInfo("en-US")),
                        myLocation.Y.ToString(new CultureInfo("en-US")),
                        filter.Radius * 1000));


                var a = (dynamic)JsonConvert.DeserializeObject(raw);
                foreach (var i in a.results)
                {
                    // Проверим цену мероприятия
                    bool    ok        = true;
                    decimal pre_price = 0;
                    decimal price     = 0;
                    // Разобьем на части строку и найдем все числовые значения.
                    string[] price_raw = ((string)i.price).Split(' ');
                    // Попробуем получить максимальное числовое представление цены и сравнить с фильтром.
                    foreach (var s in price_raw)
                    {
                        if (decimal.TryParse(s, out pre_price))
                        {
                            if (pre_price > price)
                            {
                                price = pre_price;
                            }
                            if (price > filter.Price)
                            {
                                ok = false;
                                break;
                            }
                        }
                    }

                    // Иначе вход свободный.
                    // Проверим возрастное ограничение мероприятия
                    string rat = (string)i.age_restriction ?? "0";
                    rat = rat.Replace("0", "0+"); // Для внутренней поддержки.
                    var ratI = KGItem.Str2Rating(rat);
                    // Проверяем возрастной рейтинг непосредственно (enum -> int compare)
                    if ((int)ratI > (int)filter.Rating)
                    {
                        ok = false;
                    }


                    //Проверяем.
                    if (ok)
                    {
                        KGItem t = new KGItem(new KGCategory(
                                                  (string)i.title,
                                                  KGCategory.CType.Event),
                                              filter.Date.Date,
                                              price,
                                              ratI,
                                              Utilites.StripHTML((string)i.description),
                                              new KGLocation(0D, 0D));
                        // Запишем ID варианта чтобы в последствии найти координаты по возможности
                        t.PlaceID = (int)i.place.id;
                        // Чтобы в последствии подгрузить комментарии
                        t.EventID = (int)i.id;

                        results.Add(t);
                    }
                }
            }

            if (filter.Category.Type == KGCategory.CType.Place)
            {
                // Ищем место...
                // https://kudago.com/public-api/v1.3/places/21000/?lang=&fields=coords
                string raw = KGAPI.Get(
                    string.Format(
                        "https://kudago.com/public-api/v1.2/places/?fields=title,description,coords,id&categories={0}&lat={1}&lon={2}&radius={3}&page_size=" + RESULTS_MAX,
                        filter.Category.Slug,
                        myLocation.X.ToString(new CultureInfo("en-US")),
                        myLocation.Y.ToString(new CultureInfo("en-US")),
                        filter.Radius * 1000));

                var a = (dynamic)JsonConvert.DeserializeObject(raw);
                foreach (var i in a.results)
                {
                    KGItem t = new KGItem(new KGCategory(
                                              (string)i.title,
                                              KGCategory.CType.Place),
                                          filter.Date.Date,
                                          0M,
                                          KGItem.ORating.R0,
                                          Utilites.StripHTML((string)i.description),
                                          new KGLocation((double)i.coords.lat, (double)i.coords.lon));
                    // Чтобы в последствии подгрузить комментарии
                    t.PlaceID = (int)i.id;
                    results.Add(t);
                }
            }
            if (filter.Category.Type == KGCategory.CType.Event)
            {
                // Попробуем отыскать координаты мест
                int[] ids = results.Select(x => x.PlaceID).ToArray();
                ids = ids.Distinct().ToArray();

                string query = string.Join(",", ids);
                string n     = KGAPI.Get(string.Format("https://kudago.com/public-api/v1.3/places/?fields=id,coords&ids={0}",
                                                       query));
                var na = (dynamic)JsonConvert.DeserializeObject(n); // As Object
                Dictionary <int, KGLocation> id2loc = new Dictionary <int, KGLocation>();
                foreach (var tr in na.results)
                {
                    var crd = tr.coords;  // As Coord Object
                    int id  = (int)tr.id; // As Id Object
                    id2loc[id] = new KGLocation((double)crd.lat, (double)crd.lon);
                }
                results.ForEach(x =>
                {
                    if (id2loc.Keys.Contains(x.PlaceID))
                    {
                        x.Location = id2loc[x.PlaceID];
                    }
                });
            }
            return(results);
        }
Пример #3
0
 public Comments(KGItem item)
 {
     InitializeComponent();
     this.item  = item;
     this.Title = string.Format("Комментарии про [{0}]", item.Type.Name);
 }