public async Task <List <HandlerServiceResult> > BuildStatistics(Category category, DateTime?from = null, DateTime?to = null) { var result = new List <HandlerServiceResult> { new HandlerServiceResult { Message = "Here's your statistics:", StatusCode = StatusCodeEnum.Ok } }; var operations = await _operationDocumentService.GetByCategoryId(category.Id); if (from != null && to != null) { operations = operations.Where(o => o.Date >= from && o.Date <= to).ToList(); } operations.Sort((x, y) => x.Date.CompareTo(y.Date)); var sign = category.Type == CategoryTypeEnum.Income ? "+" : "-"; result.AddRange(operations.Select(operation => new HandlerServiceResult { Message = $"{operation.Date}: {sign}{BuildAmountWithCurrency((float) operation.CreditAmountInCents / 100, category.Currency)}", StatusCode = StatusCodeEnum.Ok })); return(result); }
private async Task <List <HandlerServiceResult> > ConfigureCategoryDateRange(string answer, User user) { answer = answer.Trim(); if (string.IsNullOrEmpty(answer)) { return(new List <HandlerServiceResult> { _resultService.BuildEmptyAnswerErrorResult() }); } var category = await _categoryDocumentService.GetByIdAsync(user.Context.CategoryId); if (answer.Equals("All time")) { user.Context = null; await _userDocumentService.UpdateAsync(user); return(await _statsService.BuildStatistics(category)); } try { var dateStrings = answer.Split('-'); var from = dateStrings[0].Split('.'); var to = dateStrings[1].Split('.'); var fromDate = new DateTime(int.Parse(from[2]), int.Parse(from[1]), int.Parse(from[0])); var toDate = new DateTime(int.Parse(to[2]), int.Parse(to[1]), int.Parse(to[0])); var operations = await _operationDocumentService.GetByCategoryId(category.Id); operations = operations.Where(o => o.Date >= fromDate && o.Date <= toDate).ToList(); if (operations.Count == 0) { return(new List <HandlerServiceResult> { _resultService.BuildStatsNoOperationsOnDateRangeErrorResult() }); } user.Context = null; await _userDocumentService.UpdateAsync(user); return(await _statsService.BuildStatistics(category, fromDate, toDate)); } catch (Exception) { return(new List <HandlerServiceResult> { _resultService.BuildOperationInvalidDateErrorResult() }); } }