Esempio n. 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Look up cost center
            CostCenterInfo costCenterInfo = await _apiClient.GetCostCenterInfoAsync(Expense.SubmitterEmail);

            Expense.CostCenter    = costCenterInfo.CostCenter;
            Expense.ApproverEmail = costCenterInfo.ApproverEmail;

            // Write to DB, but don't wait right now
            _context.Expense.Add(Expense);
            Task t = _context.SaveChangesAsync();

            // Serialize the expense and write it to the Azure Storage Queue
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_queueInfo.ConnectionString);
            CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
            CloudQueue          queue          = queueClient.GetQueueReference(_queueInfo.QueueName);
            await queue.CreateIfNotExistsAsync();

            CloudQueueMessage queueMessage = new CloudQueueMessage(JsonConvert.SerializeObject(Expense));
            await queue.AddMessageAsync(queueMessage);

            // Ensure the DB write is complete
            t.Wait();

            return(RedirectToPage("./Index"));
        }
Esempio n. 2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Expense).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ExpenseExists(Expense.ExpenseId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Look up cost center
            CostCenter costCenter = await GetCostCenterAsync(costCenterAPIUrl, Expense.SubmitterEmail);

            if (costCenter != null)
            {
                Expense.CostCenter    = costCenter.CostCenterName;
                Expense.ApproverEmail = costCenter.ApproverEmail;
            }
            else
            {
                Expense.CostCenter    = "Unkown";
                Expense.ApproverEmail = "Unknown";
            }

            // Write to DB, but don't wait right now
            _context.Expense.Add(Expense);
            Task t = _context.SaveChangesAsync();

            // Serialize the expense and write it to the Azure Storage Queue
            //CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_queueInfo.ConnectionString);
            //CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            //CloudQueue queue = queueClient.GetQueueReference(_queueInfo.QueueName);
            //await queue.CreateIfNotExistsAsync();
            //CloudQueueMessage queueMessage = new CloudQueueMessage(JsonConvert.SerializeObject(Expense));
            //await queue.AddMessageAsync(queueMessage);

            //ToDo: Until we figure out how to OpenFaaS locally/container, ignoring OpenFaaS stuff locally. Otherwise this will fail
            //https://secanablog.wordpress.com/2018/06/10/run-your-own-faas-with-openfaas-and-net-core/
            if (!_env.IsDevelopment())
            {
                // Serialize the expense and write it to the NATS Queue
                var cf = new ConnectionFactory();
                using (var c = cf.CreateConnection(_queueInfo.ConnectionString))
                {
                    Console.WriteLine($"Sending POST {JsonConvert.SerializeObject(Expense)}");
                    c.Publish(_queueInfo.QueueName, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Expense)));
                }
            }
            // Ensure the DB write is complete
            t.Wait();

            // This is to track customer App Metrics counter to track number of expenses submitted
            _metrics.Measure.Counter.Increment(MetricsRegistry.CreatedExpenseCounter);

            return(RedirectToPage("./Index"));
        }
Esempio n. 4
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Expense = await _context.Expense.FindAsync(id);

            if (Expense != null)
            {
                _context.Expense.Remove(Expense);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 5
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Look up cost center
            CostCenter costCenter = await GetCostCenterAsync(costCenterAPIUrl, Expense.SubmitterEmail);

            if (costCenter != null)
            {
                Expense.CostCenter    = costCenter.CostCenterName;
                Expense.ApproverEmail = costCenter.ApproverEmail;
            }
            else
            {
                Expense.CostCenter    = "Unkown";
                Expense.ApproverEmail = "Unknown";
            }

            // Write to DB, but don't wait right now
            _context.Expense.Add(Expense);
            Task t = _context.SaveChangesAsync();

            // Serialize the expense and write it to the Azure Storage Queue
            QueueClient queueClient = new QueueClient(_queueInfo.ConnectionString, _queueInfo.QueueName, new QueueClientOptions
            {
                MessageEncoding = QueueMessageEncoding.Base64
            });

            await queueClient.CreateIfNotExistsAsync();

            await queueClient.SendMessageAsync(JsonConvert.SerializeObject(Expense));

            // Ensure the DB write is complete
            t.Wait();

            return(RedirectToPage("./Index"));
        }
Esempio n. 6
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            CostCenter costCenter = await GetCostCenterAsync(costCenterAPIUrl, Expense.SubmitterEmail);

            if (costCenter != null)
            {
                Expense.CostCenter    = costCenter.CostCenterName;
                Expense.ApproverEmail = costCenter.ApproverEmail;
            }
            else
            {
                Expense.CostCenter    = "Unkown";
                Expense.ApproverEmail = "Unknown";
            }

            // Write to DB, but don't wait right now
            _context.Expense.Add(Expense);
            Task t = _context.SaveChangesAsync();

            // Serialize the expense and write it to the Azure Storage Queue
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_queueInfo.ConnectionString);
            CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
            CloudQueue          queue          = queueClient.GetQueueReference(_queueInfo.QueueName);
            await queue.CreateIfNotExistsAsync();

            CloudQueueMessage queueMessage = new CloudQueueMessage(JsonConvert.SerializeObject(Expense));
            await queue.AddMessageAsync(queueMessage);

            // Ensure the DB write is complete
            t.Wait();

            _metrics.Measure.Counter.Increment(MetricsRegistry.CreatedExpenseCounter);

            return(RedirectToPage("./Index"));
        }