private async Task RefreshExpenses() { Expenses.Clear(); // load expenses for group var client = ExpenseTrackerHttpClient.GetClient(); HttpRequestMessage request = new HttpRequestMessage(); request.Method = HttpMethod.Get; request.RequestUri = new Uri("api/expensegroups/" + ExpenseGroup.Id + "/expenses?fields=id,date,description,amount", UriKind.Relative); request.Headers.CacheControl = new CacheControlHeaderValue() { NoCache = true }; HttpResponseMessage response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var lstExpenses = JsonConvert.DeserializeObject <IEnumerable <Expense> >(content); Expenses = new ObservableCollection <Expense>(lstExpenses); } else { // something went wrong, log this, handle this, show message, ... } }
private async Task GetExpenseGroups() { // load open expense groups var client = ExpenseTrackerHttpClient.GetClient(); string userId = App.ExpenseTrackerIdentity.Claims.First (c => c.Name == "unique_user_key").Value; HttpResponseMessage response = await client .GetAsync("api/expensegroups?fields=id,title,description&status=open" + "&userId=" + userId); string content = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var lstEG = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroup> >(content); ExpenseGroups = new ObservableCollection <ExpenseGroup>(lstEG); } else { // something went wrong, log this, handle this, show message, ... } }
private async Task SaveExpense() { // partial update var client = ExpenseTrackerHttpClient.GetClient(); // create a patch document, containing the (possible) changes to the // expense DTO JsonPatchDocument <DTO.Expense> patchDoc = new JsonPatchDocument <DTO.Expense>(); patchDoc.Replace(e => e.Description, Expense.Description); // serialize & PATCH var serializedItemToUpdate = JsonConvert.SerializeObject(patchDoc); var response = client.PatchAsync("api/expenses/" + Expense.Id, new StringContent(serializedItemToUpdate, System.Text.Encoding.Unicode, "application/json")).Result; if (response.IsSuccessStatusCode) { // return to the expense list App.RootFrame.Navigate(typeof(ExpensesView), true); } else { // error: handle, log, ... } }
public async Task <ActionResult> Create(ExpenseGroup expenseGroup) { try { var client = ExpenseTrackerHttpClient.GetClient(); var claimsIdentity = this.User.Identity as ClaimsIdentity; var userId = claimsIdentity.FindFirst("unique_user_key").Value; // an expensegroup is created with status "Open", for the current user expenseGroup.ExpenseGroupStatusId = 1; expenseGroup.UserId = userId; var serializedItemToCreate = JsonConvert.SerializeObject(expenseGroup); var response = await client.PostAsync("api/expensegroups", new StringContent(serializedItemToCreate, System.Text.Encoding.Unicode, "application/json")); if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { return(Content("An error occurred")); } } catch { return(Content("An error occurred.")); } }
private void SaveBtn_OnClicked(object sender, EventArgs e) { Expense expense = new Expense() { ExpenseGroupId = ExpenseGroupId, Amount = Convert.ToDecimal(ExpenseAmount.Text), Description = ExpenseDescription.Text }; HttpClient client = ExpenseTrackerHttpClient.GetClient(); if (expense.Date < DateTime.Now.AddYears(-2)) { expense.Date = DateTime.Now; } var serializedItemToCreate = JsonConvert.SerializeObject(expense); var response = client.PostAsync("api/expenses", new StringContent(serializedItemToCreate, System.Text.Encoding.Unicode, "application/json")).Result; if (response.IsSuccessStatusCode) { Navigation.PopModalAsync(); } else { DisplayAlert("Alert", "Save was unsuccesfull", "Cancel"); } }
public async Task <ActionResult> Edit(int id, ExpenseGroup expenseGroup) { try { var client = ExpenseTrackerHttpClient.GetClient(); JsonPatchDocument <DTO.ExpenseGroup> patchDoc = new JsonPatchDocument <ExpenseGroup>(); patchDoc.Replace(eg => eg.Title, expenseGroup.Title); patchDoc.Replace(eg => eg.Description, expenseGroup.Description); var serializedItemToUpdate = JsonConvert.SerializeObject(patchDoc); var response = await client.PatchAsync("api/expensegroups/" + id, new StringContent(serializedItemToUpdate, System.Text.Encoding.Unicode, "application/json")); if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { return(Content("An error has occurred.")); } } catch { return(Content("An error has occurred.")); } }
public async Task <ActionResult> Edit(int id, Expense expense) { try { var client = ExpenseTrackerHttpClient.GetClient(); // create a JSON Patch Document JsonPatchDocument <DTO.Expense> patchDoc = new JsonPatchDocument <DTO.Expense>(); patchDoc.Replace(e => e.Amount, expense.Amount); patchDoc.Replace(e => e.Date, expense.Date); patchDoc.Replace(e => e.Description, expense.Description); // serialize and PATCH var serializedItemToUpdate = JsonConvert.SerializeObject(patchDoc); var response = await client.PatchAsync("api/expenses/" + id, new StringContent(serializedItemToUpdate, System.Text.Encoding.Unicode, "application/json")); if (response.IsSuccessStatusCode) { return(RedirectToAction("Details", "ExpenseGroups", new { id = expense.ExpenseGroupId })); } else { return(Content("An error occurred")); } } catch { return(Content("An error occurred")); } }
public async Task <ActionResult> Create(ExpenseGroup expenseGroup) { try { var client = ExpenseTrackerHttpClient.GetClient(); expenseGroup.ExpenseGroupStatusId = 1; expenseGroup.UserId = @"https://expensetrackeridsrv3/embedded_1"; var serializedItemToCreate = JsonConvert.SerializeObject(expenseGroup); var response = await client.PostAsync("api/expensegroups" , new StringContent(serializedItemToCreate, System.Text.Encoding.Unicode, "application/json")); if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { return(Content("An error occurred.")); } } catch (Exception) { return(Content("An error occurred.")); } }
public async Task <ActionResult> Edit(int id, ExpenseGroup expenseGroup) { try { var client = ExpenseTrackerHttpClient.GetClient(); var serializedItemToUpdate = JsonConvert.SerializeObject(expenseGroup); var response = await client.PutAsync("api/expensegroups/" + id, new StringContent(serializedItemToUpdate, System.Text.Encoding.Unicode, "application/json")); if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { return(Content("An error occurred.")); } } catch (Exception) { return(Content("An error occurred.")); } }
public async Task <ActionResult> Create(Expense expense) { try { var client = ExpenseTrackerHttpClient.GetClient(); // serialize & POST var serializedItemToCreate = JsonConvert.SerializeObject(expense); var response = await client.PostAsync("api/expenses", new StringContent(serializedItemToCreate, System.Text.Encoding.Unicode, "application/json")); if (response.IsSuccessStatusCode) { return(RedirectToAction("Details", "ExpenseGroups", new { id = expense.ExpenseGroupId })); } else { return(Content("An error occurred")); } } catch { return(Content("An error occurred")); } }
public async Task <ActionResult> Create(ExpenseGroup expenseGroup) { try { var client = ExpenseTrackerHttpClient.GetClient(); // an expensegroup is created with status "Open", for the current user expenseGroup.ExpenseGroupStatusId = 1; expenseGroup.UserId = @"https://expensetrackeridsrv3/embedded_1"; var serializedItemToCreate = JsonConvert.SerializeObject(expenseGroup); var response = await client.PostAsync("api/expensegroups", new StringContent(serializedItemToCreate, System.Text.Encoding.Unicode, "application/json")); if (response.IsSuccessStatusCode) { // If everything went good we want to redirect to the list of ExpenseGroups return(RedirectToAction("Index")); } else { return(Content("An error occurred.")); } } catch { return(Content("An error occurred.")); } }
private async void SetNewExpenseGroup(DTO.ExpenseGroup expenseGroup) { // a new expense group has been clicked. Set the expense group, and // load the expenses for this expense group. ExpenseGroup = expenseGroup; // load expenses for group Expenses.Clear(); var client = ExpenseTrackerHttpClient.GetClient(); HttpResponseMessage response = await client.GetAsync("api/expensegroups/" + ExpenseGroup.Id + "/expenses?fields=id,date,description,amount"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var lstExpenses = JsonConvert.DeserializeObject <IEnumerable <Expense> >(content); Expenses = new ObservableCollection <Expense>(lstExpenses); } else { // something went wrong, log this, handle this, show message, ... } }
public async Task <ActionResult> Index(int?page = 1) { var client = ExpenseTrackerHttpClient.GetClient(); var model = new ExpenseGroupsViewModel(); // Calling the GET method on API HttpResponseMessage egsResponse = await client.GetAsync("api/expensegroupstatusses"); if (egsResponse.IsSuccessStatusCode) { string egsContent = await egsResponse.Content.ReadAsStringAsync(); var lstExpenseGroupStatusses = JsonConvert .DeserializeObject <IEnumerable <ExpenseGroupStatus> >(egsContent); model.ExpenseGroupStatusses = lstExpenseGroupStatusses; } else { return(Content("An error occurred.")); } // Without sorting //HttpResponseMessage response = await client.GetAsync("api/expensegroups"); // With sorting. Expense groups will be sorted first by status code and then by title //HttpResponseMessage response = await client.GetAsync("api/expensegroups?sort=expensegroupstatusid,title"); // With paging. We already have suport for paging in API but we need to add new support in the client. // I added PagingInfo.cs & HeaderParser.cs helper classes to implement paging on this MVC client. HttpResponseMessage response = await client.GetAsync("api/expensegroups?sort=expensegroupstatusid,title&page=" + page + "&pagesize=5"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var pagingInfo = HeaderParser.FindAndParsePagingInfo(response.Headers); // Also, added NuGet package pagedlist.mvc to implement paging var lstExpenseGroups = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroup> >(content); var pagedExpenseGroupsList = new StaticPagedList <ExpenseGroup>(lstExpenseGroups, pagingInfo.CurrentPage, pagingInfo.PageSize, pagingInfo.TotalCount); model.ExpenseGroups = pagedExpenseGroupsList; model.PagingInfo = pagingInfo; } else { return(Content("An error occurred.")); } return(View(model)); }
// GET: ExpenseGroup public async Task <ActionResult> Index(int?page = 1) { var client = ExpenseTrackerHttpClient.GetClient(); var model = new ExpenseGroupsViewModel(); var egsResponse = await client.GetAsync("api/expensegroupstatusses"); if (egsResponse.IsSuccessStatusCode) { string egsContent = await egsResponse.Content.ReadAsStringAsync(); var lstExpenseGroupStatusses = JsonConvert .DeserializeObject <IEnumerable <ExpenseGroupStatus> >(egsContent); model.ExpenseGroupStatusses = lstExpenseGroupStatusses; } else { return(Content("An error occurred.")); } // HttpResponseMessage response = await client.GetAsync("api/expensegroups?sort=expensegroupstatusid,title"); HttpResponseMessage response = await client.GetAsync("api/expensegroups?sort=expensegroupstatusid,title&page=" + page + "&pagesize=5"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var pagingInfo = HeaderParser.FindAndParsePagingInfo(response.Headers); var lstExpenseGroups = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroup> >(content); // model.ExpenseGroups = lstExpenseGroups; var pagedExpenseGroupsList = new StaticPagedList <ExpenseGroup>(lstExpenseGroups, pagingInfo.CurrentPage, pagingInfo.PageSize, pagingInfo.TotalCount); model.ExpenseGroups = pagedExpenseGroupsList; model.PagingInfo = pagingInfo; } else { return(Content("An error occurred.")); } return(View(model)); }
public async Task <ActionResult> Index(int?page = 1) { //calling get or post happen as async mode var client = ExpenseTrackerHttpClient.GetClient(); var model = new ExpenseGroupsViewModel(); // GET Expense Group Status var egsResponse = await client.GetAsync("api/expensegroupstatusses"); if (egsResponse.IsSuccessStatusCode) { string egsContent = await egsResponse.Content.ReadAsStringAsync(); //deserelized the response to get oreginal data var lstExpenseGroupStatusses = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroupStatus> >(egsContent); model.ExpenseGroupStatusses = lstExpenseGroupStatusses; } else { return(Content("An error occurred.")); } // GET Expense Group HttpResponseMessage response = await client.GetAsync("api/expensegroups?sort=expensegroupstatusid,title&page=" + page + "&pagesize=5"); //done sorting if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); //paging var pagingInfo = HeaderParser.FindAndParsePagingInfo(response.Headers); //deserelized the response and get the data as IEnumerable var lstExpenseGroups = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroup> >(content); //then it convert to IPagedList inerface for pageing var pagedExpenseGroupsList = new StaticPagedList <ExpenseGroup>(lstExpenseGroups, pagingInfo.CurrentPage, pagingInfo.PageSize, pagingInfo.TotalCount); model.ExpenseGroups = pagedExpenseGroupsList; model.PagingInfo = pagingInfo; } else { return(Content("An error occured.")); } return(View(model)); }
public async Task <ActionResult> Index() { var client = ExpenseTrackerHttpClient.GetClient(); HttpResponseMessage response = await client.GetAsync("api/expensegroups"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroup> >(content); return(View(model.ToList())); } return(Content("An error occured")); }
public async Task <ActionResult> Index(int?page = 1) { var client = ExpenseTrackerHttpClient.GetClient(); var model = new ExpenseGroupsViewModel(); HttpResponseMessage egsResponse = await client.GetAsync("api/expensegroupstatusses"); if (egsResponse.IsSuccessStatusCode) { string egsContent = await egsResponse.Content.ReadAsStringAsync(); var lstExpenseGroupStatusses = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroupStatus> >(egsContent); model.ExpenseGroupStatusses = lstExpenseGroupStatusses; } else { return(Content("An error occurred.")); } string userId = (this.User.Identity as ClaimsIdentity).FindFirst("unique_user_key").Value; HttpResponseMessage response = await client.GetAsync("api/expensegroups?sort=expensegroupstatusid" + ",title&page=" + page + "&pagesize=5&userid=" + userId); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); // get the paging info from the header var pagingInfo = HeaderParser.FindAndParsePagingInfo(response.Headers); var lstExpenseGroups = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroup> >(content); var pagedExpenseGroupsList = new StaticPagedList <ExpenseGroup>(lstExpenseGroups, pagingInfo.CurrentPage, pagingInfo.PageSize, pagingInfo.TotalCount); model.ExpenseGroups = pagedExpenseGroupsList; model.PagingInfo = pagingInfo; } else { return(Content("An error occurred.")); } return(View(model)); }
// GET: ExpenseGroups/Edit/5 public async Task <ActionResult> Edit(int id) { var client = ExpenseTrackerHttpClient.GetClient(); HttpResponseMessage response = await client.GetAsync("api/expensegroups/" + id); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <ExpenseGroup>(content); return(View(model)); } return(Content("An error occurred.")); }
// GET: ExpenseGroups/Edit/5 // Clicking the Edit link for any expense group will bring you to this method. This method // returns back the Edit.cshtml view. Clicking the Save button on Edit.cshtml // will trigger POST and call the Edit method below. public async Task <ActionResult> Edit(int id) { var client = ExpenseTrackerHttpClient.GetClient(); HttpResponseMessage response = await client.GetAsync("api/expensegroups/" + id + "?fields=id,title,description"); // Data shaping - requesting only specific fields from the API string content = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var model = JsonConvert.DeserializeObject <ExpenseGroup>(content); return(View(model)); } return(Content("An error occurred:" + content)); }
// GET: ExpenseGroups/Details/5 public async Task <ActionResult> Details(int id) { var client = ExpenseTrackerHttpClient.GetClient(); // wer get required the id,description,title fo expensegroup and related expenses for fatching (data association) HttpResponseMessage response = await client.GetAsync("api/expensegroups/" + id + "?fields=id,description,title,expenses"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <ExpenseGroup>(content); return(View(model)); } return(Content("An error occurred")); }
// GET: ExpenseGroups/Details/5 public async Task <ActionResult> Details(int id) { var client = ExpenseTrackerHttpClient.GetClient(); // Data shaping - we are asking for specific fields and we are asking for expenses (which is an example of associations) HttpResponseMessage response = await client.GetAsync("api/expensegroups/" + id + "?fields=id,description,title,expenses"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <ExpenseGroup>(content); return(View(model)); } return(Content("An error occurred")); }
// GET: ExpenseGroups/Edit/5 public async Task <ActionResult> Edit(int id) { //fill up the view of existing data var client = ExpenseTrackerHttpClient.GetClient(); HttpResponseMessage response = await client.GetAsync("api/expensegroups/" + id + "?fields=id,title,description"); //filter with if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); //deserelized the response var model = JsonConvert.DeserializeObject <ExpenseGroup>(content); return(View(model)); } return(Content("An error occurred.")); }
// GET: Expenses/Edit/5 public async Task <ActionResult> Edit(int id) { // get version 2 var client = ExpenseTrackerHttpClient.GetClient("2"); HttpResponseMessage response = await client.GetAsync("api/expenses/" + id + "?fields=id,description,date,amount"); string content = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var model = JsonConvert.DeserializeObject <Expense>(content); return(View(model)); } return(Content("An error occurred: " + content)); }
private async Task GetExpenseGroups() { // load open expense groups var client = ExpenseTrackerHttpClient.GetClient(); HttpResponseMessage response = await client .GetAsync("api/expensegroups?fields=id,title,description"); string content = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var lstEG = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroup> >(content); ExpenseGroups = new ObservableCollection <ExpenseGroup>(lstEG); } else { // something went wrong, log this, handle this, show message, ... } }
// GET: Expenses/Edit/5 public async Task <ActionResult> Edit(int id) { // Calling V2 version of GET method using Custom request header method. We pass in "2" which gets mapped to V2 var client = ExpenseTrackerHttpClient.GetClient("2"); HttpResponseMessage response = await client.GetAsync("api/expenses/" + id + "?fields=id,description,date,amount"); // Data shaping - requesting only specific fields: id, description, date & amount // Calling V2 version of GET method using URI //HttpResponseMessage response = await client.GetAsync("api/v2/expenses/" + id + "?fields=id,description,date,amount"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <Expense>(content); return(View(model)); } return(Content("An error occurred.")); }
public async Task <ActionResult> Index() { var client = ExpenseTrackerHttpClient.GetClient(); var model = new ExpenseGroupsViewModel(); var egsResponse = await client.GetAsync("api/expensegroupstatusses"); if (egsResponse.IsSuccessStatusCode) { string egsContent = await egsResponse.Content.ReadAsStringAsync(); var lstExpenseGroupStatusses = JsonConvert .DeserializeObject <IEnumerable <ExpenseGroupStatus> >(egsContent); model.ExpenseGroupStatusses = lstExpenseGroupStatusses; } else { return(Content("An error occurred.")); } HttpResponseMessage response = await client.GetAsync("api/expensegroups"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var lstExpenseGroups = JsonConvert.DeserializeObject <IEnumerable <ExpenseGroup> >(content); model.ExpenseGroups = lstExpenseGroups; } else { return(Content("An error occurred.")); } return(View(model)); }
// POST: ExpenseGroups/Delete/5 public async Task <ActionResult> Delete(int id) { try { var client = ExpenseTrackerHttpClient.GetClient(); var response = await client.DeleteAsync("api/expensegroups/" + id); if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { return(Content("An error occurred.")); } } catch (Exception) { return(Content("An error occurred.")); } }
// GET: Expenses/Delete/5 public async Task <ActionResult> Delete(int expenseGroupId, int id) { try { var client = ExpenseTrackerHttpClient.GetClient(); var response = await client.DeleteAsync("api/expenses/" + id); if (response.IsSuccessStatusCode) { return(RedirectToAction("Details", "ExpenseGroups", new { id = expenseGroupId })); } else { return(Content("An error occurred")); } } catch { return(Content("An error occurred")); } }
// GET: ExpenseGroups/Details/5 public async Task <ActionResult> Details(int id) { try { var client = ExpenseTrackerHttpClient.GetClient(); HttpResponseMessage response = await client.GetAsync("api/expensegroups/" + id + "?fields=id,description,title,expenses"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var model = JsonConvert.DeserializeObject <ExpenseGroup>(content); return(View(model)); } return(Content("An error occurred.")); } catch (Exception) { return(Content("An error occurred.")); } }
private async Task AddExpense() { // create an expense var client = ExpenseTrackerHttpClient.GetClient(); // serialize & POST var serializedItemToCreate = JsonConvert.SerializeObject(Expense); var response = client.PostAsync("api/expenses", new StringContent(serializedItemToCreate, System.Text.Encoding.Unicode, "application/json")).Result; if (response.IsSuccessStatusCode) { // return to the expense overview App.RootFrame.Navigate(typeof(ExpensesView), true); } else { // handle, log, ... } }