Esempio n. 1
0
 public static WatcherStatus BuildStatus(Watcher watcher)
 {
     // Evaluate and return
     if (WatcherExpression.WatcherNotSet().Invoke(watcher))
     {
         return(WatcherStatus.NOT_SET);
     }
     if (WatcherExpression.WatcherBuying().Invoke(watcher))
     {
         return(WatcherStatus.BUYING);
     }
     if (WatcherExpression.WatcherSelling().Invoke(watcher))
     {
         return(WatcherStatus.SELLING);
     }
     if (WatcherExpression.WatcherBought().Invoke(watcher))
     {
         return(WatcherStatus.BOUGHT);
     }
     if (WatcherExpression.WatcherHolding().Invoke(watcher))
     {
         return(WatcherStatus.HOLDING);
     }
     if (WatcherExpression.WatcherSold().Invoke(watcher))
     {
         return(WatcherStatus.SOLD);
     }
     throw new NotImplementedException();
 }
Esempio n. 2
0
        public async Task <Responses.Watcher> SetWatcher(SetWatcher request)
        {
            // Get watcher
            var watcher = await _mainDbContext.Watchers.FindAsync(request.WatcherId);

            // Watcher not found
            if (watcher == null)
            {
                throw new NotFoundException(WatcherMessage.WatcherNotFound);
            }

            // Buy limit must be lower than watcher value
            if (WatcherExpression.BuyLimitMustBeLowerThanWatcherValue(request.Buy).Invoke(watcher))
            {
                throw new ConflictException(new Conflict <SetWatcherConflictReason>(SetWatcherConflictReason.BUY_LIMIT_MUST_BE_LOWER_THAN_WATCHER_VALUE, string.Format(WatcherMessage.BuyLimitMustBeLowerThanWatcherValue, watcher.Value)));
            }

            // Sell limit must be higher than watcher value
            if (WatcherExpression.SellLimitMustBeHigherThanWatcherValue(request.Sell).Invoke(watcher))
            {
                throw new ConflictException(new Conflict <SetWatcherConflictReason>(SetWatcherConflictReason.SELL_LIMIT_MUST_BE_HIGHER_THAN_WATCHER_VALUE, string.Format(WatcherMessage.SellLimitMustBeHigherThanWatcherValue, watcher.Value)));
            }

            // Watcher already got liquidated
            if (WatcherExpression.WatcherSold().Invoke(watcher))
            {
                throw new ConflictException(new Conflict <SetWatcherConflictReason>(SetWatcherConflictReason.WATCHER_ALREADY_LIQUIDATED, WatcherMessage.WatcherAlreadyLiquidated));
            }

            // Watcher already bought
            if (WatcherExpression.WatcherAlreadyBought(request.Buy).Invoke(watcher))
            {
                throw new ConflictException(new Conflict <SetWatcherConflictReason>(SetWatcherConflictReason.WATCHER_ALREADY_BOUGHT, string.Format(WatcherMessage.WatcherAlreadyBought, watcher.EntryPrice)));
            }

            // Watcher already sold
            if (WatcherExpression.WatcherAlreadySold(request.Sell).Invoke(watcher))
            {
                throw new ConflictException(new Conflict <SetWatcherConflictReason>(SetWatcherConflictReason.WATCHER_ALREADY_SOLD, string.Format(WatcherMessage.WatcherAlreadySold, watcher.EntryPrice)));
            }

            // Set watcher
            watcher.Set(request.Buy, request.Sell, request.Quantity);

            // Update
            _mainDbContext.Watchers.Update(watcher);

            // Save
            await _mainDbContext.SaveChangesAsync();

            // Response
            var response = _mapper.Map <Responses.Watcher>(watcher);

            // Log
            _logger.LogInformation("{@Event}, {@UserId}, {@Request}, {@Response}", "WatcherSet", request.UserId, request, response);

            // Return
            return(response);
        }
        public void Test_WatcherLiquidated()
        {
            // Arrange
            var watchersBuyingAndSelling = FakeWatcher.GetWatchersLiquidated();

            // Act
            var filter = watchersBuyingAndSelling.Where(WatcherExpression.WatcherSold()).ToList();

            // Assert
            Assert.AreEqual(watchersBuyingAndSelling.Count, filter.Count);
        }