コード例 #1
0
        public async Task HandleAsync(IUpdateContext context, UpdateDelegate next, CancellationToken cancellationToken)
        {
            // ToDo: Remove keyboard if that was set in /bus command

            var userchat = context.Update.ToUserchat();

            float latitude, longitude;

            if (context.Update.Message.Location != null)
            {
                latitude  = context.Update.Message.Location.Latitude;
                longitude = context.Update.Message.Location.Longitude;
            }
            else if (context.Update.Message.Text != null)
            {
                var result = _locationService.TryParseLocation(context.Update.Message.Text);
                latitude  = result.Lat;
                longitude = result.Lon;
            }
            else
            {
                throw new InvalidOperationException();
            }

            var locationContext = new UserLocationContext
            {
                Latitude          = latitude,
                Longitude         = longitude,
                LocationMessageId = context.Update.Message.MessageId,
            };
            await _cache.SetLocationAsync(userchat, locationContext, cancellationToken)
            .ConfigureAwait(false);

            context.Items[nameof(UserLocationContext)] = locationContext;

            await next(context, cancellationToken).ConfigureAwait(false);

            // ToDo add location to cache

//            var locationTuple = _locationService.TryParseLocation(context.Update);
//
//            if (locationTuple.Successful)
//            {
//                _locationService.AddLocationToCache(userchat, locationTuple.Location);
//
//                await _predictionsManager
//                    .TryReplyWithPredictionsAsync(context.Bot, userchat, context.Update.Message.MessageId)
//                    .ConfigureAwait(false);
//            }
//            else
//            {
//                // todo : if saved location available, offer it as keyboard
//                await context.Bot.Client.SendTextMessageAsync(
//                    context.Update.Message.Chat.Id,
//                    "_Invalid location!_",
//                    ParseMode.Markdown,
//                    replyToMessageId: context.Update.Message.MessageId
//                ).ConfigureAwait(false);
//            }
        }
コード例 #2
0
 public IHttpActionResult UserLocation(UserLocationParamModel model)
 {
     try
     {
         using (var dataContext = new HuntingEntities())
         {
             var session = GetSession();
             if (session == null)
             {
                 return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE));
             }
             var userSession = AclUserContext.GetUserSession(dataContext, session);
             if (userSession == null)
             {
                 return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE));
             }
             var locationId = UserLocationContext.CreateUserLocation(dataContext, model.TrackId, model.LocationX, model.LocationY, userSession.AclUserId);
             if (locationId == null)
             {
                 return(Content(HttpStatusCode.InternalServerError, FAILED_MESSAGE));
             }
             return(Ok());
         }
     }
     catch (Exception exception)
     {
         logger.Error(exception, "MobileController");
         return(InternalServerError());
     }
 }
コード例 #3
0
 public static Task SetLocationAsync(
     this IDistributedCache cache,
     UserChat userchat,
     UserLocationContext context,
     CancellationToken cancellationToken = default
     ) =>
 cache.SetStringAsync(
     GetKey(userchat, "location"),
     JsonConvert.SerializeObject(context),
     new DistributedCacheEntryOptions
 {
     SlidingExpiration = TimeSpan.FromMinutes(15)
 },
     cancellationToken
     );
コード例 #4
0
 public IHttpActionResult UserLocationList(int id)
 {
     try
     {
         using (var dataContext = new HuntingEntities())
         {
             var session = GetSession();
             if (session == null)
             {
                 return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE));
             }
             var userSession = AclUserContext.GetUserSession(dataContext, session);
             if (userSession == null)
             {
                 return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE));
             }
             var territory = TerritoryContext.GetDetail(dataContext, id);
             if (userSession.AclUser.CanViewTerritory(territory) == false)
             {
                 return(Content(HttpStatusCode.Forbidden, FORBIDDEN_MESSAGE));
             }
             var userLocationList = UserLocationContext.GetUserLocationList(dataContext, territory);
             if (userLocationList == null)
             {
                 return(Content(HttpStatusCode.InternalServerError, FAILED_MESSAGE));
             }
             var model = userLocationList.ConvertAll(item => new UserLocationModel(item));
             return(Ok(model));
         }
     }
     catch (Exception exception)
     {
         logger.Error(exception, "MobileController");
         return(InternalServerError());
     }
 }