Ejemplo n.º 1
0
        public async Task <CommandResult> Handle(UpdateFoodCommand request, CancellationToken cancellationToken)
        {
            CustomFood customFood = await _foodRepository.GetCustomByIdAsync(request.FoodId, _currentProfileId);

            if (customFood == null)
            {
                return(FailureDueToCustomFoodNotFound());
            }

            CustomFoodTable customFoodTable = await _foodTableRepository.GetCustomByIdAsync(request.FoodTableId, _currentProfileId);

            if (customFoodTable == null)
            {
                return(FailureDueToCustomFoodTableNotFound());
            }

            customFood.Update(
                request.Name,
                request.Description,
                request.FoodTableId,
                _mapper.Map <MacronutrientTable>(request.Macronutrients),
                _mapper.Map <MicronutrientTable>(request.Micronutrients),
                new FoodUnit(request.UnitType, request.DefaultGramsQuantityMultiplier)
                );

            await _foodRepository.UpdateAsync(customFood);

            return(await CommitAndPublishDefaultAsync());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UpdateAsync(Guid id, [FromBody] UpdateFoodCommand command)
        {
            bool validCommand = command.Validate();

            if (!validCommand)
            {
                return(CreateErrorResponse(command.ValidationResult));
            }

            Food food = await _foodRepository.GetDefaultByIdAsync(id);

            if (food == null)
            {
                return(NotFound());
            }

            food.Update(
                command.Name,
                command.Description,
                command.FoodTableId,
                _mapper.Map <MacronutrientTable>(command.Macronutrients),
                _mapper.Map <MicronutrientTable>(command.Micronutrients),
                new FoodUnit(command.UnitType, command.DefaultGramsQuantityMultiplier)
                );

            await _foodRepository.UpdateAsync(food);

            return(await CommitAsync());
        }
Ejemplo n.º 3
0
        public async Task <BaseResponse <Food> > Handle(UpdateFoodCommand request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <Food> ()
            {
                ReponseName = nameof(UpdateFoodCommand), Content = new List <Food> ()
                {
                }
            };
            var entity = await _foodRepository.GetOneAsync(p => p.Id == request.Id);

            if (entity == null)
            {
                response.Status  = ResponseType.Error;
                response.Message = $"{nameof(Food)} not found.";
                response.Content = null;
            }
            else
            {
                _mapper.Map(request, entity, typeof(UpdateFoodCommand), typeof(Food));
                await _foodRepository.UpdateAsync(entity);

                response.Status  = ResponseType.Success;
                response.Message = $"{nameof(Food)} updated successfully.";
                response.Content.Add(entity);
            }
            return(response);
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Update(int id, UpdateFoodCommand command)
        {
            if (id != command.Id)
            {
                return(BadRequest());
            }

            await Mediator.Send(command);

            return(NoContent());
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <BaseResponse <Food> > > UpdateFood(UpdateFoodCommand command)
        {
            try {
                var result = await _mediator.Send(command);

                return(Ok(result));
            } catch (ValidationException ex) {
                var err = new BaseResponse <Food> ();
                err.Status  = ResponseType.Error;
                err.Message = ex.Message;
                err.Content = null;
                return(Ok(err));
            }
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> UpdateAsync(Guid id, [FromBody] UpdateFoodCommand command)
        {
            command.FoodId = id;

            return(await CreateCommandResponse(command));
        }