public async Task <ActionResult> PostTransaction(TransactionBindingModel transactionBidingModel) { var transaction = _mapper.Map <Transaction>(transactionBidingModel); _repositoryWrapper.TransactionRepository.Create(transaction); _repositoryWrapper.Save(); return(Ok()); }
public override void OnActionExecuting(HttpActionContext actionContext) { var modelState = actionContext.ModelState; if (!modelState.IsValid) { actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, actionContext.ModelState); } var actionParamentrTransaction = actionContext.ActionArguments.SingleOrDefault(p => p.Key == "id").Value; var transactionId = 0; var userId = hBHelper.GetCurrentUserId(); if (actionParamentrTransaction != null && Int32.TryParse(actionParamentrTransaction.ToString(), out transactionId)) { var transaction = hBHelper.GetTransactionById(transactionId); if (transaction == null) { actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, "Transaction doens't exist"); } else { var bankAccount = DbContext .BankAccounts .FirstOrDefault(p => p.Id == transaction.BankAccountId && (p.Household.OwnerId == userId || transaction.OwnerId == userId)); if (bankAccount == null) { actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, "Bank account doens't exist or your have no right to perfom operation"); } else { var actionModel = actionContext.ActionArguments.SingleOrDefault(p => p.Key == "bindingModel").Value; if (actionModel != null) { TransactionBindingModel model = (TransactionBindingModel)actionContext.ActionArguments["bindingModel"]; if (model != null) { var category = DbContext.Categories.FirstOrDefault(p => p.Id == model.CategoryId && p.CategoryHousehold.Participants.Where(m => m.Id == userId).Any()); if (category == null) { actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, "Category doens't exist or your have no right to perform operation"); } } } } } } }
public IHttpActionResult Create(int?Id, TransactionBindingModel transactionBinding) { // Id being the id of the bankAccount if (ModelState is null || !ModelState.IsValid || Id is null) { return(BadRequest(ModelState)); } var bankAccount = DbContext.BankAccounts.FirstOrDefault(p => p.Id == Id); if (bankAccount is null) { return(BadRequest("No bank account with that Id")); } // Ensure that the bank account and category are in the same houeshold. if (!bankAccount.Household.Categories.Any(p => p.Id == transactionBinding.CategoryId)) { return(BadRequest("No category with that Id")); } var transaction = new Transaction { Title = transactionBinding.Title, Description = transactionBinding.Description, CategoryId = transactionBinding.CategoryId, Date = transactionBinding.Date, Amount = transactionBinding.Amount, CreatorId = User.Identity.GetUserId(), DateCreated = DateTime.Now, DateUpdated = null, BankAccountId = (int)Id, IsVoid = false }; bankAccount.AddTransaction(transaction); DbContext.SaveChanges(); var transactionView = TransactionHelpers.MapToView(transaction); return(Created(Url.Link( "GetTransactionById", new { transaction.Id }), transactionView )); }
public IHttpActionResult Create(int id, TransactionBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userId = User.Identity.GetUserId(); var bankAccount = Context .BankAccounts .FirstOrDefault(p => p.Id == model.BankAccountId && (p.HouseHold.OwnerId == userId || p.HouseHold.Members.Any(t => t.Id == userId))); if (bankAccount == null) { ModelState.AddModelError("", "Bank account doesn't exist or you don't belong to this household"); return(BadRequest(ModelState)); } var category = Context .Categories .FirstOrDefault(p => p.Id == model.CategoryId && p.HouseHoldId == bankAccount.HouseHoldId); if (category == null) { ModelState.AddModelError("", "Category doesn't exist in this household"); return(BadRequest(ModelState)); } var transaction = Mapper.Map <Transaction>(model); transaction.CreatorId = userId; bankAccount.Balance += transaction.Amount; Context.Transactions.Add(transaction); Context.SaveChanges(); var result = Mapper.Map <TransactionViewModel>(transaction); return(Ok(result)); }
public IHttpActionResult Create(TransactionBindingModel formData) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userId = User.Identity.GetUserId(); var bankAccount = DbContext.BankAccounts.FirstOrDefault(p => p.Id == formData.BankAccountId); if (bankAccount == null) { ModelState.AddModelError("BankAccountId", "This bankAccount doesn't exist"); return(BadRequest(ModelState)); } var isMemberOfHouseHold = Validation.IsMemberOfHouseHold(bankAccount.HouseHoldId, userId); if (!isMemberOfHouseHold) { ModelState.AddModelError("UserId", "Sorry, you are not the member of this houseHold"); return(BadRequest(ModelState)); } var isCategoryExist = Validation.IsCategoryExist(bankAccount.HouseHoldId, formData.CategoryId); if (!isCategoryExist) { ModelState.AddModelError("CategoryId", "This category doesn't exist"); return(BadRequest(ModelState)); } var transaction = Mapper.Map <Transaction>(formData); transaction.Created = DateTime.Now; transaction.OwnerId = userId; DbContext.Transactions.Add(transaction); DbContext.SaveChanges(); var model = Mapper.Map <TransactionViewModel>(transaction); CalcurateBalance(transaction.BankAccountId); return(Ok(model)); }
public async Task <ActionResult <Transaction> > PostTransaction(TransactionBindingModel transactionBindingModel) { try { var transaction = _mapper.Map <Transaction>(transactionBindingModel); _context.Transactions.Add(transaction); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetTransaction), new { id = transaction.CakeId }, transaction)); } catch (Exception ex) { } return(BadRequest()); }
public async Task <IHttpActionResult> PostTransaction(TransactionBindingModel transactionData) { this.logger.Debug("Web Api. Проведение транзакции"); if (transactionData == null) { return(BadRequest()); } if (string.IsNullOrEmpty(transactionData.UserName) || transactionData.Amount <= 0) { return(BadRequest()); } // Находим получателя var recipient = this.UserManager.Users.Where(x => x.UserName == transactionData.UserName).FirstOrDefault(); // Выдаем ошибку если получатель не найден if (recipient == null) { return(NotFound()); } // Получаем текущего пользователя var sender = this.UserManager.Users.Where(x => x.UserName == User.Identity.Name).FirstOrDefault(); // Выдаем ошибку если не удалось найти текущего пользователя if (sender == null) { return(InternalServerError()); } // Выдаем ошибку если баланса не достаточно для транзакции if (sender.Balance < transactionData.Amount) { return(BadRequest()); } Transaction transaction = await Repository.Post(recipient, sender, transactionData.Amount); // возвращаем историю проведенной транзакции и ссылку на новый ресурс return(CreatedAtRoute("GetTransaction", new { id = transaction.Id }, transaction)); }
public IHttpActionResult PostTransaction(int id, TransactionBindingModel bindingModel) { if (bindingModel == null) { return(BadRequest("Provide required parameters")); } var transaction = Mapper.Map <Transaction>(bindingModel); transaction.OwnerId = User.Identity.GetUserId(); transaction.BankAccountId = id; DbContext.Transactions.Add(transaction); hBHelper.GetBankAccountById(id).Balance += transaction.Ammount; DbContext.SaveChanges(); var url = Url.Link("DefaultApi", new { Controller = "Transaction", Id = transaction.Id }); var transactionModel = Mapper.Map <TransactionViewModel>(transaction); return(Created(url, transactionModel)); }
public Transaction CreateTransaction(TransactionBindingModel model, string userId) { var user = _context.Users.First(x => x.Id == userId); if (user == null) { throw new Exception("User does not exist"); } var transaction = new Transaction { TransactionDate = model.TransactionDate, Value = model.Value, Type = model.Type, User = user }; _context.Transactions.Add(transaction); _context.SaveChanges(); return(transaction); }
public IHttpActionResult PutTransaction(int id, TransactionBindingModel bindingModel) { if (bindingModel == null) { return(BadRequest("Provide required parameters")); } var transaction = hBHelper.GetTransactionById(id); var prevAmount = transaction.Ammount; Mapper.Map(bindingModel, transaction); transaction.Updated = DateTime.Now; var bankAccount = hBHelper.GetBankAccountById(transaction.BankAccountId); bankAccount.Balance = !transaction.Voided ? bankAccount.Balance - prevAmount + bindingModel.Ammount : bankAccount.Balance; DbContext.SaveChanges(); var transactionModel = Mapper.Map <TransactionViewModel>(transaction); return(Ok(transactionModel)); }
public IHttpActionResult Edit(int?Id, TransactionBindingModel bindingModel) { if (ModelState is null || !ModelState.IsValid) { return(BadRequest(ModelState)); } var transaction = DbContext.Transactions.FirstOrDefault(p => p.Id == Id); if (transaction is null) { return(BadRequest("No Transaction with that Id")); } if (!transaction.BankAccount.Household.Categories.Any(p => p.Id == bindingModel.CategoryId)) { return(BadRequest("No Category with that Id")); } if (!transaction.IsVoid) { transaction.BankAccount.Balance -= transaction.Amount; // Removing the old transaction amount from the old bank account transaction.BankAccount.Balance += bindingModel.Amount; // Adding the new amounts to the new account } transaction.Title = bindingModel.Title; transaction.Description = bindingModel.Description; transaction.CategoryId = bindingModel.CategoryId; transaction.Amount = bindingModel.Amount; transaction.Date = bindingModel.Date; transaction.DateUpdated = DateTime.Now; DbContext.SaveChanges(); return(OkView(transaction)); }
public Transaction CreateTransaction(TransactionBindingModel model, string userId) { return(new Transaction { Id = 1, TransactionDate = new DateTime(2017, 10, 10, 8, 53, 4), Type = TransactionType.FromSpin, Value = 20, User = user }); }
public Transaction CreateTransaction(TransactionBindingModel model, string userId) { return(_transactionRepo.CreateTransaction(model, userId)); }