public async Task <ActionResult <Voucher> > CreateVoucher(string name)
        {
            var voucher = new Voucher(name);

            await _context.Vouchers.AddAsync(voucher);

            var voucherId = voucher.Id;

            string voucherCode = ConvertIdToVoucherCode(voucherId);

            voucher.VoucherCode = voucherCode;

            await _context.SaveChangesAsync();

            return(voucher);
        }
        public async Task <ActionResult <PayInvoiceResponse> > PayVoucher(string token, string payreq)
        {
            Console.WriteLine("CONTROLLERLOG: PayVoucher " + token + " " + payreq);
            var voucherItem = await _context.VoucherItems.FindAsync(token);

            if (voucherItem == null)
            {
                return(NotFound());
            }



            using (var transaction = _context.Database.BeginTransaction())
            {
                var satCost = await _lightning.SatCost(payreq);

                var calculatedFee = await _lightning.getSatFee(payreq);

                if (calculatedFee == -1)
                {
                    return(new PayInvoiceResponse
                    {
                        PaymentError = "could not find a route",
                    });
                }
                var estimatedCost = (ulong)(satCost + calculatedFee);

                Console.WriteLine("CONTROLLERLOG: Estimated Cost:" + estimatedCost);
                if (estimatedCost > voucherItem.StartSat - voucherItem.UsedSat)
                {
                    return(new PayInvoiceResponse
                    {
                        PaymentError = "not enough satoshi on voucher to pay for transaction and network fee"
                    });
                }
                Console.WriteLine("CONTROLLERLOG: PayVoucher BEFORE PAYMENT" + token + " " + payreq);
                var res = await _lightning.SendPayment(payreq);

                Console.WriteLine("CONTROLLERLOG: PayVoucher AFTER PAYMENT" + token + " " + payreq);
                if (res.PaymentError != "")
                {
                    _context.Entry(voucherItem).State = EntityState.Unchanged;
                    return(new PayInvoiceResponse()
                    {
                        PaymentError = res.PaymentError
                    });
                }
                Console.WriteLine("COST: " + (ulong)(res.PaymentRoute.TotalAmtMsat / 1000));
                var realCost = (ulong)(res.PaymentRoute.TotalAmtMsat / 1000);
                voucherItem.UsedSat += realCost;
                _context.Entry(voucherItem).State = EntityState.Modified;

                if (voucherItem.UsedSat >= voucherItem.StartSat)
                {
                    _context.Entry(voucherItem).State = EntityState.Deleted;
                }
                await _context.SaveChangesAsync();

                transaction.Commit();
                return(new PayInvoiceResponse()
                {
                    PaymentError = res.PaymentError, PaymentPreimage = res.PaymentPreimage.ToBase64(), PaymentRoute = res.PaymentRoute
                });
            }
        }