public async Task <ActionResult <Wallet> > DeleteWallet(DeleteWalletCommand command) { var wallet = await _context.Wallets.FindAsync(command.WalletId); if (wallet == null) { return(null); } wallet.Deleted = true; _context.Entry(wallet).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!WalletExists(command.WalletId)) { return(null); } else { throw; } } return(wallet); }
public async Task <IActionResult> Add(int id, decimal sum, string cur) { Wallet wallet = _context.Wallets.FirstOrDefault(p => p.UserId == id && p.Currency == cur.ToUpper()); if (wallet == null) { return(BadRequest("Wallet wasn't found")); } wallet.Sum += sum; _context.Entry(wallet).State = EntityState.Modified; await _context.SaveChangesAsync(); return(RedirectToAction("Get", "Wallets", new { id = id })); }
public async Task <IActionResult> PutWallet(Guid id, Wallet wallet) { if (id != wallet.Id) { return(BadRequest()); } _context.Entry(wallet).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!WalletExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutTransaction(Guid CommonCode, DepositViewModel transaction) { if (CommonCode != transaction.CommonCode) { return(BadRequest()); } var existingTransaction = await _context.Transactions.Where(x => x.CommonCode == CommonCode).ToListAsync(); if (existingTransaction.Count > 0) { var existingAccount = await _context.Accounts.ToListAsync(); var updateableTransaction = existingTransaction.Where(x => x.AccountId == x.AccountId).ToList(); updateableTransaction.ForEach(x => x.Amount = x.Account.AccountPercent * x.Amount / 100); await _context.SaveChangesAsync(); foreach (var item in existingTransaction) { if (existingAccount.Any(x => x.AccountId == item.AccountId)) { _context.Entry(item).State = EntityState.Modified; await _context.SaveChangesAsync(); } } } else { return(NotFound()); } return(NoContent()); }
private async Task UpdateEntityStateAsync(Contact contact, EntityState entityState) { using (var context = new WalletContext()) { var contactDto = _mapper.Map <ContactDto>(contact); context.Contacts.Attach(contactDto); context.Entry(contactDto).State = entityState; await context.SaveChangesAsync(); } }
public async Task RemoveLedgerEntriesAsync(List <LedgerEntry> ledgerEntries) { using (var context = new WalletContext()) { var dtos = _mapper.Map <List <LedgerEntryDto> >(ledgerEntries); context.LedgerEntries.AttachRange(dtos); dtos.ForEach(d => context.Entry(d).State = EntityState.Deleted); await context.SaveChangesAsync(); } }
public async Task RemoveLedgerEntriesOnBlockAsync(ulong blockId) { using (var context = new WalletContext()) { var entries = await context.LedgerEntries.Where(e => e.BlockId == (long)blockId).ToListAsync(); entries.ForEach(e => context.Entry(e).State = EntityState.Deleted); await context.SaveChangesAsync(); } }
public async Task UpdateLedgerEntriesAsync(List <LedgerEntry> updatedLedgerEntries) { using (var context = new WalletContext()) { var dtos = _mapper.Map <List <LedgerEntryDto> >(updatedLedgerEntries); foreach (var dto in dtos) { context.LedgerEntries.Add(dto); context.Entry(dto).State = EntityState.Modified; } await context.SaveChangesAsync(); } }
public async Task <IActionResult> PutTransaction(Guid TransactionCode, WithdrawViewModel transaction) { if (TransactionCode != transaction.TransactionCode) { return(BadRequest()); } var existingData = await _context.Transactions.FirstOrDefaultAsync(x => x.TransactionCode == TransactionCode); if (existingData == null) { return(NotFound()); } var existingAccount = await _context.Accounts.FirstOrDefaultAsync(x => x.AccountCode == transaction.AccountCode); if (existingAccount == null) { return(BadRequest()); } existingData.Amount = transaction.Amount * (-1); existingData.AddedTime = (DateTime)transaction.TransactionDate; existingData.AccountId = existingAccount.AccountId; _context.Entry(existingData).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TransactionExists(TransactionCode)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Transaction> > CreateTransaction(CreateTransactionOrderCommand command) { try { var wallet = await _walletContext.Wallets.Where(x => x.Id == command.WalletId).FirstOrDefaultAsync(); if (wallet == null) { return(null); } if ((wallet.Balance + command.Value) < 0) { return(null); } wallet.Balance += command.Value; var transactionDoc = new Transaction { WalletId = command.WalletId, Type = (command.Value > 0) ? TransactionType.Deposit : TransactionType.Withdrawal, WalletBalance = wallet.Balance, Amount = Math.Abs(command.Value), CreatedAt = DateTime.Now, Timestamp = command.Timestamp }; _txContext.Transactions.Add(transactionDoc); await _txContext.SaveChangesAsync(); _walletContext.Entry(wallet).State = EntityState.Modified; await _walletContext.SaveChangesAsync(); return(transactionDoc); } catch (Exception e) { Console.WriteLine(e.StackTrace); return(null); } }
public async Task <IActionResult> PutAccount(Guid AccountCode, AccountViewModel model) { if (AccountCode != model.AccountCode) { return(BadRequest()); } var account = await _context.Accounts.FirstOrDefaultAsync(x => x.AccountCode == model.AccountCode); if (account == null) { return(BadRequest()); } account.AccountCode = model.AccountCode; account.AccountName = model.AccountName; account.AccountPercent = model.AccountPercent; account.AddedTime = DateTime.Now; account.IpAddress = HttpContext.Connection.RemoteIpAddress.ToString(); _context.Entry(account).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AccountExists(AccountCode)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutUser(Guid UserCode, UserViewModel model) { if (UserCode != model.UserCode) { return(BadRequest()); } var existingData = await _context.Users.FirstOrDefaultAsync(x => x.UserCode == UserCode); existingData.IpAddress = HttpContext.Connection.RemoteIpAddress.ToString(); existingData.AddedTime = DateTime.Now; existingData.UserEmail = model.UserEmail; existingData.UserImage = model.UserImage; existingData.UserMobile = model.UserMobile; existingData.UserName = model.UserName; existingData.UserPassword = model.UserPassword; _context.Entry(existingData).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(UserCode)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public void Delete(TEntity entity) { EntityEntry dbEntityEntry = _context.Entry(entity); dbEntityEntry.State = EntityState.Deleted; }