public IActionResult CreateFund([FromBody] InvestmentFundViewModel _fund) { if (ModelState.IsValid) { try { _fund.IsActive = _fund.IsActive.Trim(); _fund.IsSingle = _fund.IsSingle.Trim(); var existingFund = this.context.InvestmentFund.Where(b => b.Apircode == _fund.Apircode).FirstOrDefault(); if (existingFund == null) { var fund = this.mapper.Map <InvestmentFund>(_fund); this.context.InvestmentFund.Add(fund); this.context.SaveChanges(); var result = this.mapper.Map <InvestmentFundViewModel>(fund); return(Ok(result)); } else { this.mapper.Map <InvestmentFundViewModel, InvestmentFund>(_fund, existingFund); this.context.InvestmentFund.Update(existingFund); this.context.SaveChanges(); var result = this.mapper.Map <InvestmentFundViewModel>(_fund); return(Ok(result)); } } catch (Exception ex) { throw ex; } } else { return(BadRequest(ModelState)); } }
public void SendAsync() { try { string url = ""; url = @"https://api.morningstar.com/v2/service/mf/ugkyhaskr79mdqge/universeid/y4266l541ybiknfe?accesscode=sklfgghyv7g0fl51klfheqdue7ol0te1"; using (var response = ApiHelper.ApiClient.GetAsync(url).Result) { if (response.IsSuccessStatusCode) { var responseContent = response.Content; // return responseContent.ReadAsStringAsync().Result; var content = response.Content.ReadAsStringAsync().Result; XmlDocument doc = new XmlDocument(); doc.LoadXml(content); string jsonText = JsonConvert.SerializeXmlNode(doc); var res = JObject.Parse(jsonText); var data1 = (res["response"] as JObject); var apiDet = (data1["data"] as JArray).ToString(); var result = JsonConvert.DeserializeObject<List<APIDetailsModel>>(apiDet, Converter.Settings); List<InvestmentFundViewModel> fundArray = new List<InvestmentFundViewModel>(); foreach (APIDetailsModel api in result) { if (api.Api.Apircode != null && api.Api.Apircode != "") { InvestmentFundViewModel _fund = new InvestmentFundViewModel(); _fund.Apircode = api.Api.Apircode; _fund.MId = api.Api.MId; _fund.FundName = api.Api.FundName; _fund.BuySpread = api.Api.BuySpread; _fund.Icr = api.Api.Icr; if (api.Api.AssetAllocation.HasValue && api.Api.AssetAllocation != null) { if (api.Api.AssetAllocation.Value.AssetAllocationElementArray != null) { _fund.DomesticEquity = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticEquity").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticEquity").Sum(c => c.Value) : 0; _fund.InternationalEquity = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalEquity").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalEquity").Sum(c => c.Value) : 0; _fund.DomesticProperty = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticProperty").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticProperty").Sum(c => c.Value) : 0; _fund.InternationalProperty = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalProperty").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalProperty").Sum(c => c.Value) : 0; _fund.GrowthAlternatives = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "GrowthAlternatives").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "GrowthAlternatives").Sum(c => c.Value) : 0; _fund.DefensiveAlternatives = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DefensiveAlternatives").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DefensiveAlternatives").Sum(c => c.Value) : 0; _fund.DomesticFixedInterest = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticFixedInterest").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticFixedInterest").Sum(c => c.Value) : 0; _fund.InternationalFixedInterest = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalFixedInterest").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalFixedInterest").Sum(c => c.Value) : 0; _fund.DomesticCash = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticCash").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticCash").Sum(c => c.Value) : 0; _fund.InternationalCash = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalCash").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalCash").Sum(c => c.Value) : 0; _fund.OtherGrowth = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "OtherGrowth").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "OtherGrowth").Sum(c => c.Value) : 0; } else if (api.Api.AssetAllocation.Value.AssetAllocationElement != null) { _fund.DomesticEquity = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DomesticEquity" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.InternationalEquity = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "InternationalEquity" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.DomesticProperty = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DomesticProperty" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.InternationalProperty = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "InternationalProperty" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.GrowthAlternatives = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "GrowthAlternatives" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.DefensiveAlternatives = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DefensiveAlternatives" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.DomesticFixedInterest = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DomesticFixedInterest" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.InternationalFixedInterest = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "InternationalFixedInterest" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.DomesticCash = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DomesticCash" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.InternationalCash = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "InternationalCash" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; _fund.OtherGrowth = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "OtherGrowth" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; } } else { _fund.DomesticEquity = 0; _fund.InternationalEquity = 0; _fund.DomesticProperty = 0; _fund.InternationalProperty = 0; _fund.GrowthAlternatives = 0; _fund.DefensiveAlternatives = 0; _fund.DomesticFixedInterest = 0; _fund.InternationalFixedInterest = 0; _fund.DomesticCash = 0; _fund.InternationalCash = 0; _fund.OtherGrowth = 0; } _fund.UpdatedOn = DateTime.Now; fundArray.Add(_fund); } } if (fundArray.Count > 0) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { foreach (InvestmentFundViewModel _fund in fundArray) { var existingFund = this.context.InvestmentFund.Where(b => b.Apircode == _fund.Apircode).FirstOrDefault(); if (existingFund == null) { var fund = mapper.Map<InvestmentFund>(_fund); this.context.InvestmentFund.Add(fund); this.context.SaveChanges(); } else { this.mapper.Map<InvestmentFundViewModel, InvestmentFund>(_fund, existingFund); this.context.InvestmentFund.Update(existingFund); this.context.SaveChanges(); } } dbContextTransaction.Commit(); } catch (Exception ex) { throw ex; } } } } else { throw new Exception(response.ReasonPhrase); } } //using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url)) //{ // if (response.IsSuccessStatusCode) // { // var content = await response.Content.ReadAsStringAsync(); // XmlDocument doc = new XmlDocument(); // doc.LoadXml(content); // string jsonText = JsonConvert.SerializeXmlNode(doc); // var res = JObject.Parse(jsonText); // var data1 = (res["response"] as JObject); // var apiDet = (data1["data"] as JArray).ToString(); // var result = JsonConvert.DeserializeObject<List<APIDetailsModel>>(apiDet, Converter.Settings); // List<InvestmentFundViewModel> fundArray = new List<InvestmentFundViewModel>(); // foreach (APIDetailsModel api in result) // { // if (api.Api.Apircode != null && api.Api.Apircode != "") // { // InvestmentFundViewModel _fund = new InvestmentFundViewModel(); // _fund.Apircode = api.Api.Apircode; // _fund.MId = api.Api.MId; // _fund.FundName = api.Api.FundName; // _fund.BuySpread = api.Api.BuySpread; // _fund.Icr = api.Api.Icr; // if (api.Api.AssetAllocation.HasValue && api.Api.AssetAllocation != null) // { // if (api.Api.AssetAllocation.Value.AssetAllocationElementArray != null) // { // _fund.DomesticEquity = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticEquity").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticEquity").Sum(c => c.Value) : 0; // _fund.InternationalEquity = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalEquity").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalEquity").Sum(c => c.Value) : 0; // _fund.DomesticProperty = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticProperty").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticProperty").Sum(c => c.Value) : 0; // _fund.InternationalProperty = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalProperty").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalProperty").Sum(c => c.Value) : 0; // _fund.GrowthAlternatives = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "GrowthAlternatives").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "GrowthAlternatives").Sum(c => c.Value) : 0; // _fund.DefensiveAlternatives = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DefensiveAlternatives").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DefensiveAlternatives").Sum(c => c.Value) : 0; // _fund.DomesticFixedInterest = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticFixedInterest").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticFixedInterest").Sum(c => c.Value) : 0; // _fund.InternationalFixedInterest = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalFixedInterest").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalFixedInterest").Sum(c => c.Value) : 0; // _fund.DomesticCash = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticCash").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "DomesticCash").Sum(c => c.Value) : 0; // _fund.InternationalCash = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalCash").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "InternationalCash").Sum(c => c.Value) : 0; // _fund.OtherGrowth = api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "OtherGrowth").Any() ? api.Api.AssetAllocation.Value.AssetAllocationElementArray.Where(c => c.Type.ToString() == "OtherGrowth").Sum(c => c.Value) : 0; // } // else if (api.Api.AssetAllocation.Value.AssetAllocationElement != null) // { // _fund.DomesticEquity = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DomesticEquity" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.InternationalEquity = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "InternationalEquity" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.DomesticProperty = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DomesticProperty" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.InternationalProperty = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "InternationalProperty" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.GrowthAlternatives = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "GrowthAlternatives" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.DefensiveAlternatives = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DefensiveAlternatives" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.DomesticFixedInterest = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DomesticFixedInterest" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.InternationalFixedInterest = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "InternationalFixedInterest" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.DomesticCash = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "DomesticCash" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.InternationalCash = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "InternationalCash" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // _fund.OtherGrowth = api.Api.AssetAllocation.Value.AssetAllocationElement.Type.ToString() == "OtherGrowth" ? api.Api.AssetAllocation.Value.AssetAllocationElement.Value : 0; // } // } // else // { // _fund.DomesticEquity = 0; // _fund.InternationalEquity = 0; // _fund.DomesticProperty = 0; // _fund.InternationalProperty = 0; // _fund.GrowthAlternatives = 0; // _fund.DefensiveAlternatives = 0; // _fund.DomesticFixedInterest = 0; // _fund.InternationalFixedInterest = 0; // _fund.DomesticCash = 0; // _fund.InternationalCash = 0; // _fund.OtherGrowth = 0; // } // _fund.UpdatedOn = DateTime.Now; // fundArray.Add(_fund); // } // } // if (fundArray.Count > 0) // { // using (var dbContextTransaction = context.Database.BeginTransaction()) // { // try // { // foreach (InvestmentFundViewModel _fund in fundArray) // { // var existingFund = this.context.InvestmentFund.Where(b => b.Apircode == _fund.Apircode).FirstOrDefault(); // if (existingFund == null) // { // var fund = mapper.Map<InvestmentFund>(_fund); // this.context.InvestmentFund.Add(fund); // this.context.SaveChanges(); // } // else // { // this.mapper.Map<InvestmentFundViewModel, InvestmentFund>(_fund, existingFund); // this.context.InvestmentFund.Update(existingFund); // this.context.SaveChanges(); // } // } // dbContextTransaction.Commit(); // } // catch (Exception ex) // { // throw ex; // } // } // } // } // else // { // throw new Exception(response.ReasonPhrase); // } //} } catch (Exception ex) { throw ex; } }