public async Task <ActionResult> Create(ViewModels.Transaction transaction) { if (ModelState.IsValid) { var currentUTCTime = DateTimeOffset.UtcNow; var computer = await db.Computers.Where(x => x.SKU == transaction.Computer).SingleOrDefaultAsync(); var stock = computer.Stock - transaction.Amount; var sale = new Models.Transaction() { IdTransaction = Guid.NewGuid().ToString(), Computer = computer, Notes = transaction.Notes, Created = currentUTCTime, Type = TransactionType.Debet, Amount = transaction.Amount }; db.Transactions.Add(sale); var result = await db.SaveChangesAsync(); if (result > 0) { computer.Stock = stock; db.Entry(computer).State = EntityState.Modified; var resultV2 = await db.SaveChangesAsync(); if (resultV2 > 0) { return(RedirectToAction("Index")); } } } return(View("Error")); }
public async Task <ActionResult> Create(ViewModels.Transaction buy) { if (ModelState.IsValid) { var currentUTCTime = DateTimeOffset.UtcNow; var transaction = await db.Transactions.Include("Computer").Where(x => x.IdTransaction == buy.IdTransaction).SingleOrDefaultAsync(); var stock = transaction.Amount - buy.Amount; var total = transaction.Computer.Price * buy.Amount; var purchase = new Models.Transaction() { IdTransaction = Guid.NewGuid().ToString(), Computer = transaction.Computer, Notes = buy.Notes, Created = currentUTCTime, Type = TransactionType.Credit, Amount = buy.Amount, TotalTransaction = total }; db.Transactions.Add(purchase); var result = await db.SaveChangesAsync(); if (result > 0) { var totalBuyers = transaction.TotalTransaction + total; transaction.TotalTransaction = totalBuyers; transaction.Amount = stock; db.Entry(transaction).State = EntityState.Modified; var resultV2 = await db.SaveChangesAsync(); if (resultV2 > 0) { return(RedirectToAction("Index")); } } } return(View("Error")); }
private static string SerializeTransaction(ViewModels.Transaction result) { var jsonResolver = new PropertyRenameAndIgnoreSerializerContractResolver(); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "PriceBtc"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "PriceEur"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "PriceUsd"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "ConfirmedOnFormatted"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "Raw"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "ReceiptRaw"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "OriginalTransactionHash"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "Symbol"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "Url"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "Found"); jsonResolver.IgnoreProperty(typeof(ViewModels.Transaction), "Confirmations"); var serializerSettings = new JsonSerializerSettings(); serializerSettings.ContractResolver = jsonResolver; var json = JsonConvert.SerializeObject(result, Formatting.Indented, serializerSettings); return(json); }
public async Task <IActionResult> Get(string transactionHash) { using (var transactionService = new TransactionService(_nodeUrls)) using (var blockService = new BlockService(_nodeUrls)) { var transactionBuilder = Builders <Caladan.Models.Transaction> .Filter; var transactionFilter = transactionBuilder.Where(x => x.TransactionHash == transactionHash.ToLower()); var transactions = await _transactionRepository.FindAsync(transactionFilter, null); var transaction = transactions.FirstOrDefault(); if (transaction == null) { transaction = await transactionService.GetTransactionAsync(transactionHash); if (transaction == null) { return(Ok(new ViewModels.Transaction() { Found = false })); } } await GetTransactionReceipt(transactionHash, transactionService, _transactionRepository, _transactionReceiptRepository, transaction); ulong confirmations = 0; if (transaction.BlockNumber != 0) { var orderByBlock = Builders <Caladan.Models.Block> .Sort.Descending("block_number"); var lastBlock = await _blockRepository.GetAsync(null, orderByBlock); if (lastBlock == null || lastBlock.BlockNumber < transaction.BlockNumber) { confirmations = await blockService.GetBlockNumberFromNodeAsync() - transaction.BlockNumber + 1; } else { confirmations = lastBlock.BlockNumber - transaction.BlockNumber + 1; } } double fee = 0; if (transaction.Receipt != null) { fee = NodeServices.Helpers.ConversionHelper.ConvertWei(transaction.Receipt.GasUsed * transaction.GasPrice, 18); } var result = new ViewModels.Transaction() { BlockNumber = transaction.BlockNumber, From = transaction.From, Gas = transaction.Gas, GasPrice = (ulong)NodeServices.Helpers.ConversionHelper.ConvertWei(transaction.GasPrice, 9), GasUsed = transaction.Receipt?.GasUsed ?? 0, Fee = fee, To = transaction.To, TransactionHash = transaction.TransactionHash, Value = transaction.Value.FromHexWei(transaction.Decimals), Symbol = transaction.Symbol, BlockHash = transaction.BlockHash, ConfirmedOnFormatted = transaction.Created.ToString(), Input = transaction.Input, Nonce = transaction.Nonce, Timestamp = transaction.Timestamp, TransactionIndex = transaction.TransactionIndex, PriceBtc = transaction.PriceBtc, PriceEur = transaction.PriceEur, PriceUsd = transaction.PriceUsd, Confirmations = confirmations, Receipt = transaction.Receipt == null ? null : new ViewModels.TransactionReceipt() { BlockHash = transaction.Receipt.BlockHash, BlockNumber = transaction.Receipt.BlockNumber, ContractAddress = transaction.Receipt.ContractAddress, CumulativeGasUsed = transaction.Receipt.CumulativeGasUsed, GasUsed = transaction.Receipt.GasUsed, Logs = transaction.Receipt.Logs, TransactionHash = transaction.Receipt.TransactionHash, TransactionIndex = transaction.Receipt.TransactionIndex, From = transaction.Receipt.From, LogsBloom = transaction.Receipt.LogsBloom, Root = transaction.Receipt.Root, To = transaction.Receipt.To }, Found = true }; result.Raw = SerializeTransaction(result); if (transaction.Receipt != null) { result.ReceiptRaw = SerializeTransactionReceipt(transaction.Receipt); } return(Ok(result)); } }