Пример #1
0
        private async Task <RecipeIngredient[]> GetShoppingList(List <long> recipeIDList)
        {
            List <RecipeIngredient> buildMe = new List <RecipeIngredient>();
            Dictionary <string, Dictionary <string, Dictionary <string, Fractionable> > > ingredientInfoList = new Dictionary <string, Dictionary <string, Dictionary <string, Fractionable> > >();

            foreach (long recipeID in recipeIDList)
            {
                RecipeAPI recipeAPI = new RecipeAPI();
                using ApplicationDbContext _context = new ApplicationDbContext();
                RecipeIngredient[] processThese = _context.RecipeIngredients.Where(ri => ri.RecipeId == recipeID).Include(recipeI => recipeI.Ingredient).ToArray();
                ingredientInfoList = AddRecipeIngredientsToList(ingredientInfoList, processThese);
            }
            //now translate into shopping list
            string ingredientName, ingredientPreparation, ingredientMeasurement;

            foreach (string lookupName in ingredientInfoList.Keys)
            {
                ingredientName = lookupName;
                Dictionary <string, Dictionary <string, Fractionable> > preparationsIntoMeasurementsIntoQuantities = ingredientInfoList[ingredientName];
                foreach (string lookupPreparation in preparationsIntoMeasurementsIntoQuantities.Keys)
                {
                    ingredientPreparation = lookupPreparation;
                    Dictionary <string, Fractionable> measurementsIntoQuantities = preparationsIntoMeasurementsIntoQuantities[lookupPreparation];
                    foreach (string lookupMeasurement in measurementsIntoQuantities.Keys)
                    {
                        ingredientMeasurement = lookupMeasurement;
                        buildMe.Add(new RecipeIngredient()
                        {
                            Ingredient = new Ingredient()
                            {
                                Name = ingredientName
                            }, Preparation = ingredientPreparation, Measurement = ingredientMeasurement, Quantity = measurementsIntoQuantities[ingredientMeasurement]
                        });
                    }
                }
            }
            return(buildMe.ToArray());
        }
Пример #2
0
 public static void MapAPIEndpoints(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints)
 {
     endpoints.MapGet("/api/recipe/all/", async(context) =>
     {
         if (!context.Request.Query.ContainsKey("page") ||
             !int.TryParse(context.Request.Query["page"].ToString(), out int page))
         {
             page = 0;
         }
         if (!context.Request.Query.ContainsKey("count") ||
             !int.TryParse(context.Request.Query["count"].ToString(), out int count))
         {
             count = 100;
         }
         RecipeAPI api = new RecipeAPI();
         await context.Response.WriteAsJsonAsync(api.GetAll(new AuthenticationToken(), page, count));
     });
     endpoints.MapGet("/api/recipe/", async(context) =>
     {
         if (!context.Request.Query.ContainsKey("id") ||
             !long.TryParse(context.Request.Query["id"].ToString(), out long id))
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 404, Message = "Could not find a Recipe with that ID." });
         }
         else
         {
             RecipeAPI api = new RecipeAPI();
             await context.Response.WriteAsJsonAsync(api.GetOne(new AuthenticationToken(), id));
         }
     });
     endpoints.MapGet("/api/recipe/mine/", async(context) => {
         long id = 0;
         if ((!context.Request.Query.ContainsKey("name") && !context.Request.Query.ContainsKey("id")) ||
             (context.Request.Query.ContainsKey("name") && string.IsNullOrWhiteSpace(context.Request.Query["name"].ToString()) ||
              (context.Request.Query.ContainsKey("id") && !long.TryParse(context.Request.Query["id"].ToString(), out id))))
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Either both no name and no id specified for that Cook, or one of the two wasn't readable." });
         }
         else
         {
             int page = 0, count = 100;
             if (context.Request.Query.ContainsKey("page") && !int.TryParse(context.Request.Query["page"].ToString(), out page) ||
                 (context.Request.Query.ContainsKey("count") && !int.TryParse(context.Request.Query["count"].ToString(), out count)) ||
                 (context.Request.Query.ContainsKey("page") && !context.Request.Query.ContainsKey("count")))
             {
                 await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Either couldn't read page or count requested, or page was requested and count (per page) wasn't." });
             }
             else
             {
                 AuthenticationToken tokenUser = new AuthenticationToken {
                     ApplicationWideId = id, ApplicationWideName = (context.Request.Query.ContainsKey("name")) ? context.Request.Query["name"].ToString() : ""
                 };
                 RecipeAPI api = new RecipeAPI();
                 await context.Response.WriteAsJsonAsync(api.GetMine(tokenUser, page, count));
             }
         }
     });
     endpoints.MapDelete("/api/recipe", async(context) =>
     {
         if (!context.Request.Query.ContainsKey("id") ||
             !long.TryParse(context.Request.Query["id"].ToString(), out long id))
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 404, Message = "Could not find a Recipe with that ID." });
         }
         else
         {
             RecipeAPI api = new RecipeAPI();
             await context.Response.WriteAsJsonAsync(api.Delete(new AuthenticationToken(), id));
         }
     });
     endpoints.MapPost("/api/recipe", async(context) =>
     {
         Stream body   = context.Request.Body;
         Recipe recipe = null;
         if (body != null)
         {
             string bodyJsonString = await(new StreamReader(body)).ReadToEndAsync();
             recipe = JsonSerializer.Deserialize <Recipe>(bodyJsonString);
         }
         else
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Body of request must contain a Recipe in Json format." });
         }
         if (recipe != null)
         {
             RecipeAPI api = new RecipeAPI();
             await context.Response.WriteAsJsonAsync(api.Create(new AuthenticationToken(), recipe));
         }
         else
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Could not read a Recipe from the body of your request." });
         }
     });
     endpoints.MapPut("/api/recipe", async(context) => {
         Stream body   = context.Request.Body;
         Recipe recipe = null;
         if (body != null)
         {
             string bodyJsonString = await(new StreamReader(body)).ReadToEndAsync();
             recipe = JsonSerializer.Deserialize <Recipe>(bodyJsonString);
         }
         else
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Body of request must contain a Recipe in Json format." });
         }
         if (recipe != null)
         {
             RecipeAPI api = new RecipeAPI();
             await context.Response.WriteAsJsonAsync(api.Update(new AuthenticationToken(), recipe));
         }
         else
         {
             await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Could not read a Recipe from the body of your request." });
         }
     });
 }