public static async Task <IActionResult> RunAsync( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, [CosmosDB("ProjetWeb", "Users", ConnectionStringSetting = "CosmosDB")] DocumentClient users, ILogger log) { string bearer = req.Headers["Authorization"]; var authHeader = AuthUtils.GetClaims(bearer.Substring(7)); Models.User foundUser = UserUtils.GetUserFromEmail(users, authHeader["email"].ToString()); if (foundUser == null) { return(new UnauthorizedResult()); } var collectionUri = UriFactory.CreateDocumentCollectionUri("ProjetWeb", "Users"); var queryOptions = new FeedOptions { EnableCrossPartitionQuery = true }; var query = users.CreateDocumentQuery <Models.User>(collectionUri, queryOptions); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var data = JsonConvert.DeserializeObject <Models.Product>(requestBody); var newProduct = new Models.Product { id = data.id, Title = data.Title, Description = data.Description, Price = data.Price, Year = data.Year, HorsePower = data.HorsePower, Mileage = data.Mileage, Fuel = data.Fuel, Images = data.Images }; if (foundUser.Orders == null) { foundUser.Orders = new List <Models.Order>(); foundUser.Orders.Add(new Models.Order { Product = newProduct, IsPayed = true, OrderPlacedDate = DateTime.Now }); } foundUser.Orders.Add(new Models.Order { id = Guid.NewGuid().ToString(), Product = newProduct, IsPayed = true, OrderPlacedDate = DateTime.Now }); await users.UpsertDocumentAsync(collectionUri, foundUser); return(new OkObjectResult("OK")); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, [CosmosDB("ProjetWeb", "Users", ConnectionStringSetting = "CosmosDB")] DocumentClient users, ILogger log) { string bearer = req.Headers["Authorization"]; var authHeader = AuthUtils.GetClaims(bearer.Substring(7)); Models.User foundUser = UserUtils.GetUserFromEmail(users, authHeader["email"].ToString()); if (foundUser == null) { var notFoundResponse = new BaseResponse <object>(); notFoundResponse.Errors.Add("Utilisateur non trouvé."); var notFoundResult = new OkObjectResult(notFoundResponse) { StatusCode = StatusCodes.Status401Unauthorized }; return(notFoundResult); } return(new OkObjectResult(new BaseResponse <UserDto>(_mapper.Map <UserDto>(foundUser)))); }
public async Task <IActionResult> RunAsync( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, [CosmosDB("ProjetWeb", "Users", ConnectionStringSetting = "CosmosDB")] DocumentClient users, ILogger log) { string bearer = req.Headers["Authorization"]; var authHeader = AuthUtils.GetClaims(bearer.Substring(7)); Models.User foundUser = UserUtils.GetUserFromEmail(users, authHeader["email"].ToString()); if (foundUser == null) { return(new UnauthorizedResult()); } var collectionUri = UriFactory.CreateDocumentCollectionUri("ProjetWeb", "Users"); var query = users.CreateDocumentQuery <Models.User>(collectionUri); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var data = JsonConvert.DeserializeObject <Models.DTO.CreateOrUpdateUserDto>(requestBody); foundUser.FirstName = data.FirstName; foundUser.LastName = data.LastName; foundUser.Address = data.Address; foundUser.PostalCode = data.PostalCode; foundUser.City = data.City; await users.UpsertDocumentAsync(collectionUri, foundUser); return(new OkObjectResult(new BaseResponse <Models.User>(foundUser))); }