Exemple #1
0
        public static bool TryParse(string tokenString, out SimpleWebToken token)
        {
            token = null;
            if (tokenString == string.Empty)
            {
                token = new SimpleWebToken();
                return(true);
            }

            if (tokenString == null)
            {
                return(false);
            }

            var claimPairs = tokenString.Split("&".ToArray());

            if (!claimPairs.All(x => x.Contains("=")))
            {
                return(false);
            }

            var claims = claimPairs
                         .Select(s => s.Split("=".ToArray()))
                         .Select(a => new Claim(a[0], a[1]));

            token = new SimpleWebToken(claims.ToArray());

            return(true);
        }
        public HttpResponseMessage Post(JournalEntryModel journalEntry)
        {
            SimpleWebToken swt;

            SimpleWebToken.TryParse(this.Request.Headers.Authorization.Parameter, out swt);
            var userName = swt.Single(c => c.Type == "userName").Value;

            var connStr = ConfigurationManager.ConnectionStrings["running-journal"].ConnectionString;
            var db      = Database.OpenConnection(connStr);

            var userId = db.User.Insert(UserName: userName).UserId;

            db.JournalEntry.Insert(
                UserId: userId,
                Time: journalEntry.Time,
                Distance: journalEntry.Distance,
                Duration: journalEntry.Duration
                );

            return(this.Request.CreateResponse());
        }
        public HttpResponseMessage Get()
        {
            SimpleWebToken swt;

            SimpleWebToken.TryParse(this.Request.Headers.Authorization.Parameter, out swt);
            var userName = swt.Single(c => c.Type == "userName").Value;

            var connStr = ConfigurationManager.ConnectionStrings["running-journal"].ConnectionString;
            var db      = Database.OpenConnection(connStr);

            var entries = db.JournalEntry
                          .FindAll(db.JournalEntry.User.UserName == userName)
                          .ToArray <JournalEntryModel>();

            return(this.Request.CreateResponse(
                       HttpStatusCode.OK,
                       new JournalModel
            {
                Entries = entries
            }));
        }