コード例 #1
0
        public async Task <IActionResult> Edit(UpdateRecipeCommand command)
        {
            try
            {
                var recipe     = _service.GetRecipe(command.Id);
                var authResult = await _authService.AuthorizeAsync(User, recipe, "CanManageRecipe");

                if (!authResult.Succeeded)
                {
                    return(new ForbidResult());
                }

                if (ModelState.IsValid)
                {
                    _service.UpdateRecipe(command);
                    return(RedirectToAction(nameof(View), new { id = command.Id }));
                }
            }
            catch (Exception)
            {
                // TODO: Log error
                // Add a model-level error by using an empty string key
                ModelState.AddModelError(
                    string.Empty,
                    "An error occured saving the recipe"
                    );
            }

            //If we got to here, something went wrong
            return(View(command));
        }
コード例 #2
0
        public async Task <IActionResult> Put(int id, [FromBody] UpdateRecipeCommand command)
        {
            command.Id = id;
            var result = await _service.UpdateAsync(command);

            return(Ok(result));
        }
コード例 #3
0
 private void UpdateRecipe(Recipe recipe, UpdateRecipeCommand cmd)
 {
     recipe.Name         = cmd.Name;
     recipe.TimeToCook   = new TimeSpan(cmd.TimeToCookHrs, cmd.TimeToCookMins, 0);
     recipe.IsVegetarian = cmd.IsVegetarian;
     recipe.IsVegan      = cmd.IsVegan;
 }
コード例 #4
0
ファイル: RecipeController.cs プロジェクト: Lev1t/Recipes
        public async Task <IActionResult> Edit(UpdateRecipeCommand cmd)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var authResult = await AuthorizeToManageRecipeAsync(cmd.Id);

                    if (!authResult.Succeeded)
                    {
                        _logger.LogWarning("User {UserName} attempted to edit the recipe ID {Id} without permission",
                                           User.Identity.Name, cmd.Id);

                        return(new ForbidResult());
                    }
                    await _recipeService.UpdateRecipeAsync(cmd);

                    _logger.LogInformation("User {UserName} has updated the recipe ID {Id}", User.Identity.Name, cmd.Id);
                    return(RedirectToAction(nameof(Details), new { Id = cmd.Id }));
                }
                else
                {
                    _logger.LogWarning("Updating the recipe ID {Id}  by User {UserName} was fail due to invalid ModelState",
                                       cmd.Id, User.Identity.Name);
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "An error occurred saving the recipe");
                _logger.LogError("Failed to update the recipe ID {Id} by User {UserName}\nException: {Exception}",
                                 cmd.Id, User.Identity.Name, ex);
            }
            return(View(cmd));
        }
コード例 #5
0
        public virtual async Task <NotificationResult> UpdateAsync(UpdateRecipeCommand command)
        {
            BeginTransaction();
            var result = await _handler.UpdateAsync(command);

            return(Commit(result));
        }
コード例 #6
0
        public UpdateRecipeCommand UpdateRecipe(UpdateRecipeCommand cmd)
        {
            var recipe = _context.Recipes.Find(cmd.Id);

            if (recipe == null)
            {
                throw new Exception("Unable to find the recipe");
            }
            UpdateRecipe(recipe, cmd);
            _context.SaveChanges();
            return(cmd);
        }
コード例 #7
0
        public async Task <IActionResult> OnGet(int id)
        {
            Input = await _service.GetRecipeForUpdate(id);

            if (Input is null)
            {
                // If id is not for a valid Recipe, generate a 404 error page
                // TODO: Add status code pages middleware to show friendly 404 page
                return(NotFound());
            }
            return(Page());
        }
コード例 #8
0
ファイル: RecipeService.cs プロジェクト: Lev1t/Recipes
        public async Task UpdateRecipeAsync(UpdateRecipeCommand cmd)
        {
            var recipe = await _context.Recipes
                         .Include(r => r.Ingridients)
                         .FirstOrDefaultAsync(r => r.RecipeId == cmd.Id);

            if (recipe == null)
            {
                throw new Exception($"Unable to find recipe with ID {cmd.Id}");
            }

            cmd.UpdateRecipe(recipe);
            await _context.SaveChangesAsync();
        }
コード例 #9
0
        /// <summary>
        /// Updateds an existing recipe
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns>The id of the new recipe</returns>
        public async Task UpdateRecipe(UpdateRecipeCommand cmd)
        {
            var recipe = await _context.Recipes.FindAsync(cmd.Id);

            if (recipe == null)
            {
                throw new Exception("Unable to find the recipe");
            }
            if (recipe.IsDeleted)
            {
                throw new Exception("Unable to update a deleted recipe");
            }

            cmd.UpdateRecipe(recipe);
            await _context.SaveChangesAsync();
        }
コード例 #10
0
        /// <summary>
        /// Updateds an existing recipe
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns>The id of the new recipe</returns>
        public void UpdateRecipe(UpdateRecipeCommand cmd)
        {
            var recipe = _context.Recipes.Find(cmd.Id);

            if (recipe == null)
            {
                throw new Exception("Unable to find the recipe");
            }
            if (recipe.IsDeleted)
            {
                throw new Exception("Unable to update a deleted recipe");
            }

            cmd.UpdateRecipe(recipe);
            _context.SaveChanges();
        }
