Example #1
0
        public async Task <IActionResult?> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "dishes/family/{familyId}/id/{dishId}")] HttpRequest req,
            string familyId,
            string dishId
            )
        {
            var(authenticationStatus, authenticationResponse) = await req.HttpContext.AuthenticateAzureFunctionAsync();

            if (!authenticationStatus)
            {
                return(authenticationResponse);
            }
            if (!_authz.Authorize(req.HttpContext.User.GetNameIdentifierId() !, familyId, Resources.Dish, Actions.Delete))
            {
                return(new UnauthorizedResult());
            }

            try
            {
                var dishCommand = new DeleteDishCommand(_dishRepository);
                await dishCommand.Handle(Guid.Parse(familyId), Guid.Parse(dishId));

                return(new OkResult());
            }
            catch (Exception e)
            {
                if (e is ArgumentException || e is ArgumentNullException)
                {
                    return(new BadRequestObjectResult(e.Message));
                }
                throw;
            }
        }
Example #2
0
        public async Task <ActionResult <string> > DeleteDish([FromBody] Guid id)
        {
            var deleteCommand = new DeleteDishCommand(id);
            var response      = await _mediator.Send(deleteCommand);

            return(string.IsNullOrEmpty(response)
                ? (ActionResult <string>)Ok(id)
                : BadRequest(response));
        }
Example #3
0
        public async Task <IActionResult> DeleteAsync([FromRoute] long id)
        {
            var command = new DeleteDishCommand
            {
                Id = id
            };

            var response = await _mediator.Send(command);

            return(JsonResult(response));
        }
Example #4
0
        public async Task <DishResponseModel> Handle(DeleteDishCommand request, CancellationToken cancellationToken)
        {
            var existedDish = await _dishRepository.GetByIdAsync(request.Id);

            if (existedDish == null)
            {
                throw new Exception($"Dish with id {request.Id} does not exist");
            }

            await _dishRepository.Delete(existedDish);

            var response = existedDish.Adapt <DishResponseModel>();

            return(response);
        }