Exemple #1
0
        public async Task Events(HttpRequest req)
        {
            req.SetContentType(ContentType.Json);

            // Check if we have enough parameters for the search
            if (!req.HasGET("address") && !req.HasGET("lat", "lng"))
            {
                req.SetStatusCode(HttpStatusCode.BadRequest);
                await req.Close();

                return;
            }

            int      distance = int.Parse(req.GET("distance", "1000"));
            Location location;

            if (req.HasGET("lat", "lng"))
            {
                location = new Location(double.Parse(req.GET("lat")), double.Parse(req.GET("lng")));
            }
            else
            {
                location = await Google.Geocode(req.GET("address"));
            }

            if (location == null || distance > 50)
            {
                req.SetStatusCode(HttpStatusCode.BadRequest);
                await req.Close();

                return;
            }

            SelectQuery <Event> query = new SelectQuery <Event>();

            // Filter by distance
            query.Where("HAVERSINE(lat, lng, @loc_lat, @loc_lng) < @distance")
            .AddParameter("@loc_lat", location.Latitude)
            .AddParameter("@loc_lng", location.Longitude)
            .AddParameter("@distance", distance * 1000);

            // Filter by price
            if (req.HasGET("price"))
            {
                query.Where("price", WhereRelation.UpTo, req.GET("price"));
            }

            // Filter by age
            int age = 0;

            if (req.HasGET("age") && int.TryParse(req.GET("age"), out age))
            {
                query.Where("age_min", WhereRelation.UpTo, age)
                .Where("age_max", WhereRelation.AtLeast, age);
            }

            // Filter by duration
            if (req.HasGET("duration_min"))
            {
                query.Where("duration", WhereRelation.AtLeast, req.GET("duration_min"));
            }
            if (req.HasGET("duration_max"))
            {
                query.Where("duration", WhereRelation.UpTo, req.GET("duration_max"));
            }

            // Filter by gender
            if (req.HasGET("gender") && (req.GET("gender") == "male" || req.GET("gender") == "female"))
            {
                query.Where(
                    new WhereClause("genders", req.GET("gender"))
                    | new WhereClause("genders", 2)
                    );
            }

            // Perform query and build the response object
            JArray       arr    = new JArray();
            List <Event> events = await Program.MySql().Execute(query);

            foreach (Event evt in events)
            {
                DateTime minDate = DateTime.Now;
                DateTime maxDate = DateTime.MaxValue;

                if (req.HasGET("min_date") && !string.IsNullOrWhiteSpace(req.GET("min_date")))
                {
                    DateTime.TryParse(req.GET("min_date"), out minDate);
                }

                if (req.HasGET("max_date") && !string.IsNullOrWhiteSpace(req.GET("max_date")))
                {
                    DateTime.TryParse(req.GET("max_date"), out maxDate);
                }

                int categoryId = -1;

                if (req.HasGET("category") && !string.IsNullOrWhiteSpace(req.GET("category")))
                {
                    int.TryParse(req.GET("category"), out categoryId);
                }

                // DateTime.TryParse(req.GET("max_date"), out maxDate);


                // int.TryParse(req.GET("category"), out categoryId);

                if (await evt.HappensBetween(minDate, maxDate) &&
                    (categoryId < 0 || await evt.HasCategories(categoryId)))
                {
                    arr.Add(await evt.SerializeWithScheduled());
                }
            }

            if (Options.Randomized)
            {
                Chance c = new Chance();
                for (int i = 0; i < 50; i++)
                {
                    int   id  = c.Natural();
                    Event evt = Pleisure.Event.Random(id, location.Latitude, location.Longitude, distance);
                    arr.Add(await evt.SerializeWithScheduled());
                }
            }

            JToken response = JToken.FromObject(new
            {
                center  = location.Serialize(),
                results = arr
            });

            await req.Write(response.ToString());

            await req.Close();
        }
Exemple #2
0
        public async Task CreateEvent(HttpRequest req)
        {
            UserSession session = req.Session as UserSession;

            User user = await session.GetUser();

            if (user == null)
            {
                req.SetStatusCode(HttpStatusCode.Unauthorized);
                await req.Close();

                return;
            }

            int eventId = Program.Chance().Natural();

            if (!await req.HasPOST("title", "description", "price", "duration", "address",
                                   "datetime", "recurrence"))
            {
                req.SetStatusCode(HttpStatusCode.BadRequest);
                await req.Close();

                return;
            }

            string title = await req.POST("title");

            string description = await req.POST("description");

            int    price;
            int    duration;
            string address = await req.POST("address");

            int      genders  = 2;
            Location location = await Google.Geocode(address);

            int age_min = 4;
            int age_max = 17;


            string scheduledTime = await req.POST("datetime");

            string recurrence = await req.POST("recurrence");


            int.TryParse(await req.POST("genders"), out genders);
            int.TryParse(await req.POST("age_min"), out age_min);
            int.TryParse(await req.POST("age_max"), out age_max);


            if (!int.TryParse(await req.POST("price"), out price) ||
                !int.TryParse(await req.POST("duration"), out duration))
            {
                req.SetStatusCode(HttpStatusCode.BadRequest);
                await req.Close();

                return;
            }

            // First try scheduling
            if (location == null ||
                !await ScheduleEvent(eventId, scheduledTime, recurrence))
            {
                if (location == null)
                {
                    Console.WriteLine("Bad event location");
                }
                req.SetStatusCode(HttpStatusCode.NotAcceptable);
                await req.Close();

                return;
            }


            if (await req.HasPOST("image"))
            {
                MemoryStream imgStream = await req.GetContentData("image");

                string directory = Options.StoragePath("eventimg");
                string filePath  = string.Format("{0}/{1}.png", directory, eventId);
                Directory.CreateDirectory(directory);

                byte[] buffer = new byte[imgStream.Length];
                await imgStream.ReadAsync(buffer, 0, buffer.Length);

                using (FileStream writer = File.OpenWrite(filePath))
                {
                    await writer.WriteAsync(buffer, 0, buffer.Length);

                    await writer.FlushAsync();
                }
            }

            InsertQuery query = new InsertQuery("events");

            query.Value("organizer_id", user.ID)
            .Value("event_id", eventId)
            .Value("title", title)
            .Value("description", description)
            .Value("price", price)
            .Value("lat", location.Latitude)
            .Value("lng", location.Longitude)
            .Value("address", address)
            .Value("duration", duration)
            .Value("age_min", age_min)
            .Value("age_max", age_max)
            .Value("genders", genders);

            NonQueryResult result = await Program.MySql().ExecuteNonQuery(query);


            int category;

            if (await req.HasPOST("category") && int.TryParse(await req.POST("category"), out category))
            {
                InsertQuery categoryQuery = new InsertQuery("event_categories");
                categoryQuery.Value("event_id", eventId)
                .Value("category_id", category);

                await Program.MySql().ExecuteNonQuery(categoryQuery);
            }

            await req.Redirect("/event/" + eventId);
        }