public async Task <IActionResult> Run( [HttpTrigger(Constants.AUTHORIZATION_LEVEL, "put", Route = "taskweekset/{id?}")] HttpRequest req, ILogger log, CancellationToken ct, int?id) { log.LogTrace($"PutTaskWeek function processed a request for id:{id}."); TaskWeek data = null; try { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); data = JsonConvert.DeserializeObject <TaskWeek>(requestBody); var userIdentifier = await GetTargetUserIdentifier(req); if (data.UserIdentifier != userIdentifier && !await IsInRole(userIdentifier, Constants.Role.Parent)) { throw new SecurityException("Invalid attempt to access a record by an invalid user"); } await _taskWeekService.Update(data); } catch (Exception exception) { return(new BadRequestObjectResult($"Error trying to execute PutTaskWeek. {exception.Message}")); } return(new OkObjectResult(data.Id.Value)); }
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)); }