private ActionResult FillDropDownListOnCreation(int id) { var model = new TranscationBindingModel(); var responseBa = SendReqGetRes(true, id, "BankAccount", "GetAllByHhId"); if (responseBa.IsSuccessStatusCode) { model.BankAccounts = MakeNewDropDownList(ReadOutputDropDownList(responseBa)); } else if (responseBa.StatusCode == HttpStatusCode.NotFound) { AddTempDataMsg(true); return(View("Error")); } var responseCat = SendReqGetRes(true, id, "Category", "GetAllByHhId"); if (responseCat.IsSuccessStatusCode) { model.Categories = MakeNewDropDownList(ReadOutputDropDownList(responseCat)); return(View(model)); } else if (responseCat.StatusCode == HttpStatusCode.NotFound) { AddTempDataMsg(false); return(View("Error")); } return(View("Error")); }
public IHttpActionResult Create(TranscationBindingModel formData) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (formData.DateTransacted.Year < 1900 || formData.DateTransacted.Year > 2100) { ModelState.AddModelError("Date", "Date is not vaild."); return(BadRequest(ModelState)); } var joinedUsers = UserHelper.GetJoinedUsersByBaId(formData.BankAccountId); if (!joinedUsers.Any()) { return(NotFound()); } var validCatId = TransactionHelper .IsCategoryBelongToSameHhByBaId(formData.BankAccountId, formData.CategoryId); if (!validCatId) { ModelState.AddModelError("CategoryId", "You don't share the same Household with this Cat."); return(BadRequest(ModelState)); } var isJoined = joinedUsers.Any(p => p.Id == CurrentUserID); if (!isJoined) { return(Unauthorized()); } var transcation = Mapper.Map <Transaction>(formData); transcation.CreatorId = CurrentUserID; DbContext.Transactions.Add(transcation); DirectCalculateBalance(true, transcation.BankAccountId, formData.Amount); DbContext.SaveChanges(); var viewModel = Mapper.Map <TranscationViewModel>(transcation); return(Ok(viewModel)); }
public ActionResult Create(int id, TranscationBindingModel formData) { if (!ModelState.IsValid) { return(FillDropDownListOnCreation(id)); } var title = formData.Title; var description = formData.Description; int categoryId = formData.CategoryId; int bankAccountId = formData.BankAccountId; var dateTransacted = formData.DateTransacted; decimal amount = formData.Amount; var parameters = new List <KeyValuePair <string, string> >(); parameters.Add(new KeyValuePair <string, string>("title", title)); parameters.Add(new KeyValuePair <string, string>("description", description)); parameters.Add(new KeyValuePair <string, string>("amount", amount.ToString())); parameters.Add(new KeyValuePair <string, string>("categoryId", categoryId.ToString())); parameters.Add(new KeyValuePair <string, string>("bankAccountId", bankAccountId.ToString())); parameters.Add(new KeyValuePair <string, string>("dateTransacted", dateTransacted.ToString())); var response = RequestHelper.SendGetRequestAuth(parameters, "Transaction" , "Create", null, MyToken, CusHttpMethod.Post); if (response.IsSuccessStatusCode) { return(ToShowMe(bankAccountId)); } if (response.StatusCode == HttpStatusCode.NotFound) { return(NotFoundOrUnauthorized(true)); } else if (response.StatusCode == HttpStatusCode.Unauthorized) { return(NotFoundOrUnauthorized(false)); } else if (response.StatusCode == HttpStatusCode.InternalServerError) { CusAddTempDataMsg("Date Transacted maybe not valid"); return(View("Error")); } else if (response.StatusCode != HttpStatusCode.BadRequest) { return(View("Error")); } if (response.StatusCode == HttpStatusCode.BadRequest) { BadRequestSmall(response); var responseBa = SendReqGetResDropDownList(true, id); if (responseBa.IsSuccessStatusCode) { formData.BankAccounts = MakeNewDropDownList(ReadOutputDropDownList(responseBa)); } else if (responseBa.StatusCode == HttpStatusCode.NotFound) { AddTempDataMsg(true); return(View("Error")); } var responseCat = SendReqGetResDropDownList(false, id); if (responseCat.IsSuccessStatusCode) { formData.Categories = MakeNewDropDownList(ReadOutputDropDownList(responseCat)); return(View(formData)); } else if (responseCat.StatusCode == HttpStatusCode.NotFound) { AddTempDataMsg(false); return(View("Error")); } return(View("Error")); } return(View("Error")); }