public ActionResult CheckError(HttpResponseMessage response, CreateEditTransactionViewModel model) { if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { var data = response.Content.ReadAsStringAsync().Result; var result = JsonConvert.DeserializeObject <APIErrorData>(data); if ((result.ModelState != null) && (model != null)) { var modelErrors = result.ModelState.SingleOrDefault().Value; foreach (var error in modelErrors) { ModelState.AddModelError("", error); } return(View(model)); } else { TempData["Error"] = result.Message; return(RedirectToAction("GetHouseholds", "Household")); } } return(View("Error")); }
public ActionResult Edit(int id, CreateEditTransactionViewModel model) { var httpClient = HttpContext.Items["httpClient"] as HttpClient; var response = httpClient.PutAsync(url + $"PutTransaction/{id}", TransactionEncode(model)).Result; return(CheckStatusCode(response, model.BankAccountId, model)); }
public ActionResult CreateTransaction(int bankAccountId, int houseHoldId) { var cookie = Request.Cookies["MyFirstCookie"]; if (cookie == null) { return(RedirectToAction("Login", "Account")); } var url = $"http://localhost:64310/api/Category/view/{houseHoldId}"; var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {cookie.Value}"); var response = httpClient .GetAsync(url) .Result; CreateEditTransactionViewModel model = null; if (response.StatusCode == System.Net.HttpStatusCode.OK) { var data = response.Content.ReadAsStringAsync().Result; var categories = JsonConvert.DeserializeObject <List <CategoryViewModel> >(data); model = new CreateEditTransactionViewModel { CategoryList = categories.Select(p => new SelectListItem { Text = p.Name, Value = p.Id.ToString() }).ToList() }; } else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { var data = response.Content.ReadAsStringAsync().Result; var errors = JsonConvert.DeserializeObject <APIErroData>(data); foreach (var key in errors.ModelState) { foreach (var error in key.Value) { ModelState.AddModelError(key.Key, error); } } } else { //Create a log for the error message ModelState.AddModelError("", "Sorry. An unexpected error has occured. Please try again later"); } return(View(model)); }
public ActionResult EditTransaction(int id, int houseHoldId) { var cookie = Request.Cookies["MyFirstCookie"]; if (cookie == null) { return(RedirectToAction("Login", "Account")); } var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {cookie.Value}"); var response = httpClient .GetAsync($"http://localhost:64310/api/Transaction/ViewById/{id}") .Result; var a = response.Content.ReadAsStringAsync().Result; if (response.StatusCode == System.Net.HttpStatusCode.OK) { var data = response.Content.ReadAsStringAsync().Result; var result = JsonConvert.DeserializeObject <TransactionViewModel>(data); var categoriesResponse = httpClient .GetAsync($"http://localhost:64310/api/Category/view/{houseHoldId}") .Result; var categoriesData = categoriesResponse.Content.ReadAsStringAsync().Result; var categories = JsonConvert.DeserializeObject <List <CategoryViewModel> >(categoriesData); //if (!result.IsOwner) //{ // return RedirectToAction(nameof(HomeController.Index), "Home"); //} var editViewModel = new CreateEditTransactionViewModel(); editViewModel.Title = result.Title; editViewModel.Description = result.Description; editViewModel.TransactionDate = result.TransactionDate; editViewModel.Amount = result.Amount; editViewModel.CategoryList = categories.Select(p => new SelectListItem { Text = p.Name, Value = p.Id.ToString() }).ToList(); return(View(editViewModel)); } else { //Create a log for the error message return(RedirectToAction(nameof(HomeController.Index), "Home")); } }
public ActionResult Create(int id, CreateEditTransactionViewModel model) { var httpClient = HttpContext.Items["httpClient"] as HttpClient; var response = httpClient.PostAsync(url + $"PostTransaction/{id}", TransactionEncode(model)).Result; if (!response.IsSuccessStatusCode) { model.Categories = PopulateCategoriesList(id); } return(CheckStatusCode(response, id, model)); }
private FormUrlEncodedContent TransactionEncode(CreateEditTransactionViewModel model) { var httpClient = HttpContext.Items["httpClient"] as HttpClient; var parameters = new List <KeyValuePair <string, string> >(); parameters.Add(new KeyValuePair <string, string>("title", model.Title)); parameters.Add(new KeyValuePair <string, string>("description", model.Description)); parameters.Add(new KeyValuePair <string, string>("ammount", model.Ammount.ToString())); parameters.Add(new KeyValuePair <string, string>("transactionDate", model.TransactionDate.ToString())); parameters.Add(new KeyValuePair <string, string>("categoryId", model.CategoryId.ToString())); var encodedParameters = new FormUrlEncodedContent(parameters); return(encodedParameters); }
public ActionResult Create(int id) { var model = new CreateEditTransactionViewModel { Categories = PopulateCategoriesList(id), TransactionDate = DateTime.Now }; if (model.Categories.Any()) { model.Categories.ToList()[0].Selected = true; } else { var messageError = "There are no category in the household"; TempData["Error"] = messageError; return(RedirectToAction("GetHouseholds", "Household")); } return(View(model)); }
public ActionResult EditTransaction(int id, int houseHoldId, int bankAccountId, CreateEditTransactionViewModel model) { var cookie = Request.Cookies["MyFirstCookie"]; if (cookie == null) { return(RedirectToAction("Login", "Account")); } if (!ModelState.IsValid) { return(View(model)); } var parameters = new List <KeyValuePair <string, string> >(); parameters.Add( new KeyValuePair <string, string>("Title", model.Title)); parameters.Add( new KeyValuePair <string, string>("Description", model.Description)); parameters.Add( new KeyValuePair <string, string>("BankAccountId", Convert.ToString(model.BankAccountId))); parameters.Add( new KeyValuePair <string, string>("CategoryId", Convert.ToString(model.CategoryId))); parameters.Add( new KeyValuePair <string, string>("Amount", Convert.ToString(model.Amount))); parameters.Add( new KeyValuePair <string, string>("TransactionDate", Convert.ToString(model.TransactionDate))); var encodedParameters = new FormUrlEncodedContent(parameters); var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {cookie.Value}"); var response = httpClient .PostAsync($"http://localhost:64310/api/Transaction/edit/{id}", encodedParameters) .Result; if (response.StatusCode == System.Net.HttpStatusCode.OK) { return(RedirectToAction("DisplayTransaction", new { bankAccountId, houseHoldId })); } else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { var data = response.Content.ReadAsStringAsync().Result; var errors = JsonConvert.DeserializeObject <APIErroData>(data); foreach (var key in errors.ModelState) { foreach (var error in key.Value) { ModelState.AddModelError(key.Key, error); } } return(View(model)); } else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { return(RedirectToAction(nameof(HomeController.Index), "Home")); } else { //Create a log for the error message ModelState.AddModelError("", "Sorry. An unexpected error has occured. Please try again later"); return(View(model)); } }
public ActionResult CheckStatusCode(HttpResponseMessage response, int bankAccountId, CreateEditTransactionViewModel model) { if (response.IsSuccessStatusCode) { return(RedirectToAction("GetTransactions", new { id = bankAccountId })); } else { return(CheckError(response, model)); } }
public virtual async Task OnGetAsync() { var dto = await _service.GetAsync(Id); ViewModel = ObjectMapper.Map <TransactionDto, CreateEditTransactionViewModel>(dto); }