public static User BuildUser() { return(new User() { Id = ParseUserId.GetUserId(CreateAuthHeader().Request.Headers), Email = "*****@*****.**", Name = "xunit" }); }
public void ItShouldExtractUserIdFromAuthToken() { var httpContext = new DefaultHttpContext(); httpContext.Request.Headers["authorization"] = $"Bearer {Utilities.Token}"; string userId = ParseUserId.GetUserId(httpContext.Request.Headers); Assert.Equal("JEFe2Smd7lOtw4OEBe2vovG8OzH2", userId); }
public async Task <ActionResult <object> > GeneratePlanner(GeneratePlannerInput reqBody) { string userId = ParseUserId.GetUserId(Request.Headers); /* the client will select a list of mealtypes that they want in their planner */ List <string> mealTypes = reqBody.input.mealTypes.ToList(); List <Recipe> allRecipes = (from recipe in _context.Recipes where recipe.Owner == userId where mealTypes.Contains(recipe.MealType) select recipe).ToList(); // this will shuffle the recipes List <Recipe> shuffledRecipes = allRecipes.OrderBy(r => Guid.NewGuid()).ToList(); DateTime startDate = DateTime.Parse(reqBody.input._gte); DateTime endDate = DateTime.Parse(reqBody.input._lte).AddDays(-1); /* delete existing planners for the given time because we are over writing it */ _context.RemoveRange( _context.Planners .Where(p => DateTime.Compare(p.Date, startDate) > 0) .Where(p => DateTime.Compare(p.Date, endDate) < 0) ); List <Planner> newPlanners = BuildPlannerForTheWeek( mealTypes, shuffledRecipes, userId, startDate ); await _context.Planners.AddRangeAsync(newPlanners); await _context.SaveChangesAsync(); var returningIds = newPlanners.Select(p => new { id = p.RecipeId }).ToList(); return(Ok(returningIds)); }
public async Task <ActionResult <object> > ScrapeRecipe(ScraperInput scraperInput) { string url = scraperInput.input.url; try { Recipe recipe = await Scraper.RunScraper(url); string userId = ParseUserId.GetUserId(Request.Headers); recipe.Owner = userId; // insert ot db _context.Recipes.Add(recipe); await _context.SaveChangesAsync(); return(Ok(new { id = recipe.Id })); } catch (System.OperationCanceledException e) { return(BadRequest(e.Message)); } }
public async Task <object> PostRecipe(InsertRecipeInput recipeInputRaw) { var userId = ParseUserId.GetUserId(Request.Headers); var recipeInput = recipeInputRaw.input; // parse the ingredient text and map it List <RecipeIngredient> recipeIngredients = new(); if (recipeInput.ingredients is not null) { recipeIngredients = (await Task.WhenAll(recipeInput.ingredients .Select(Parser.RunParser))) .ToList(); } // map everything to their proper location for db write Recipe recipe = new() { Directions = recipeInput.directions, Owner = userId, Img = recipeInput.img, TotalTime = recipeInput.total_time, Yields = recipeInput.yields, Cuisine = recipeInput.cuisine, MealType = recipeInput.meal_type, Title = recipeInput.title, RecipeIngredients = recipeIngredients }; _context.Recipes.Add(recipe); await _context.SaveChangesAsync(); return(new { id = recipe.Id }); } }