public FinanceResponse CreateResponse(FinanceException ex) { FinanceResponse fex = new FinanceResponse(); fex.Result = ex.HResult; fex.ErrMsg = ex.Message; return(fex); }
//重写基类的异常处理方法 public override void OnException(HttpActionExecutedContext actionExecutedContext) { //1.异常日志记录(正式项目里面一般是用log4net记录异常日志) logger.Error(actionExecutedContext.Exception); //2.返回调用方具体的异常信息 if (actionExecutedContext.Exception is FinanceException) { FinanceResponse fex = new FinanceResponse(); fex.Result = actionExecutedContext.Exception.HResult; fex.ErrMsg = actionExecutedContext.Exception.Message; actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.OK, fex); } base.OnException(actionExecutedContext); }
public async Task OnGetAsync(int id) { FinanceResponse finance = await _apiClient.GetFinanceAsync(id); this.Finance = new Finance { Id = finance.Id, BSPE = finance.BSPE, Minimum = finance.Minimum, Maximum = finance.Maximum, Month = finance.Month, Year = finance.Year, MonthlySum = finance.MonthlySum, PeopleCount = finance.PeopleCount }; }
public Financial getFinancials(string symbol, bool update) { // string to specify information to be retrieved from the API string IEXTrading_API_PATH = "stock/" + symbol + "/financials"; // initialize objects needed to gather data string financialsString = ""; Financial Financials = new Financial(); // connect to the API and obtain the response HttpResponseMessage response = httpClient.GetAsync(IEXTrading_API_PATH).GetAwaiter().GetResult(); // now, obtain the Json objects in the response as a string if (response.IsSuccessStatusCode) { financialsString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); // parse the string into appropriate objects if (!financialsString.Equals("") && !financialsString.Equals("{}")) { //If update, first remove all the entities associated with the symbol, and then insert it again. //Why do not update? because we cannot track a specific ChartElement //Since our ID is an autoincrement. if (update) { foreach (Financial currentEntity in dbContext.Financials.Where(f => f.symbol == symbol).ToList()) { dbContext.Remove(currentEntity); } } FinanceResponse FinanResponse = JsonConvert.DeserializeObject <FinanceResponse>(financialsString, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); Financials = FinanResponse.financials.OrderBy(f => f.reportDate).Last(); // The symbol serves as the foreign key in the database and connects the // financials to the company // Also save the data on the Database Financials.symbol = symbol; dbContext.Financials.Add(Financials); dbContext.SaveChanges(); } } return(Financials); }