Example #1
0
        public async Task <OperationResult <FoodCreateCommandResult> > Handle(FoodCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var food = new Food(request.Name, new Mony(request.Price), request.FoodType)
                {
                    Description = request.Description
                };
                food.UpdatePrice(new Mony(request.Price));
                var newfood = await _db.Foods.AddAsync(food);

                // await _db.SaveChangesAsync();
                var result = OperationResult <FoodCreateCommandResult>
                             .BuildSuccessResult(new FoodCreateCommandResult
                {
                    FoodId = newfood.Entity.ID
                });

                return(result);
            }
            catch (Exception ex)
            {
                var exResult = OperationResult <FoodCreateCommandResult> .BuildFailure(ex);

                return(exResult);
            }
        }
Example #2
0
        public async Task <OperationResult <FoodCreateCommandResult> > Handle(FoodCreateCommand request, CancellationToken cancellationToken)
        {
            var food = new Food(request.Name, new Money(request.Price), request.FoodType)
            {
                Description = request.Description
            };

            try
            {
                var newFoodId = await _uow.FoodRepository.Create(food);// await _db.Foods.AddAsync(food);

                await _uow.CommitAsync();

                var result = OperationResult <FoodCreateCommandResult>
                             .BuildSuccessResult(new FoodCreateCommandResult
                {
                    FoodId = newFoodId
                });

                await Task.CompletedTask;
                return(result);
            }
            catch (Exception ex)
            {
                var exResult = OperationResult <FoodCreateCommandResult> .BuildFailure(ex);

                return(exResult);
            }
        }
Example #3
0
        public async Task <OperationResult <FoodCreateCommandResult> > Handle(FoodCreateCommand request, CancellationToken cancellationToken, RequestHandlerDelegate <OperationResult <FoodCreateCommandResult> > next)
        {
            var any = _db.Foods.Any(f => f.Name.Trim() == request.Name.Trim());

            if (any)
            {
                var result = OperationResult <FoodCreateCommandResult> .BuildFailure(new Exception("Food name already exists!"), "Food name already exists!");

                return(result);
                //throw
            }

            var response = await next();

            return(response);
        }
        public async Task <int> Handle(FoodCreateCommand request, CancellationToken cancellationToken, RequestHandlerDelegate <int> next)
        {
            var validator = new FoodCreateCommandValidator();
            var check     = validator.Validate(request);

            if (check.IsValid)
            {
                var response = await next();

                return(response);
            }
            else
            {
                throw new Exception(" Food is not valid");
            }
        }