コード例 #1
0
        public async Task <IHttpActionResult> PostEvent(AddEventBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            string locCaption;

            using (var client = new HttpClient()) {
                var query    = String.Format("http://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}", model.Latitude, model.Longitude);
                var response = await client.GetAsync(query);

                var data = await response.Content.ReadAsStringAsync();

                try
                {
                    locCaption = (JObject.Parse(data))["results"][0]["formatted_address"].Value <string>();
                }
                catch
                {
                    locCaption = "unknown";
                }
            }
            var ev = new Event
            {
                UserId          = CurrentUser.UserId,
                Location        = DbGeography.FromText(String.Format("POINT({1} {0})", model.Latitude, model.Longitude)),
                LocationCaption = locCaption,
                Description     = model.Description,
                EventDate       = model.EventDate,
                DateCreate      = DateTime.UtcNow
            };

            ev = await eventsRepository.SaveInstance(ev);

            if (model.PhotoIds != null)
            {
                await Task.WhenAll(model.PhotoIds.Select(fid => photosRepo.SaveInstance(new Photo {
                    UserId     = ev.UserId,
                    AlbumId    = 0,
                    EntityType = PhotoEntityTypes.Event,
                    EntityId   = ev.EventId,
                    UserFileId = fid
                })));

                await dataRepo.Database.ExecuteSqlCommandAsync(
                    "UPDATE dbo.UserFiles SET State=@p0 WHERE UserFileId IN (@p1)",
                    UserFileState.Assigned,
                    String.Join(",", model.PhotoIds.ToArray()));
            }
            var rids = await gcmRepo.Objects.Select(r => r.RegId).ToArrayAsync();

            var gcmClient = new GCMClient();

            //await gcmClient.SendNotification(rids, new { Code = "NEW_EVENT", EventId = ev.EventId } as Object);
            GlobalHost.ConnectionManager.GetHubContext <EventsHub>().Clients.All.broadcastNewEvent(ev.EventId.ToString());

            return(Ok(ev.EventId));
        }