Ejemplo n.º 1
0
        private async Task <IResult> OnEditRecipe(Recipe recipe, IAuthService _auth, IRecipeService _recipes,
                                                  HttpContext ctx)
        {
            var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(ctx));

            if (user?.Id is null)
            {
                return(Results.UnprocessableEntity(new ErrorResponse("Missing User from Token")));
            }

            var validationResult = ctx.Request.Validate(recipe);

            if (!validationResult.IsValid)
            {
                return(Results.BadRequest(new ErrorResponse("Failed Validation")
                {
                    Errors = validationResult.GetFormattedErrors()
                }));
            }

            if (recipe.UserId != user.Id)
            {
                return(Results.Forbid());
            }

            var didUpdate = await _recipes.Update(recipe);

            return(Results.Ok(didUpdate));
        }
Ejemplo n.º 2
0
        public async Task <IResult> OnCreateRecipe(Recipe payload, IRecipeService _recipes, IAuthService _auth,
                                                   HttpContext ctx)
        {
            var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(ctx));

            if (user?.Id is null)
            {
                return(Results.UnprocessableEntity(new ErrorResponse("Missing User from Token")));
            }

            var result = ctx.Request.Validate(payload);

            if (!result.IsValid)
            {
                return(Results.BadRequest(new ErrorResponse("Failed Validation")
                {
                    Errors = result.GetFormattedErrors()
                }));
            }

            var recipe = await _recipes.Create(payload with {
                UserId = user.Id
            });

            return(Results.Created($"/api/recipes/{recipe.Id}", recipe));
        }
Ejemplo n.º 3
0
        private async Task <IResult> OnFineOneRecipe(string id, IRecipeService _recipes, IAuthService _auth, HttpContext ctx)
        {
            var recipe = await _recipes.FindOne(id);

            var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(ctx));

            return(recipe?.UserId != user?.Id ? Results.Forbid() : Results.Ok(recipe));
        }
Ejemplo n.º 4
0
        private async Task <IResult> OnFindAllRecipes(IRecipeService _recipes, IAuthService _auth, HttpRequest req)
        {
            var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));

            if (user is null)
            {
                return(Results.UnprocessableEntity(new ErrorResponse("Missing User from Token")));
            }

            var _page  = req?.Query?.FirstOrDefault(f => f.Key == "page").Value.FirstOrDefault();
            var _limit = req?.Query?.FirstOrDefault(f => f.Key == "limit").Value.FirstOrDefault();

            var(page, limit) = ModuleHelpers.GetPagination(_page, _limit);
            var recipes = await _recipes.FindByUser(user.Id ?? string.Empty, page, limit);

            return(Results.Ok(recipes));
        }
Ejemplo n.º 5
0
        public async Task <IResult> OnDeleteRecipe(string id, IAuthService _auth, IRecipeService _recipes, HttpContext ctx)
        {
            var recipe = await _recipes.FindOne(id);

            var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(ctx));

            if (recipe.UserId != user?.Id)
            {
                return(Results.Forbid());
            }

            var didDestroy = await _recipes.Destroy(id);

            return(didDestroy
        ? Results.NoContent()
        : Results.UnprocessableEntity(new ErrorResponse("Failed To Delete This Recipe")));
        }
Ejemplo n.º 6
0
        public RecipeModule(IAuthService _auth, IRecipeService _recipes)
            : base("/api/recipes")
        {
            this.Before = ctx => ModuleHelpers.VerifyJwt(ctx, _auth);
            Get("", async(req, res) =>
            {
                var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (user == null)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(new ErrorResponse {
                        Message = "Missing User from Token"
                    });
                    return;
                }
                var _page        = req.Query.FirstOrDefault(f => f.Key == "page").Value.FirstOrDefault();
                var _limit       = req.Query.FirstOrDefault(f => f.Key == "limit").Value.FirstOrDefault();
                var(page, limit) = ModuleHelpers.GetPagination(_page, _limit);
                var recipes      = await _recipes.FindByUser(user.Id ?? string.Empty, page, limit);
                await res.Negotiate(recipes);
                return;
            });

            Get("/{id}", async(req, res) =>
            {
                var strId  = req.RouteValues.As <string>("id");
                var recipe = await _recipes.FindOne(strId);
                var user   = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (recipe?.UserId != user?.Id)
                {
                    res.StatusCode = 403;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "You don't have access to this recipe",
                    }
                        );
                    return;
                }

                await res.Negotiate(recipe);
            });

            Post("", async(req, res) =>
            {
                var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (user?.Id == null)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(new ErrorResponse {
                        Message = "Missing User from Token"
                    });
                    return;
                }

                var(validationResult, payload) = await req.BindAndValidate <Recipe>();
                if (!validationResult.IsValid)
                {
                    res.StatusCode = 400;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "Failed Validation",
                        Errors  = validationResult.GetFormattedErrors()
                    }
                        );
                    return;
                }
                payload.UserId = user.Id;
                var recipe     = await _recipes.Create(payload);

                res.StatusCode = 201;
                await res.Negotiate(recipe);
            });

            Put("", async(req, res) =>
            {
                var user = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (user?.Id == null)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(new ErrorResponse {
                        Message = "Missing User from Token"
                    });
                    return;
                }

                var(validationResult, recipe) = await req.BindAndValidate <Recipe>();
                if (!validationResult.IsValid)
                {
                    res.StatusCode = 400;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "Failed Validation",
                        Errors  = validationResult.GetFormattedErrors()
                    }
                        );
                    return;
                }

                if (recipe.UserId != user.Id)
                {
                    res.StatusCode = 403;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "You don't have access to this recipe",
                    }
                        );
                    return;
                }

                var didUpdate = await _recipes.Update(recipe);
                await res.Negotiate(didUpdate);
            });

            Delete("/{id}", async(req, res) =>
            {
                var strId  = req.RouteValues.As <string>("id");
                var recipe = await _recipes.FindOne(strId);
                var user   = await _auth.ExtractUserAsync(ModuleHelpers.ExtractTokenStr(req.HttpContext));
                if (recipe.UserId != user?.Id)
                {
                    res.StatusCode = 403;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "You don't have access to this recipe",
                    }
                        );
                    return;
                }

                var didDestroy = await _recipes.Destroy(strId);
                if (!didDestroy)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(
                        new ErrorResponse
                    {
                        Message = "Failed To Delete This Recipe",
                    }
                        );
                    return;
                }

                res.StatusCode = 204;
            });
        }