public async Task <IActionResult> UpdateRecipe(string recipeId, [FromBody] RecipePostPutModel data)
        {
            if (string.IsNullOrWhiteSpace(recipeId))
            {
                ModelState.AddModelError(nameof(recipeId), "No ID provided to update.");
            }

            if (data == null)
            {
                ModelState.AddModelError("Body", "No body provided.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                await _recipeService.Update(recipeId, data, CurrentUser.Id, CurrentUser.IsAdmin);

                return(Ok());
            }
            catch (KeyNotFoundException)
            {
                return(NotFound());
            }
            catch (Exception ex)
            {
                return(BadRequest($"Issue updating a recipe: {ex.Message}"));
            }
        }
示例#2
0
        public async Task <string> Create(RecipePostPutModel model, string executedById)
        {
            var newRecipe = new Recipe
            {
                Description  = model.Description,
                Ingredients  = model.Ingredients,
                Instructions = model.Instructions,
                Name         = model.Name,
                CreateDate   = _dateTimeService.GetEasternNow(),
                UpdateDate   = _dateTimeService.GetEasternNow(),
                CreatedById  = executedById,
                UpdatedById  = executedById
            };

            return(await _recipeStorage.Create(newRecipe));
        }
示例#3
0
        public async Task Update(string id, RecipePostPutModel model, string executedById, bool isAdmin)
        {
            var originalRecipe = await _recipeStorage.Read(id);

            if (originalRecipe == null)
            {
                throw new KeyNotFoundException();
            }

            if (!isAdmin && originalRecipe.CreatedById != executedById)
            {
                throw new Exception("You cannot update someone else's recipe");
            }

            originalRecipe.Description  = model.Description;
            originalRecipe.Ingredients  = model.Ingredients;
            originalRecipe.Instructions = model.Instructions;
            originalRecipe.Name         = model.Name;
            originalRecipe.UpdatedById  = executedById;
            originalRecipe.UpdateDate   = _dateTimeService.GetEasternNow();

            await _recipeStorage.Update(originalRecipe);
        }
        public async Task <IActionResult> CreateRecipe([FromBody] RecipePostPutModel data)
        {
            if (data == null)
            {
                ModelState.AddModelError("Body", "No body provided.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var createdId = await _recipeService.Create(data, CurrentUser.Id);

                return(Ok(createdId));
            }
            catch (Exception ex)
            {
                return(BadRequest($"Issue creating a new recipe: {ex.Message}"));
            }
        }