public void OrgAccToChangeBalance(FinOpViewModel finOpModel, decimal originalAmount) { var orgAccTo = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccToId); orgAccTo.CurrentBalance += finOpModel.Amount - originalAmount; _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo); }
public FinOpViewModel CreateIncome(FinOpViewModel finOpModel) { FinOpInputDataValidation(finOpModel); try { var orgAcc = _unitOfWork.OrganizationAccountRepository.GetOrgAccountByName(finOpModel.OrgId, finOpModel.CardTo); var finOp = new FinOp { Amount = finOpModel.Sum, AccToId = orgAcc.Id, Description = finOpModel.Description, TargetId = finOpModel.TargetId, FinOpDate = DateTime.Now, }; _unitOfWork.FinOpRepository.Create(finOp); orgAcc.CurrentBalance += finOpModel.Sum; _unitOfWork.OrganizationAccountRepository.Edit(orgAcc); _unitOfWork.SaveChanges(); return(finOpModel); } catch (Exception ex) { throw new BusinessLogicException("Щось пішло не так....О_о", ex); } }
public FinOpViewModel CreateTransfer(FinOpViewModel finOpModel) { FinOpInputDataValidation(finOpModel); try { var orgAccFrom = _unitOfWork.OrganizationAccountRepository.GetOrgAccountByName(finOpModel.OrgId, finOpModel.CardFrom); var orgAccTo = _unitOfWork.OrganizationAccountRepository.GetOrgAccountByName(finOpModel.OrgId, finOpModel.CardTo); if (finOpModel.Sum > orgAccFrom.CurrentBalance) { throw new ArgumentException("Витрати не можуть перебільшувати баланс рахунку"); } var finOp = new FinOp { Amount = finOpModel.Sum, AccToId = orgAccTo.Id, AccFromId = orgAccFrom.Id, Description = finOpModel.Description, FinOpDate = DateTime.Now, }; _unitOfWork.FinOpRepository.Create(finOp); orgAccFrom.CurrentBalance -= finOpModel.Sum; _unitOfWork.OrganizationAccountRepository.Edit(orgAccFrom); orgAccTo.CurrentBalance += finOpModel.Sum; _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo); _unitOfWork.SaveChanges(); return(finOpModel); } catch (Exception ex) { throw new BusinessLogicException("Щось пішло не так....О_о", ex); } }
public FinOpViewModel CreateTransfer(FinOpViewModel finOpModel) { FinOpInputDataValidation(finOpModel); try { var orgAccFrom = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccFromId); var orgAccTo = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccToId); if (finOpModel.Amount > orgAccFrom.CurrentBalance) { throw new ArgumentException(ErrorMessages.SpendingIsExceeded); } var finOp = new FinOp { Amount = finOpModel.Amount, AccToId = orgAccTo.Id, AccFromId = orgAccFrom.Id, Description = finOpModel.Description, FinOpDate = finOpModel.Date, FinOpType = finOpModel.FinOpType, UserId = finOpModel.UserId }; _unitOfWork.FinOpRepository.Create(finOp); orgAccFrom.CurrentBalance -= finOpModel.Amount; _unitOfWork.OrganizationAccountRepository.Edit(orgAccFrom); orgAccTo.CurrentBalance += finOpModel.Amount; _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo); _unitOfWork.SaveChanges(); return(finOpModel); } catch (Exception ex) { throw new BusinessLogicException(ErrorMessages.OperationTransferError, ex); } }
/// <Summary> /// Gets the fin ops by id. /// </Summary> /// <param name="id">The fin ops identifier.</param> /// <returns></returns> public FinOpViewModel GetFinOpsById(int id) { try { if (id <= 0) { throw new BusinessLogicException(ErrorMessages.InvalidIdentificator); } var finOpFromDataBase = _unitOfWork.FinOpRepository.GetById(id); var finOp = new FinOpViewModel { Id = finOpFromDataBase.Id, AccFromId = finOpFromDataBase.AccFromId.GetValueOrDefault(0), AccToId = finOpFromDataBase.AccToId.GetValueOrDefault(0), Date = finOpFromDataBase.FinOpDate, Description = finOpFromDataBase.Description, Amount = finOpFromDataBase.Amount, TargetId = finOpFromDataBase.TargetId, Target = finOpFromDataBase.Target?.TargetName, FinOpType = finOpFromDataBase.FinOpType, DonationId = finOpFromDataBase.DonationId, IsEditable = true }; return(finOp); } catch (Exception ex) { throw new BusinessLogicException(ErrorMessages.EmptyFinOpList, ex); } }
/// public FinOpViewModel CreateIncome(FinOpViewModel finOpModel) { FinOpInputDataValidation(finOpModel); try { var orgAccTo = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccToId); var finOp = new FinOp { Amount = finOpModel.Amount, AccToId = orgAccTo.Id, Description = finOpModel.Description, TargetId = finOpModel.TargetId, FinOpDate = finOpModel.Date, FinOpType = finOpModel.FinOpType, UserId = finOpModel.UserId }; _unitOfWork.FinOpRepository.Create(finOp); orgAccTo.CurrentBalance += finOpModel.Amount; _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo); _unitOfWork.SaveChanges(); return(finOpModel); } catch (Exception ex) { throw new BusinessLogicException(ErrorMessages.OperationIncomeError, ex); } }
private void FinOpInputDataValidation(FinOpViewModel finOpModel) { if (finOpModel.Sum <= 0 || finOpModel.Sum > 1000000) { throw new ArgumentException(ErrorMessages.MoneyFinOpLimit); } }
//[Authorize(Roles = "admin, moderator")] public IActionResult Spending([FromBody] FinOpViewModel spendingModel) { if (spendingModel == null) { return(BadRequest()); } return(Ok(_service.CreateSpending(spendingModel))); }
public IActionResult BindDonationAndFinOp([FromBody] FinOpViewModel finOp) { if (finOp == null) { return(BadRequest()); } return(Ok(_service.BindDonationAndFinOp(finOp))); }
public IActionResult Edit([FromBody] FinOpViewModel finOp) { if (finOp == null) { return(BadRequest()); } return(Ok(_service.EditFinOperation(finOp))); }
//[Authorize(Roles = "admin, moderator")] public IActionResult Income([FromBody] FinOpViewModel incomeModel) { if (incomeModel == null) { return(BadRequest()); } return(Ok(_service.CreateIncome(incomeModel))); }
//[Authorize(Roles = "admin, moderator")] public IActionResult Transfer([FromBody] FinOpViewModel transferModel) { if (transferModel == null) { return(BadRequest()); } return(Ok(_service.CreateTransfer(transferModel))); }
public void Deposite(FinOpViewModel finOpModel, FinOp finOp) { var orgAccFrom = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccFromId); orgAccFrom.CurrentBalance -= finOpModel.Amount; finOp.FinOpType = finOpModel.FinOpType; finOp.AccToId = finOpModel.AccToId; _unitOfWork.OrganizationAccountRepository.Edit(orgAccFrom); }
public void Windtrhaw(FinOpViewModel finOpModel, FinOp finOp) { var orgAccTo = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccToId); orgAccTo.CurrentBalance += finOpModel.Amount; finOp.FinOpType = finOpModel.FinOpType; finOp.AccToId = finOpModel.AccToId; _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo); }
/// <Summary> /// Edit the finance operation. /// </Summary> /// <param name="finOpModel">The finance operation model.</param> /// <returns></returns> public FinOpViewModel EditFinOperation(FinOpViewModel finOpModel) { try { var finOp = _unitOfWork.FinOpRepository.GetById(finOpModel.Id); if (finOpModel.Amount != finOp.Amount) { switch (finOpModel.FinOpType) { case Constants.FinOpTypeSpending: OrgAccFromChangeBalance(finOpModel, finOp.Amount); break; case Constants.FinOpTypeIncome: OrgAccToChangeBalance(finOpModel, finOp.Amount); break; case Constants.FinOpTypeTransfer: OrgAccFromChangeBalance(finOpModel, finOp.Amount); OrgAccToChangeBalance(finOpModel, finOp.Amount); break; default: throw new BusinessLogicException(ErrorMessages.InvalidFinanceOperation); } } if (finOpModel.FinOpType == Constants.FinOpTypeTransfer) { if (finOp.FinOpType == Constants.FinOpTypeSpending) { Windtrhaw(finOpModel, finOp); } if (finOp.FinOpType == Constants.FinOpTypeIncome) { Deposite(finOpModel, finOp); } } finOp.Description = finOpModel.Description; finOp.TargetId = finOpModel.TargetId; finOp.FinOpDate = finOpModel.Date; finOp.UserId = finOpModel.UserId; finOp.DonationId = finOpModel.DonationId; finOp.Amount = finOpModel.Amount; _unitOfWork.FinOpRepository.Update(finOp); _unitOfWork.SaveChanges(); return(finOpModel); } catch (Exception ex) { throw new BusinessLogicException(ErrorMessages.UpdateDataError, ex); } }
private FinOpViewModel ConvertFinOpFromBankViewModelToFinOpViewModel(FinOpFromBankViewModel finOpModel) { var finOp = new FinOpViewModel { Amount = finOpModel.Amount, AccFromId = finOpModel.CardFromId.GetValueOrDefault(0), AccToId = finOpModel.CardToId.GetValueOrDefault(0), Description = finOpModel.Description, Date = finOpModel.FinOpDate, FinOpType = finOpModel.FinOpType, UserId = finOpModel.UserId, OrgId = finOpModel.OrgId, TargetId = finOpModel.TargetId }; return(finOp); }
public async Task Create_Transfer_Operation_Ok_Response() { //Arrange var dbContext = _testContext.GetClearDbContext(); FinOpViewModel transferModel = new FinOpViewModel() { Amount = 100, AccFromId = 1, AccToId = 2, Description = "TestTransferDescription", TargetId = null, Date = new DateTime(2017, 11, 3), FinOpType = 2, UserId = 1 }; var notModifiedOrgAccountFirst = _testContext.TestFinOpRepository.GetTestOrgAccountById(1); var notModifiedOrgAccountSecond = _testContext.TestFinOpRepository.GetTestOrgAccountById(2); var testOrgAccounts = _testContext.TestFinOpRepository.GetTestOrgAccounts(); _testContext.TestFinOpRepository.AddRangeOfOrgAccountsToDb(testOrgAccounts); //Act var content = new StringContent(JsonConvert.SerializeObject(transferModel), Encoding.UTF8, "application/json"); var response = await _testContext.Client.PostAsync($"/api/FinOp/Transfer", content); var stream = await response.Content.ReadAsStreamAsync(); var reader = new StreamReader(stream, Encoding.UTF8); var resultFinOps = new JsonSerializer().Deserialize <FinOpViewModel>(new JsonTextReader(reader)); //Assert response.EnsureSuccessStatusCode(); var modifiedOrgAccountFirst = dbContext.OrgAccounts.Where(acc => acc.Id == 1).FirstOrDefault(); var modifiedOrgAccountSecond = dbContext.OrgAccounts.Where(acc => acc.Id == 2).FirstOrDefault(); Assert.True(HttpStatusCode.OK == response.StatusCode); Assert.NotNull(resultFinOps); Assert.True(dbContext.FinOps.Count() == 1); Assert.False(dbContext.FinOps.Count() == 8); Assert.True(modifiedOrgAccountFirst.CurrentBalance == 900); Assert.True(modifiedOrgAccountSecond.CurrentBalance == 1100); Assert.NotEqual(notModifiedOrgAccountFirst, modifiedOrgAccountFirst); Assert.NotEqual(notModifiedOrgAccountSecond, modifiedOrgAccountSecond); Assert.True(modifiedOrgAccountFirst.CurrentBalance == notModifiedOrgAccountFirst.CurrentBalance - resultFinOps.Amount); Assert.True(modifiedOrgAccountSecond.CurrentBalance == notModifiedOrgAccountSecond.CurrentBalance + resultFinOps.Amount); }
public FinOpViewModel BindDonationAndFinOp(FinOpViewModel finOp) { try { var donation = _unitOfWork.DonationRepository.Get((int)finOp.DonationId); donation.UserId = finOp.UserId; _unitOfWork.DonationRepository.Update(donation); var finOpEntity = _unitOfWork.FinOpRepository.GetById(finOp.Id); finOpEntity.UserId = finOp.UserId; finOpEntity.DonationId = finOp.DonationId; _unitOfWork.FinOpRepository.Update(finOpEntity); _unitOfWork.SaveChanges(); return(finOp); } catch (Exception e) { throw new BusinessLogicException(ErrorMessages.BindingDonationToFinOp, e); } }
public FinOpViewModel Transfer([FromBody] FinOpViewModel transferModel) { return(this._service.CreateTransfer(transferModel)); }
public FinOpViewModel Spending([FromBody] FinOpViewModel spendingModel) { return(this._service.CreateSpending(spendingModel)); }
public FinOpViewModel Income([FromBody] FinOpViewModel incomeModel) { return(this._service.CreateIncome(incomeModel)); }