public IHttpActionResult Create(int?Id, BankAccountBindingModel bankAccountBindingModel) { if (ModelState is null || !ModelState.IsValid) { return(BadRequest(ModelState)); } var household = DbContext.Households.FirstOrDefault(p => p.Id == Id); if (household is null) { return(NotFound()); } var bankAccount = new BankAccount { Name = bankAccountBindingModel.Name, Description = bankAccountBindingModel.Description, DateCreated = DateTime.Now, DateUpdated = null, Balance = 0.0m, Household = household }; household.BankAccounts.Add(bankAccount); DbContext.SaveChanges(); var bankAccountView = BankAccountHelpers.MapBankAccountToView(bankAccount); return(Created(Url.Link( "GetBankAccountById", new { bankAccount.Id }), bankAccountView )); }
public IHttpActionResult Create(BankAccountBindingModel formData) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var householdOwnerId = HouseholdHelper.GetHhOwnerIdByHhId(formData.HouseholdId); if (householdOwnerId == null) { return(NotFound()); } var currentUserId = User.Identity.GetUserId(); var IsOwner = householdOwnerId == currentUserId; if (!IsOwner) { return(Unauthorized()); } var bankAccount = Mapper.Map <BankAccount>(formData); DbContext.BankAccounts.Add(bankAccount); DbContext.SaveChanges(); var viewModel = Mapper.Map <BankAccountViewModel>(bankAccount); var url = Url.Link("DefaultApi", new { Action = "GetAllByHhId" }); return(Created(url, viewModel)); }
public IHttpActionResult Put(int id, BankAccountBindingModel formData) { var bankAccount = Context .BankAccounts .FirstOrDefault(p => p.Id == id); if (bankAccount == null) { return(NotFound()); } var owner = bankAccount .HouseHold .OwnerId; var userId = User .Identity .GetUserId(); if (userId != owner) { ModelState.AddModelError("Not the Owner", "Only the owner can edit a Bank Account"); return(BadRequest(ModelState)); } Mapper.Map(formData, bankAccount); bankAccount.DateUpdated = DateTime.Now; Context.SaveChanges(); var model = Mapper.Map <BankAccountViewModel>(bankAccount); return(Ok(model)); }
public IHttpActionResult PutBankAccount(int id, BankAccountBindingModel bindingModel) { if (bindingModel == null) { return(BadRequest("Provide required parameters")); } var bankAccount = hBHelper.GetBankAccountById(id); Mapper.Map(bindingModel, bankAccount); bankAccount.Updated = DateTime.Now; DbContext.SaveChanges(); var bankAccountModel = Mapper.Map <BankAccountViewModel>(bankAccount); return(Ok(bankAccountModel)); }
public IHttpActionResult CreateBankAccount(int id, BankAccountBindingModel model) { if (model == null) { ModelState.AddModelError(nameof(model), "Invalid form data"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var houseHold = DbContext.Households.FirstOrDefault(p => p.Id == id); if (houseHold == null) { return(NotFound()); } var userId = User.Identity.GetUserId(); if (userId != houseHold.OwnerOfHouseId) { return(BadRequest("Sorry, You are not allowed to create a Bank Account of this household.")); } var bankAccount = new BankAccount() { Name = model.Name, Description = model.Description, }; houseHold.BankAccounts.Add(bankAccount); DbContext.SaveChanges(); var url = Url.Link("AccountById", new { Id = bankAccount.Id }); var viewModel = new BankAccountViewModel(bankAccount) { DateUpdated = null, IsOwner = houseHold.OwnerOfHouse.Id == userId }; return(Created(url, viewModel)); }
public IHttpActionResult PostBankAccount(int id, BankAccountBindingModel bindingModel) { if (bindingModel == null) { return(BadRequest("Provide required parameters")); } var bankAccount = Mapper.Map <BankAccount>(bindingModel); bankAccount.HouseholdId = id; DbContext.BankAccounts.Add(bankAccount); DbContext.SaveChanges(); var url = Url.Link("DefaultApi", new { Controller = "BankAccount", Id = bankAccount.Id }); var bankAccountModel = Mapper.Map <BankAccountViewModel>(bankAccount); return(Created(url, bankAccountModel)); }
public ActionResult Edit(int id, int hhId, BankAccountBindingModel formData) { if (!ModelState.IsValid) { return(View()); } var name = formData.Name; var description = formData.Description; var parameters = new List <KeyValuePair <string, string> >(); parameters.Add(new KeyValuePair <string, string>("name", name)); parameters.Add(new KeyValuePair <string, string>("description", description)); var response = RequestHelper.SendGetRequestAuth(parameters, "BankAccount" , "Edit", id, MyToken, CusHttpMethod.Put); return(GeneralResDealer(response, MyResDealer.regSuccess, MyResDealer.empty, MyResDealer.single, hhId, MyResDealer.notFound, MyResDealer.empty, MyResDealer.noAuth, MyResDealer.badResquest)); }
[UserAuthorization(IdType = typeof(BankAccountCreator))] // only the owner of the household can edit the bank accounts public IHttpActionResult Edit(int?Id, BankAccountBindingModel bindingModel) { if (ModelState is null || !ModelState.IsValid) { return(BadRequest(ModelState)); } var bankAccount = DbContext.BankAccounts.FirstOrDefault(p => p.Id == Id); if (bankAccount is null) { return(NotFound()); } bankAccount.Name = bindingModel.Name; bankAccount.Description = bindingModel.Description; bankAccount.DateUpdated = DateTime.Now; DbContext.SaveChanges(); return(OkView(bankAccount)); }
public IHttpActionResult EditAccount(int id, BankAccountBindingModel model) { if (model == null) { ModelState.AddModelError(nameof(model), "Invalid form data"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userId = User.Identity.GetUserId(); var bankAccount = DbContext.BankAccounts.FirstOrDefault(p => p.Id == id); if (bankAccount == null) { return(NotFound()); } if (userId != bankAccount.Household.OwnerOfHouseId) { return(BadRequest("Sorry, You are not allowed to edit this Bank Account.")); } bankAccount.Name = model.Name; bankAccount.Description = model.Description; bankAccount.DateUpdated = DateTime.Now; DbContext.SaveChanges(); var viewModel = new BankAccountViewModel(bankAccount) { IsOwner = bankAccount.Household.OwnerOfHouse.Id == userId }; return(Ok(viewModel)); }
public IHttpActionResult Create(int id, BankAccountBindingModel formData) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var houseHold = Context .HouseHolds .FirstOrDefault(p => p.Id == id); if (houseHold == null) { return(NotFound()); } var userId = User .Identity .GetUserId(); if (userId != houseHold.OwnerId) { ModelState.AddModelError("Not the Owner", "Only the owner can create a Bank Account"); return(BadRequest(ModelState)); } var bankAccount = Mapper.Map <BankAccount>(formData); bankAccount.HouseHoldId = id; bankAccount.Balance = 0; houseHold.BankAccounts.Add(bankAccount); Context.SaveChanges(); var model = Mapper.Map <BankAccountViewModel>(bankAccount); return(Ok(model)); }
public IHttpActionResult CreateBankAccount(int id, BankAccountBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentHousehold = DbContext.Households.FirstOrDefault( house => house.Id == id); var userId = User.Identity.GetUserId(); if (currentHousehold == null) { return(NotFound()); } if (currentHousehold.CreatedById == userId) { BankAccount newBankAccount; newBankAccount = new BankAccount(); newBankAccount.Name = model.Name; newBankAccount.Description = model.Description; newBankAccount.DateCreated = DateTime.Today; currentHousehold.BankAccounts.Add(newBankAccount); DbContext.SaveChanges(); return(Ok()); } else { return(NotFound()); } }