public void ConfigureRouting(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) { endpoints.MapGet("/", async context => await Respond(context, new Homepage())); endpoints.MapGet("/ops", async context => await Respond(context, new OpsPage())); endpoints.MapGet("_eventLog", WriteRandomEvents); endpoints.MapGet("/500", Error500); }
public static void MapAPIEndpoints(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) { endpoints.MapGet("/api/tag/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() : "" }; TagAPI api = new TagAPI(); await context.Response.WriteAsJsonAsync(api.GetMine(tokenUser, page, count)); } } }); }
public static void MapAPIEndpoints(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) { endpoints.MapGet("/api/mealplan/craft", async(context) => { int numberOfMeals = 0, numberOfRecipesPerMeal = 0; if (!context.Request.Query.ContainsKey("number-meals") || !int.TryParse(context.Request.Query["number-meals"].ToString(), out numberOfMeals)) { await context.Response.WriteAsync("In order to make a meal plan, we need a number of meals to include in the meal plan. Please use an integer number of meals."); return; } if (!context.Request.Query.ContainsKey("recipes-per-meal") || !int.TryParse(context.Request.Query["recipes-per-meal"].ToString(), out numberOfRecipesPerMeal)) { await context.Response.WriteAsync("In order to make a meal plan, we need a number of recipes per meal to include in the meal plan. Please use an integer number of recipes."); return; } MealPlan result = new MealPlan() { Meals = new List <Meal>() }; for (int mealCounter = 0; mealCounter < numberOfMeals; mealCounter++) { Meal newMeal = new Meal() { Recipes = new List <Recipe>() }; for (int recipeCounter = 0; recipeCounter < numberOfRecipesPerMeal; recipeCounter++) { newMeal.Recipes.Add(new Recipe()); } result.Meals.Add(newMeal); } await context.Response.WriteAsJsonAsync <MealPlan>(result); }); }
public void AddRoutes(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder app) { /* * var _name = nameof(Diagnostic); * var tag = $"{nameof(Extensions)}-{_name}".ToLower(); * var prefix = $"{nameof(Extensions)}/{_name}"; */ var prefix = _route.prefix; var tag = _route.tag; app.MapGet($"{prefix}".ToLower(), AppRuntime.Get).WithTags(tag); app.MapPost($"{prefix}/{nameof(AppRuntime.Stop)}".ToLower(), AppRuntime.Stop).WithTags(tag); app.MapPost($"{prefix}/config/reload".ToLower(), AppRuntime.ReloadConfiguration).WithTags(tag); }
public static void MapAPIEndpoints(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) { endpoints.MapGet("/api/shoppinglist", async(context) => { if (context.Request.Query.ContainsKey("recipes")) { string recipeIDparameter; recipeIDparameter = context.Request.Query["recipes"].ToString(); string[] recipeIDs = recipeIDparameter.Split(","); List <long> recipeIDList = new List <long>(); foreach (string id in recipeIDs) { long addMe = 0; if (long.TryParse(id, out addMe)) { recipeIDList.Add(addMe); } else { await context.Response.WriteAsync("Thank you for specifying a 'recipes' parameter, but, it needs to be a comma-separated list of integers. I couldn't read at least one of them."); return; } } AuthenticationToken tokenUser = new AuthenticationToken { ApplicationWideId = 0, ApplicationWideName = (context.Request.Query.ContainsKey("name")) ? context.Request.Query["name"].ToString() : "" }; ShoppingListAPI api = new ShoppingListAPI(); RecipeIngredient[] result = await api.GetShoppingList(recipeIDList); await context.Response.WriteAsJsonAsync <RecipeIngredient[]>(result); } else { await context.Response.WriteAsync("Nope, you need to supply a selection of Recipes using their IDs. You can do this by adding, eg '?recipes=1,2'"); } }); }
public static void MapAPIEndpoints(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) { endpoints.MapGet("/api/ingredient/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; } using ApplicationDbContext ctx = new ApplicationDbContext(); IngredientAPI api = new IngredientAPI(); Models.Ingredient[] ingredients = await api.GetAll(new AuthenticationToken(), page, count); await context.Response.WriteAsJsonAsync(ingredients); } ); endpoints.MapGet("/api/ingredient/", 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 an Ingredient with that ID." }); } else { IngredientAPI api = new IngredientAPI(); await context.Response.WriteAsJsonAsync(api.GetOne(new AuthenticationToken(), id)); } }); endpoints.MapGet("/api/ingredient/match", async(context) => { if (!context.Request.Query.ContainsKey("partial")) { await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "No partial provided to match Ingredients with." }); } else { string partial = context.Request.Query["partial"].ToString(); bool mustBeginWithPartial = !context.Request.Query.ContainsKey("starts"); if (!mustBeginWithPartial) { bool.TryParse(context.Request.Query["starts"].ToString(), out mustBeginWithPartial); } int count = DEFAULT_PARTIAL_MATCH_COUNT; if (context.Request.Query.ContainsKey("count")) { //count is optional, but, we won't support abusing it if (!int.TryParse(context.Request.Query["count"], out count) || count > MAX_PARTIAl_MATCH_COUNT || count < 1) { await context.Response.WriteAsJsonAsync(new { ResponseCode = 400, Message = "Invalid Ingredient count specified in request." }); } } IngredientAPI api = new IngredientAPI(); await context.Response.WriteAsJsonAsync(api.GetMatches(new AuthenticationToken(), partial, mustBeginWithPartial, count)); } }); endpoints.MapPost("/api/ingredient", async context => { if (!context.Request.Query.ContainsKey("name") || string.IsNullOrWhiteSpace(context.Request.Query["name"].ToString())) { await context.Response.WriteAsJsonAsync(new { ResponseCode = 404, Message = "Could not create an Ingredient with no name." }); } else { var name = context.Request.Query["name"].ToString(); IngredientAPI api = new IngredientAPI(); await context.Response.WriteAsJsonAsync(api.Create(new AuthenticationToken(), name)); } }); }