public async Task <Domain.Wish> HandleAsync(CreateWishRequest request) { var currentTimeUtc = DateTime.UtcNow.ToString(); byte[] encodedCurrentTimeUtc = Encoding.UTF8.GetBytes(currentTimeUtc); var options = new DistributedCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromSeconds(20)); await _cache.SetAsync("cachedTimeUTC", encodedCurrentTimeUtc, options); return(await _wishRepository.AddAsync(new Domain.Wish(request.Title, request.Description))); }
public async Task <WishResponse> SaveAsync(int userId, IEnumerable <Wish> wishes) { try { var existingUser = await _userRepository.FindByIdAsync(userId); if (existingUser == null) { return(new WishResponse($"Invalid user {userId}.")); } await DeleteAsync(userId); var errors = new List <string>(); foreach (var w in wishes) { var existingProduct = await _productRepository.FindByIdAsync(w.ProductId); if (existingProduct == null) { errors.Add($"Invalid product {w.ProductId}"); } if (errors.Count == 0) { w.UserId = userId; await _wishRepository.AddAsync(w); } } if (errors.Count > 0) { await DeleteAsync(userId); return(new WishResponse(errors.Aggregate((i, j) => i + ", " + j))); } await _unitOfWork.CompleteAsync(); _cache .GetKeys(CacheKeys.WishesList.ToString()) .ForEach(x => _cache.Remove(x) ); return(new WishResponse((Wish)null)); } catch (Exception ex) { return(new WishResponse($"An error occurred when saving the wish: {ex.Message}")); } }