Exemple #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));
        }
        public async Task <IActionResult> InsertEvent(string calendarId, [FromBody] AddEventBindingModel model)
        {
            var response = await _eventService.InsertEvent(model, calendarId);

            if (response.ErrorOccurred)
            {
                return(BadRequest(response));
            }

            return(Ok(response));
        }
Exemple #3
0
        public async Task <ResponseDto <BaseModelDto> > InsertEvent(AddEventBindingModel model, string calendarId)
        {
            var response = new ResponseDto <BaseModelDto>();
            var service  = await _googleCalendarService.GetCalendarService();

            if (service == null)
            {
                response.Errors.Add(ServiceErrors.SERVICE_ERROR);
                return(response);
            }

            var dbCalendar = await _calendarRepository.GetById(calendarId);

            if (dbCalendar == null)
            {
                response.Errors.Add(ServiceErrors.CALENDAR_DOES_NOT_EXIST_ERROR);
                return(response);
            }

            if (model.StartDate >= model.EndDate)
            {
                response.Errors.Add(ServiceErrors.START_DATE_GREATER_ERROR);
                return(response);
            }

            var validDate = ValidateDateTimeForInsert(model.StartDate, model.EndDate, dbCalendar);

            if (!validDate)
            {
                response.Errors.Add(ServiceErrors.EVENT_WHILE_OTHERS_ERROR);
                return(response);
            }

            var newEvent = new Event
            {
                Summary = model.Summary,
                Start   = new EventDateTime
                {
                    TimeZone = timezone,
                    DateTime = model.StartDate
                },
                End = new EventDateTime
                {
                    TimeZone = timezone,
                    DateTime = model.EndDate
                }
            };

            EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
            var createdEvent = request.Execute();

            var dbEvent = new GEvent
            {
                Id          = createdEvent.Id,
                Summary     = newEvent.Summary,
                StartDate   = model.StartDate,
                EndDate     = model.EndDate,
                Description = newEvent.Description,
                Calendar    = dbCalendar
            };

            await _eventRepository.Insert(dbEvent);

            return(response);
        }