protected override async Task Handle(Command request, CancellationToken cancellationToken)
            {
                FoodCategory category = await _categoryRepository.GetByName(request?.Category);

                if (category is null)
                {
                    category      = new FoodCategory();
                    category.Name = request?.Category;

                    await _categoryRepository.Add(category);
                }
                await _repository.Add(MapWasteProductViewToModel(request, category.Id));
            }
Esempio n. 2
0
        public async Task <BaseDtoResponse <FoodCategoryDto> > Add(FoodCategoryCreateRequest request)
        {
            try
            {
                FoodCategory model    = _mapper.Map <FoodCategoryCreateRequest, FoodCategory>(request);
                FoodCategory category = await _foodCategoryRepository.Add(model);

                if (category != null)
                {
                    FoodCategoryDto result = _mapper.Map <FoodCategory, FoodCategoryDto>(category);
                    return(new BaseDtoResponse <FoodCategoryDto>(result));
                }
                else
                {
                    return(new BaseDtoResponse <FoodCategoryDto>("Unable to create a new category, try again"));
                }
            }
            catch (Exception ex)
            {
                return(new BaseDtoResponse <FoodCategoryDto>($"An error occurred when saving the category: {ex.Message}"));
            }
        }
Esempio n. 3
0
            protected override async Task Handle(Command request, CancellationToken cancellationToken)
            {
                var foodProduct = await _repository.Get(request.Id);

                if (foodProduct is null)
                {
                    throw new Exception("Waste product doesn't exist!");
                }

                FoodCategory category = await _categoryRepository.GetByName(request?.Category?.Name);

                if (category is null)
                {
                    category = new FoodCategory {
                        Name = request?.Category?.Name
                    };

                    await _categoryRepository.Add(category);
                }

                MapWasteProductViewToModel(foodProduct, request, category.Id);
                await _repository.Update(foodProduct);
            }