Beispiel #1
0
        public void TestTrackingSessions()
        {
            // Use IOC instead...
            const string connectionString = "mongodb://localhost";
            var          client           = new MongoClient(connectionString);
            var          server           = client.GetServer();
            var          database         = server.GetDatabase("test");
            var          dataStore        = new MongoDataStore(database);

            var sessions = new TrackingSessions(dataStore);
            var session1 = sessions.Add();

            for (int i = 0; i < 100; i++)
            {
                session1.AddPosition(new TrackingPosition((decimal)(0.01 * i), (decimal)(0.02 * i)));
                sessions.Update(session1);
            }
            var session2 = sessions.Add(TrackingType.Full);

            for (int i = 0; i < 100; i++)
            {
                session2.AddPosition(new TrackingPosition((decimal)(0.01 * i), (decimal)(0.02 * i)));
                sessions.Update(session2);
            }
        }
Beispiel #2
0
        public TrackingModule(IDataStore dataStore, TrackingUsers trackingUsers, TrackingSessions trackingSessions, ErrorCodes errorCodes)
            : base("/tracking", dataStore, trackingUsers, errorCodes)
        {
            Before += ctx =>
            {
                var user = Context.CurrentUser as UserIdentity;
                return(user == null
                        ? ErrorResponse(HttpStatusCode.Unauthorized, "Invalid access token! Please login to obtain a new access token.")
                        : null);
            };

            _trackingSessions   = trackingSessions;
            Get["/{sessionid}"] = parameters =>
            {
                var user = Context.CurrentUser as UserIdentity;

                string sessionId = parameters.sessionId;
                if (sessionId == null)
                {
                    return(ErrorResponse(HttpStatusCode.NotFound));
                }
                var trackingSession = _trackingSessions.Get(sessionId);
                if (trackingSession == null)
                {
                    return(ErrorResponse(HttpStatusCode.NotFound));
                }
                if (user == null || trackingSession.UserId != user.UserId)
                {
                    return(ErrorResponse(HttpStatusCode.Unauthorized));
                }
                if (trackingSession.Expires <= DateTime.Now)
                {
                    return(ErrorResponse(HttpStatusCode.Forbidden, "Session has expired!"));
                }

                return(Response.AsJson(trackingSession));
            };
            Post["/"] = parameters =>
            {
                var user = Context.CurrentUser as UserIdentity;
                if (user == null)
                {
                    return(ErrorResponse(HttpStatusCode.Unauthorized));
                }

                Response response;
                if (!CheckSaveRetention(SessionCache, out response))
                {
                    return(response);
                }

                var trackingSession = this.Bind <TrackingSession>();
                trackingSession.UserId = user.UserId;
                _trackingSessions.Add(trackingSession);

                return(Response.AsJson(trackingSession));
            };
            Put["/{sessionid}"] = parameters =>
            {
                var user = Context.CurrentUser as UserIdentity;
                if (user == null)
                {
                    return(ErrorResponse(HttpStatusCode.Unauthorized));
                }

                Response response;
                if (!CheckSaveRetention(SessionCache, out response))
                {
                    return(response);
                }

                string sessionId = parameters.sessionId;
                if (sessionId == null)
                {
                    return(ErrorResponse(HttpStatusCode.NotFound));
                }

                var trackingSession = this.Bind <TrackingSession>();
                trackingSession.Id     = sessionId;
                trackingSession.UserId = user.UserId;

                _trackingSessions.Update(trackingSession);

                return(Response.AsJson(trackingSession));
            };
            Delete["/{sessionid}"] = parameters =>
            {
                var user = Context.CurrentUser as UserIdentity;
                if (user == null)
                {
                    return(ErrorResponse(HttpStatusCode.Unauthorized));
                }

                string sessionId = parameters.sessionId;
                if (sessionId != null)
                {
                    var trackingSession = _trackingSessions.Get(sessionId);
                    if (trackingSession == null)
                    {
                        return(ErrorResponse(HttpStatusCode.NotFound));
                    }
                    if (trackingSession.UserId != user.UserId)
                    {
                        return(ErrorResponse(HttpStatusCode.Unauthorized));
                    }

                    _trackingSessions.Delete(sessionId);
                    return(ErrorResponse(HttpStatusCode.OK,
                                         string.Format("Tracking session '{0}' including all its positions is removed.", sessionId)));
                }
                return(ErrorResponse(HttpStatusCode.BadRequest));
            };
        }