Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] ProjectModel project)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string userIdVal = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            int    userId    = 0;

            if (!string.IsNullOrEmpty(userIdVal))
            {
                userId = Convert.ToInt32(userIdVal);
            }
            if (userId == 0)
            {
                return(BadRequest("Unauthorized user access to api"));
            }
            project.ProjectUrl = projectUrl;
            decimal exchangeRate = 1;

            exchangeRate = ratesService.GetCurrencyRateForDate(project.StartDate, project.ProjectCurrency);
            if (exchangeRate == 0)
            {
                string        apiKey        = ratesService.GetAPIKeyForOpenExchange();
                UtilityHelper utilityHelper = new UtilityHelper();
                string        dateString    = utilityHelper.FormatDateAsString(project.StartDate);
                var           rates         = await ratesHttpService.GetRatesForDateAsync(dateString, apiKey);

                if (rates.Rates != null)
                {
                    await ratesService.SaveCurrencyRatesAsync(rates.Rates, project.StartDate);

                    exchangeRate = projectService.GetExchangeRateForCurrency(project.ProjectCurrency, rates.Rates);
                }
            }

            project.ExchangeRate = exchangeRate;
            var response = await projectService.AddAsync(project, userId);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }
            return(Ok(response.ReturnedId));
        }