Esempio n. 1
0
    public async Task <DalResult> MakeMovementAsync(int beanid, decimal lowestValue, DateTime date)
    {
        if (beanid <= 0)
        {
            return(DalResult.NotFound);
        }
        var bean = await _beanRepository.ReadAsync(beanid);

        if (bean is null)
        {
            return(DalResult.NotFound);
        }
        if (await MovementExistsAsync(beanid, date))
        {
            return(DalResult.Success);
        }
        if (lowestValue < 0)
        {
            lowestValue = Constants.MinimumBeanPrice;
        }
        var startingPrice = bean.Price;
        var breakpoint    = _breakpointManager.GenerateBreakpoint();
        var multiplier    = _breakpointManager.GetMultiplier(breakpoint);
        var amount        = (double)startingPrice * (_normalRandom.Next() / 100) * multiplier;

        bean.Price += (decimal)amount;
        if (bean.Price < lowestValue)
        {
            bean.Price = lowestValue;
        }
        var movement = new MovementEntity
        {
            Id           = 0,
            BeanId       = beanid,
            MovementDate = date,
            Open         = startingPrice,
            Close        = bean.Price,
            Movement     = bean.Price - startingPrice,
            MovementType = _breakpointManager.GetMovementType(breakpoint)
        };
        var result = await InsertAsync(movement, bean);

        return(result);
    }