public async Task <IActionResult> Get(string username, int id) { var validationResult = await ValidateUser(username); if (validationResult != null) { return(validationResult); } //Get the Entry var entry = await GetEntry(username, id); //If null, return NotFound if (entry == null) { return(NotFound()); } //Return the Entry return(Ok(EntryDto.ConvertBack(entry))); }
public async Task <IActionResult> Get(string username, DateTime?from, DateTime?to) { var validationResult = await ValidateUser(username); if (validationResult != null) { return(validationResult); } //Get the entries and query by the dates (if provided) and then convert to Dto var entries = await(await GetEntriesQuery(username)) .Where(e => !from.HasValue || from.Value.Date <= e.Date) .Where(e => !to.HasValue || e.Date <= to.Value.Date) .OrderByDescending(e => e.Date) .ThenByDescending(e => e.Id) .ToArrayAsync(); var dtos = entries .Select(e => EntryDto.ConvertBack(e)) .ToArray(); return(Ok(dtos)); }
public async Task <IActionResult> GetReport(string username, DateTime?from, DateTime?to) { var validationResult = await ValidateUser(username); if (validationResult != null) { return(validationResult); } //Get the entries var entries = await(await GetEntriesQuery(username)) .OrderByDescending(e => e.Date) .ThenByDescending(e => e.Id) .ToArrayAsync(); //If there are no entries at all, there is nothing to be done if (entries.Length == 0) { return(Ok(new ReportDto() { Entries = new ReportDetailDto[0] })); } //Get the summary data int rowCount = entries.Length; double currentBalance = entries.Sum(e => e.Amount); var deposits = entries.Where(e => e.Amount > 0); int depositCount = deposits.Count(); double totalDeposit = deposits.Sum(e => e.Amount); double depositAvg = depositCount > 0 ? deposits.Average(e => e.Amount) : 0; var withdrawals = entries.Where(e => e.Amount < 0); int withdrawalsCount = withdrawals.Count(); double totalWithdrawals = withdrawals.Sum(e => e.Amount); double withdrawalAvg = withdrawalsCount > 0 ? withdrawals.Average(e => e.Amount) : 0; //Convert to DTOs var dtos = entries .Select(e => new ReportDetailDto(EntryDto.ConvertBack(e))) .ToArray(); //Add the Balance to all rows double balance = 0; foreach (var dto in dtos.OrderBy(d => d.Date)) { balance += dto.Amount.Value; dto.Balance = balance; } //Filter by the optional date limits dtos = dtos .Where(e => !from.HasValue || from.Value.Date <= e.Date) .Where(e => !to.HasValue || e.Date <= to.Value.Date) .ToArray(); var reportDto = new ReportDto() { CurrentBalance = currentBalance, EntryCount = rowCount, DepositCount = depositCount, TotalDeposits = totalDeposit, DepositAverage = double.IsNaN(depositAvg) ? 0 : depositAvg, WithdrawCount = withdrawalsCount, TotalWithdraws = totalWithdrawals, WithdrawAverage = double.IsNaN(withdrawalAvg) ? 0 : withdrawalAvg, Entries = dtos }; return(Ok(reportDto)); }