public async Task UpdateAsync(BillPay billPay, int billPayId) { billPay.BillPayID = billPayId; billPay.ModifyDate = DateTime.UtcNow; _set.Update(billPay); await _context.SaveChangesAsync(); }
public async Task <IActionResult> PutAccount(Guid id, [FromBody] DepositRequest depositRequest) { try { //if (!RequestHelper.ValidateId(id, Request, _env)) // return BadRequest("HeaderId and Id are not equal"); var account = await _context.Accounts.FirstOrDefaultAsync(x => x.OwnerId == id); account.Balance += depositRequest.Amount; _context.Entry(account).State = EntityState.Modified; await _context.SaveChangesAsync(); _rabbitMqClient.SendMessage(new HistoryMessage { Event = "DepositedMoney", EventMessage = $"Deposited ${depositRequest.Amount} to own account", User = id, Timestamp = DateTime.UtcNow }); _logger.LogInformation("Successfully deposited {Amount} to {@Account}", depositRequest.Amount, account); } catch (Exception e) { _logger.LogError(e, "Failed to deposit money"); throw; } return(NoContent()); }
/// <summary> /// Creates a bank account /// </summary> /// <param name="bankAccount"></param> /// <returns></returns> public async Task <BankAccountDto> CreateAsync(NewBankAccountDto bankAccount) { var bankAccountEntity = _mapper.Map <BankAccount>(bankAccount); await using var dbTransaction = await _ctx.Database.BeginTransactionAsync(); var bankAccountNumber = await _ctx.BankAccountNumbers.FirstOrDefaultAsync(); if (bankAccountNumber == null) { _ctx.BankAccountNumbers.Add(new BankAccountNumber() { LastNumber = 1 }); } bankAccountNumber.LastNumber++; bankAccountNumber.GeneratedAt = DateTime.UtcNow; bankAccountEntity.Number = $"{bankAccountNumber.LastNumber:0000000000}"; bankAccountEntity.CreatedAt = DateTime.UtcNow; _ctx.BankAccounts.Add(bankAccountEntity); if (await _ctx.SaveChangesAsync() > 0) { await dbTransaction.CommitAsync(); return(_mapper.Map <BankAccountDto>(bankAccountEntity)); } return(null); }
public async Task <IHttpActionResult> PutClient(int id, Client client) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != client.Id) { return(BadRequest()); } db.Entry(client).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ClientExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutCustomer([FromRoute] long id, [FromBody] Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != customer.Id) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Create([Bind("Id,Details,Date")] Transaction transaction) { if (ModelState.IsValid) { _context.Add(transaction); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(transaction)); }
public async Task <IActionResult> Create([Bind("ClientNo,Fname,Lname,Sex,DateOfBirth,PhoneNumber")] Client client) { if (ModelState.IsValid) { _context.Add(client); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(client)); }
public async Task <int> UpdateAsync(int customerId, Customer customer) { customer.CustomerID = customerId; if (_set.Local.FirstOrDefault(x => x.CustomerID == customerId) == null) { _set.Update(customer); } await _context.SaveChangesAsync(); return(customerId); }
public async Task <IActionResult> Create([Bind("StaffNo,Fname,Lname,Sex,DateOfBirth,Salary,StaffBranchNo")] Staff staff) { if (ModelState.IsValid) { _context.Add(staff); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewData["StaffBranchNo"] = new SelectList(_context.Branch, "BranchNo", "City", staff.StaffBranchNo); return(View(staff)); }
public async Task <IActionResult> Create([Bind("DepositNo,Cash,PercentPerYear,DateOfBegin,DateOfEnd,Commentary,DepStaffNo,DepClientNo")] Deposit deposit) { if (ModelState.IsValid) { _context.Add(deposit); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewData["DepClientNo"] = new SelectList(_context.Client, "ClientNo", "Fname", deposit.DepClientNo); ViewData["DepStaffNo"] = new SelectList(_context.Staff, "StaffNo", "Fname", deposit.DepStaffNo); return(View(deposit)); }
public async Task <IActionResult> Create([Bind("Id,Balance,InterestRate,AccountNumber,AccType")] Account account) { if (ModelState.IsValid) { var user = _repo.GetUser(HttpContext.Session.GetString("User")); user.Accounts.Add(account); _context.Add(account); await _context.SaveChangesAsync(); return(RedirectToAction("Overview", "Users")); } return(View(account)); }
public async Task <InstitutionDto> CreateAsync(NewInstitutionDto institution) { var institutionEntity = _mapper.Map <Institution>(institution); institutionEntity.CreatedAt = DateTime.UtcNow; _ctx.Institutions.Add(institutionEntity); if (await _ctx.SaveChangesAsync() == 1) { return(_mapper.Map <InstitutionDto>(institutionEntity)); } return(null); }
private async Task ExecuteBillPays(CancellationToken cancellationToken) { try { using (var scope = _services.CreateScope()) { var context = new BankingContext( scope.ServiceProvider .GetRequiredService <DbContextOptions <BankingContext> >()); var now = DateTime.UtcNow; var billPaysQ = context.BillPay.Where(x => x.ScheduleDate > now - interval && x.ScheduleDate <= now); var billPays = await billPaysQ.ToListAsync(); foreach (var billPay in billPays) { string errMsg; if (billPay.ExecuteBillPay(out errMsg)) { await context.SaveChangesAsync(); _logger.LogInformation($"Executed BillPay {billPay.BillPayID}"); } else { _logger.LogError($"BillPay {billPay.BillPayID}({billPay.ScheduleDateLocal}) execution failed. {errMsg}"); } } } } catch (Exception e) { DateTime now = DateTime.Now; _logger.LogError(0, e, "An exception occured when executing bill pays"); } }
/// <summary> /// Creates a new customer /// </summary> /// <param name="newCustomer"></param> /// <returns></returns> public async Task <CustomerDto> CreateAsync(NewCustomerDto newCustomer) { var customerEntity = _mapper.Map <Customer>(newCustomer); customerEntity.Id = Guid.NewGuid(); customerEntity.CreatedAt = DateTime.UtcNow; _ctx.Customers.Add(customerEntity); if (await _ctx.SaveChangesAsync() == 1) { return(_mapper.Map <CustomerDto>(customerEntity)); } return(null); }
public virtual void Insert(T entity) { try { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } _context.Set <T>().Add(entity); _context.SaveChangesAsync(); } catch (DbEntityValidationException dbEx) { throw new Exception(GetValidations(dbEx)); } }
public async Task <ActionResult <ReservationResult> > PostReservation(ReservationObject reservationObject) { //if (!RequestHelper.ValidateId(reservationObject.AccountId, Request, _env)) // return BadRequest("HeaderId and Id are not equal"); try { var account = await _context.Accounts.FirstOrDefaultAsync(x => x.OwnerId == reservationObject.AccountId); if (account == null) { _logger.LogWarning(@"AccountId {AccountId} does not exist", reservationObject.AccountId); return(new ReservationResult { Valid = false, ErrorMessage = $"AccountId {reservationObject.AccountId} does not exist" }); } if (account.Balance < reservationObject.Amount) { _logger.LogInformation("The account {AccountId} only got a balance of {balance}, but the request is for {Amount}", reservationObject.AccountId, account.Balance, reservationObject.Amount); return(new ReservationResult { Valid = false, ErrorMessage = $"The balance is {account.Balance}, but the request is for {reservationObject.Amount}" }); } account.Balance = account.Balance - reservationObject.Amount; var reservation = new Reservation { Amount = reservationObject.Amount, OwnerAccount = account }; _context.Reservations.Add(reservation); await _context.SaveChangesAsync(); _rabbitMqClient.SendMessage(new HistoryMessage { Event = "CreatedReservation", EventMessage = $"Reserved ${reservationObject.Amount} for buying shares with reservation id {reservation.Id}", User = reservationObject.AccountId, Timestamp = DateTime.UtcNow }); _logger.LogInformation("Successfully reserved {Amount} from {@Account}", reservationObject.Amount, account); TotalMoneyReserved.Inc(reservationObject.Amount); return(new ReservationResult { Valid = true, ReservationId = reservation.Id, ErrorMessage = string.Empty }); } catch (Exception e) { _logger.LogError(e, "Failed to Reserve money"); throw; } }
public async Task <int> UpdateAsync(int id, BillPay item) { item.BillPayID = id; if (_set.Local.FirstOrDefault(x => x.BillPayID == item.BillPayID) == null) { _set.Update(item); } await _context.SaveChangesAsync(); return(item.BillPayID); }
public async Task <string> UpdateAsync(string id, Login login) { login.UserID = id; if (_set.Local.FirstOrDefault(x => x.UserID == id) == null) { _set.Update(login); } await _context.SaveChangesAsync(); return(login.UserID); }
public async Task <ActionResult <ValidationResult> > PutTransfer(TransferObject transferObject) { try { var fromAccount = await _context.Accounts.FirstOrDefaultAsync(x => x.OwnerId == transferObject.FromAccountId); if (fromAccount == null) { _logger.LogWarning($"FromAccountId {transferObject.FromAccountId} does not exist"); return(new ValidationResult { Valid = false, ErrorMessage = $"FromAccountId {transferObject.FromAccountId} does not exist" }); } var toAccount = await _context.Accounts.FirstOrDefaultAsync(x => x.OwnerId == transferObject.ToAccountId); if (toAccount == null) { _logger.LogWarning($"ToAccountId {transferObject.ToAccountId} does not exist"); return(new ValidationResult { Valid = false, ErrorMessage = $"ToAccountId {transferObject.ToAccountId} does not exist" }); } var reservation = await _context.Reservations.FirstOrDefaultAsync(r => r.OwnerAccount == fromAccount && r.Id == transferObject.ReservationId); if (reservation == null) { _logger.LogWarning($"Reservation {transferObject.ReservationId} does not exist"); return(new ValidationResult { Valid = false, ErrorMessage = $"Reservation {transferObject.ReservationId} does not exist" }); } reservation.Amount -= transferObject.Amount; toAccount.Balance += transferObject.Amount; _context.Transfers.Add(new Transfer { Amount = transferObject.Amount, From = fromAccount, To = toAccount }); TotalTransfers.Inc(); TotalMoneyTransferred.Inc(transferObject.Amount); await _context.SaveChangesAsync(); _logger.LogInformation("Successfully transferred {Amount} from {@Sender} to {@Receiver}", transferObject.Amount, fromAccount, toAccount); } catch (Exception e) { _logger.LogError(e, "Failed to transfer"); throw; } return(new ValidationResult { Valid = true, ErrorMessage = "" }); }
public async Task WithdrawAsync(Account account, decimal amount, string comment) { account.Withdraw(amount, comment); await _context.SaveChangesAsync(); }
public async void AddNewBranch(Branch branch) { _bankingContext.Add(branch); await _bankingContext.SaveChangesAsync(); }
Task ILoginsRepository.Add(Login login, CancellationToken cancellationToken) { this.context.Logins.Add(login); return(context.SaveChangesAsync(cancellationToken)); }
public async Task UpdateAsync(Login login, string userId) { login.UserID = userId; _context.Update(login); await _context.SaveChangesAsync(); }
public async Task UpdateAsync(Customer customer, int customerID) { customer.CustomerID = customerID; _context.Update(customer); await _context.SaveChangesAsync(); }