public IHttpActionResult Post([FromBody] TransactionInEntity newTransaction) { try { if (string.IsNullOrEmpty(newTransaction.ToLogin)) { throw new MyException("You must select the payment receiver"); } return(Ok(transactionService.Insert(newTransaction, loginService.GetById(CurrentUserId)))); } catch (MyException e) { return(Results(e)); } catch (System.Exception e) { return(Results(e)); } }
/// <summary> /// /// </summary> public TransactionOutEntity Insert(TransactionInEntity transaction, LoginOutEntity currentLogin) { if (currentLogin == null) { throw new MyException("You must registration"); } if (string.IsNullOrEmpty(transaction.ToLogin)) { throw new MyException("You must select the payment receiver"); } if (transaction.Amount <= 0) { throw new MyException("The amount must be greater than 0"); } ILoginService loginService = AppKernel.Get <ILoginService>(new[] { new ConstructorArgument("repositories", repositories) });; LoginOutEntity fromLogin = loginService.GetByMail(currentLogin.Email); if (!loginService.CheckEnough(fromLogin, transaction.Amount)) { throw new MyException("Not enough funds"); } LoginOutEntity toLogin = loginService.GetByMail(transaction.ToLogin); Transaction newTransaction = new Transaction(DateTime.Now, fromLogin.Id, toLogin.Id, transaction.Amount); repositories.Transaction.Save(newTransaction); repositories.Save(); return(Translate(newTransaction)); }