Ejemplo n.º 1
0
        public async Task <IActionResult> Create(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "Tweet")] HttpRequest req,
            [AccessToken] AccessTokenResult accessToken = default)
        {
            _logger.LogInformation("Starting {operation}", nameof(Create));

            if (accessToken?.Status != AccessTokenStatus.Valid)
            {
                return(new UnauthorizedResult());
            }

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            Tweet  data        = JsonConvert.DeserializeObject <Tweet>(requestBody);

            if (data == null)
            {
                return(new BadRequestObjectResult("Invalid request"));
            }

            data.User   = accessToken.Principal.UserName();
            data.UserId = accessToken.Principal.UserId();

            data = await _repository.InsertOneAsync(data);

            return(new OkObjectResult(data));
        }