Exemplo n.º 1
0
        public async Task <ActionResult <Tweet> > PostTweet([FromBody] Tweet tweet, [FromHeader] string Authorization)
        {
            string idToken = Authorization.Split(" ")[1];

            _logger.LogInformation(HttpContext.Request.ToString());
            _logger.LogInformation(idToken);
            // validate authorization headers.
            var user = await _context.Users.FindAsync(tweet.UserId);

            var code = AuthModel.validToken(user, idToken);

            _logger.LogInformation(code.ToString());
            if (code != 200)
            {
                return(StatusCode(code));
            }
            try
            {
                tweet.CreatedAt = DateTime.Now;
                _context.Tweets.Add(tweet);
                await _context.SaveChangesAsync();
            } catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Could not post tweet. Internal error.");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }

            return(CreatedAtAction("GetTweet", new { id = tweet.Id }, tweet));
        }