Example #1
0
        public async Task <Watchlist> UpdateAsync(Watchlist item)
        {
            if (item.MarketWatchlists.Count != item.MarketWatchlists.Distinct((i1, i2) => i1.MarketId == i2.MarketId).Count())
            {
                throw new InvalidDataException(ErrorCode.InvalidMarket, "Markets must be unique");
            }

            foreach (var watchlist in await _repository.GetAsync(w => w.Id != item.Id && w.UserId == item.UserId))
            {
                if (watchlist.Name == item.Name)
                {
                    throw new InvalidDataException(ErrorCode.InvalidMarket, "Watchlist name must be unique");
                }
            }

            foreach (var mw in item.MarketWatchlists)
            {
                if (!await _marketRepository.IsExistAsync(mw.MarketId))
                {
                    throw new NotFoundException(ErrorCode.MarketNotFount);
                }
            }

            Watchlist result = _repository.Update(item);
            await _repository.UnitOfWork.SaveChangesAsync();

            return(result);
        }