public async Task<CategoryProduct> Add(CategoryProduct product) { if (_categoryproductRepository.Search(b => b.Id == product.Id).Result.Any()) return null; await _categoryproductRepository.Add(product); return product; }
public async Task Handle(CategoryCreateEvent @event) { try { var categoryEvent = @event.CategoryEvent; var categoryProduct = new CategoryProduct(categoryEvent.Id, categoryEvent.Name, categoryEvent.Image, categoryEvent.CreatedAt); var result = await _categoryProductRepository.GetCategoryProductsByDocumentId(categoryEvent.Id); if (result == null) { _categoryProductRepository.Add(categoryProduct); await _unitOfWork.Commit(); var categoryProducts = await _categoryProductRepository.GetAll(); var categories = categoryProducts.Where(x => x.Status == "A") .GroupBy(g => g.Name) .Select(s => new CategoryResponse { Id = s.FirstOrDefault().Id, Name = s.FirstOrDefault().Name, Image = s.FirstOrDefault().Image }).ToList(); if (categories.Any()) { _categoryRedisRepository.Remove(KEY_CACHE, (int)RedisBase.Category); } _categoryRedisRepository.Set(KEY_CACHE, categories, (int)RedisBase.Category); } } catch (Exception ex) { } }