Пример #1
0
        public async Task <bool> Handle(DeleteCategoriesRequest message, IOutputPort <DeleteCategoriesResponse> outputPort)
        {
            try
            {
                await Task.Run(() =>
                {
                    Guid[] uids = message.Categories.Select(o => o.Uid).ToArray();
                    foreach (CategoryRelation relation in _CatRelRepo.List(o => uids.Contains(o.ChildUid) || uids.Contains(o.ParentUid)).ToArray())
                    {
                        _CatRelRepo.Delete(relation);
                    }
                    foreach (EntryCategoryRelation relation in _EntryCatRelRepo.List(o => uids.Contains(o.CategoryUid)).ToArray())
                    {
                        _EntryCatRelRepo.Delete(relation);
                    }
                    foreach (Category cat in message.Categories)
                    {
                        _CatRepo.Delete(cat);
                    }
                });

                outputPort.Handle(new DeleteCategoriesResponse(true, null));
                return(true);
            }
            catch (Exception ex)
            {
                outputPort.Handle(new DeleteCategoriesResponse(false, ex.ToString()));
                return(false);
            }
        }
Пример #2
0
        public async Task <IActionResult> Delete([FromRoute] DeleteCategoriesRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            var response = await _categoryService.DeleteListAsync(request);

            if (!response.IsValid)
            {
                return(BadRequest(response.Message));
            }
            return(Ok(response));
        }
Пример #3
0
        public async Task <BaseResponse> DeleteListAsync(DeleteCategoriesRequest request)
        {
            foreach (var categoryId in request.CategoriesIds)
            {
                var category = await _categoryRepository.GetAsync(category => category.Id == categoryId);

                if (category == null)
                {
                    return(new BaseResponse("Category is not found"));
                }
                _categoryRepository.Delete(category);
            }

            await _unitOfWork.SaveChangesAsync();

            return(new BaseResponse());
        }