コード例 #11
0
        public async Task <IActionResult> OnGet(int id)
        {
            var recipe = await _service.GetRecipe(id);

            var authResult = await _authService.AuthorizeAsync(User, recipe, "CanManageRecipe");

            if (!authResult.Succeeded)
            {
                return(new ForbidResult());
            }

            Input = await _service.GetRecipeForUpdate(id);

            if (Input is null)
            {
                // If id is not for a valid Recipe, generate a 404 error page
                // TODO: Add status code pages middleware to show friendly 404 page
                return(NotFound());
            }
            return(Page());
        }
コード例 #12
0
        public async Task <IActionResult> UpdateRecipe([FromBody] UpdateRecipeCommand command)
        {
            var validationResult = _validators.ValidateUpdate(command);

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

            var result = await _mediator.Command(command);

            if (!result.IsFailure)
            {
                return(Ok(result));
            }

            if (result.ResultCode.Equals(ResultCode.NotFound))
            {
                return(NotFound());
            }

            return(BadRequest(result));
        }
コード例 #13
0
        public virtual async Task <NotificationResult> UpdateAsync(UpdateRecipeCommand command)
        {
            var result = new NotificationResult();
            var item   = new RecipeInfo(command);

            result.Add(item.GetNotificationResult());
            if (!result.IsValid)
            {
                return(result);
            }
            result.Add(await _recipeRepository.UpdateAsync(item));
            if (result.IsValid)
            {
                result.Data = item.Id;
                result.AddMessage(Shared.Domain.Resources.Handler.UpdateSuccess_Message);
            }
            else
            {
                result.AddErrorOnTop(Shared.Domain.Resources.Handler.UpdateError_Message);
            }

            return(result);
        }
コード例 #14
0
        public IActionResult Edit(UpdateRecipeCommand command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _service.UpdateRecipe(command);
                    return(RedirectToAction(nameof(View), new { id = command.Id }));
                }
            }
            catch (Exception)
            {
                // TODO: Log error
                // Add a model-level error by using an empty string key
                ModelState.AddModelError(
                    string.Empty,
                    "An error occured saving the recipe"
                    );
            }

            //If we got to here, something went wrong
            return(View(command));
        }
コード例 #15
0
        public async Task <IActionResult> EditAsync(
            int id, [FromBody] UpdateRecipeCommand command)
        {
            var ipAddress = HttpContext
                            .Connection.RemoteIpAddress;

            if (!_allowedAddress.Contains(ipAddress))
            {
                return(Forbid());
            }

            if (!IsEnabled)
            {
                return(BadRequest());
            }

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

                if (!await _service.DoesRecipeExistAsync(id))
                {
                    return(NotFound());
                }

                await _service.UpdateRecipe(command);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(GetErrorResponse(ex));
            }
        }
コード例 #16
0
        public async override Task <string> Handle(UpdateRecipeCommand request, CancellationToken cancellationToken)
        {
            Entity = repo.Find(request.ID);

            if (Entity == null)
            {
                throw new InvalidOperationException($"Recipe Find And Update Error not Found Entity {Entity.ObjectToString()} and {request.ObjectToString()}");
            }

            Entity.ID            = request.ID;
            Entity.CookTime      = request.CookTime;
            Entity.Description   = request.Description;
            Entity.ImageUrl      = request.ImageUrl;
            Entity.NumberServing = request.NumberServing;
            Entity.TimePrep      = request.TimePrep;
            Entity.Title         = request.Title;
            Entity.VideoUrl      = request.VideoUrl;

            Result = repo.Update(Entity);

            logger.LogInformation("Update Recipe", Result, Entity, request);

            return(Result);
        }
コード例 #17
0
 public ValidationResult ValidateUpdate(UpdateRecipeCommand command)
 => _updateValidator.Validate(command);
        public async Task <IActionResult> Edit(int id, [FromBody] UpdateRecipeCommand command)
        {
            await _service.UpdateRecipe(command);

            return(Ok());
        }
コード例 #19
0
 public async Task <ActionResult> Put([FromBody] UpdateRecipeCommand command)
 {
     return(Ok(await mediator.Send(command)));
 }
コード例 #20
0
 public async Task <IActionResult> Update([FromRoute] Guid id, [FromBody] UpdateRecipeCommand command)
 {
     return(Ok(await _mediator.Send(command)));
 }
コード例 #21
0
 public IActionResult Edit(int id, [FromBody] UpdateRecipeCommand command)
 {
     _service.UpdateRecipe(command);
     return(Ok());
 }
コード例 #22
0
        public IActionResult Update(UpdateRecipeCommand cmd)
        {
            var recipe = _service.UpdateRecipe(cmd);

            return(Ok(cmd));
        }
コード例 #23
0
 public async Task <IActionResult> Put([FromRoute] Guid recipeId, UpdateRecipeCommand request)
 => Ok(await _mediator.Send(new UpdateRecipeCommand(recipeId, request)));
コード例 #24
0
 public RecipeInfo(UpdateRecipeCommand command)
 {
     Map(command);
     InitCollections();
     this.InsertOrUpdateScopeValidate();
 }
コード例 #25
0
        public async Task <IActionResult> Update([FromBody] UpdateRecipeCommand command)
        {
            var result = await Mediator.Send(command);

            return(Ok(result));
        }