Пример #1
0
        public async Task <IActionResult> Create(Transaction transaction)
        {
            if (!_hBCurrencyController.CheckCurrencyExists(transaction.CurrencyName))
            {
                ModelState.AddModelError("CurrencyName", $"Валюта {transaction.CurrencyName} не существует");
            }
            else if (transaction.Date > DateTime.Now)
            {
                ModelState.AddModelError("Date", $"Дата Транзакции {transaction.Date} не может быть больше текущей даты");
            }

            if (ModelState.IsValid)
            {
                await _currencyExchDatesController.Exchange(transaction.CurrencyName, transaction.Date.ToString());

                var entity = _dBContext.Transactions.Add(transaction);
                await _dBContext.SaveChangesAsync();

                return(Ok(entity.Entity));
            }

            List <string> errList = new List <string>();

            foreach (var modelState in ViewData.ModelState.Values)
            {
                foreach (var error in modelState.Errors)
                {
                    errList.Add(error.ErrorMessage);
                }
            }
            return(BadRequest(errList));
        }
Пример #2
0
        public async Task <ActionResult <Currency> > Exchange(string currencyName, string date)
        {
            if (DateTime.TryParse(date, out DateTime dateParam) && _hBCurrencyController.CheckCurrencyExists(currencyName))
            {
                var currency = await getCurrExch(currencyName, dateParam);

                return(Ok(currency));
            }
            return(BadRequest("Invalid input params: " + HttpContext.Request.QueryString));
        }