示例#1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(Constants.AUTHORIZATION_LEVEL, "post", Route = "updatebalance")] HttpRequest req, ILogger log, CancellationToken ct)
        {
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    transaction = JsonConvert.DeserializeObject <Transaction>(requestBody);


            var userIdentifier = GetCallingUserIdentifier(req);

            if (!await IsParent(req))
            {
                throw new SecurityException("Invalid attempt to access a record by an invalid user");
            }

            try
            {
                log.LogTrace($"UpdateBalance function processed a request from userIdentifier:{userIdentifier}.");
                var account = await _accountService.Get(transaction.AccountId);

                if (transaction.CategoryId == (int)Constants.TransactionCategory.Deposit)
                {
                    account.Balance += transaction.Amount;
                }
                else
                {
                    account.Balance -= transaction.Amount;
                }

                await _accountService.Update(account, false);

                var transactionLog = new TransactionLog()
                {
                    AccountId      = transaction.AccountId,
                    Amount         = transaction.Amount,
                    Date           = DateTime.Now,
                    CategoryId     = transaction.CategoryId,
                    Description    = transaction.Description,
                    UserIdentifier = userIdentifier
                };
                await _transactionLogService.Create(transactionLog, false);

                await _transactionLogService.SaveChanges();
            }
            catch (Exception exception)
            {
                return(new BadRequestObjectResult($"Error trying to execute UpdateBalance.  {exception.Message}"));
            }
            return(new OkObjectResult(true));
        }
示例#2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(Constants.AUTHORIZATION_LEVEL, "post", Route = "accepttaskweek")] HttpRequest req, ILogger log, CancellationToken ct)
        {
            TaskWeek data = null;

            try
            {
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                data = JsonConvert.DeserializeObject <TaskWeek>(requestBody);
                log.LogTrace($"AcceptTaskWeek function processed a request for taskWeekId:{data.Id}.");

                var userIdentifier = await GetTargetUserIdentifier(req);

                if (!await IsParent(req))
                {
                    throw new SecurityException("Invalid attempt to accept a taskweek by an invalid user");
                }
                await _taskWeekService.Update(data, false);

                var account = await _accountService.GetByUser(data.UserIdentifier);

                account.Balance += data.Value;
                await _accountService.Update(account, false);

                var transaction = new TransactionLog()
                {
                    AccountId      = account.Id.Value,
                    UserIdentifier = req.GetUserIdentifier(),
                    Amount         = data.Value,
                    CategoryId     = (int)Constants.TransactionCategory.Deposit,
                    Date           = DateTime.Now,
                    Description    = "Weekly allowance deposit."
                };
                await _transactionLogService.Create(transaction, false);

                await _transactionLogService.SaveChanges();
            }
            catch (Exception exception)
            {
                return(new BadRequestObjectResult($"Error trying to execute PutTaskWeek.  {exception.Message}"));
            }
            return(new OkObjectResult(data.Id.Value));
        }