/// <summary> /// gets the package id from ui and calls the AddMostCalledNums method from server using http /// </summary> public MostCalled AddMostCalledNums(int packageId, string num1, string num2, string num3) { try { using (var client = new HttpClient()) { client.BaseAddress = new Uri(url); var mostCalled = new MostCalled(packageId, num1, num2, num3); string json = JsonConvert.SerializeObject(mostCalled); var result = client.PostAsync("api/crm/mostcalled", new StringContent(json, System.Text.Encoding.UTF8, "application/json")).Result; if (result.IsSuccessStatusCode) { string response = result.Content.ReadAsStringAsync().Result; return(JsonConvert.DeserializeObject <MostCalled>(response)); } else { throw new Exception(result.Content.ReadAsStringAsync().Result); } } } catch (Exception e) { log.LogWrite("Add most called error: " + e.Message); throw new Exception("Add most called exception: " + e.Message); } }
public HttpResponseMessage CreateMostCalled([FromBody] MostCalled mostCalled) { try { var m = DAL.AddMostCalled(mostCalled); return(Request.CreateResponse(HttpStatusCode.OK, m)); } catch (ArgumentException e) { return(Request.CreateErrorResponse(HttpStatusCode.Conflict, e.Message)); } catch (KeyNotFoundException e) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, e.Message)); } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message)); } }
/// <summary> /// add most called numbers to db - if package not in db throw exception /// </summary> public MostCalled AddMostCalled(MostCalled mostCalled) { try { using (CellularModel context = new CellularModel()) { var packageFromDb = context.Packages.SingleOrDefault(p => p.PackageId == mostCalled.PackageId); if (packageFromDb == null) { throw new KeyNotFoundException("package not exits, unable to add most called"); } context.MostCalled.Add(mostCalled); context.SaveChanges(); return(mostCalled); } } catch (Exception e) { log.LogWrite("Add most called Dal error: " + e.Message); throw new Exception(e.Message); } }