示例#1
0
        public async Task <ActionResult <Currency> > WithDrawCurrency(int userid, [FromBody] CurrencyDTO currency)
        {
            if (currency is null)
            {
                return(NoContent());
            }
            var user = _repo.GetUserById(userid).Result;

            if (user is null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            var result = await _repo.IfExistCurrency(userid, currency.Count, currency.TypeOfCurrency);

            if (result)
            {
                var currencyForChange = user.Currencies.FirstOrDefault(c => c.TypeOfCurrency.Equals(currency.TypeOfCurrency));
                if (currencyForChange != null)
                {
                    currencyForChange.Count += currency.Count;
                }
                _repo.Update(currencyForChange);
            }
            else
            {
                await _repo.AddNewCurrency(userid, currency.Count, currency.TypeOfCurrency);
            }

            if (await _repo.SaveAll())
            {
                return(CreatedAtAction("GetCurrency", new { id = currency.Id }, currency));
            }

            throw new System.Exception($"Creating currency {currency.Id} filed on create");
        }