| | 1 | | using Domain.Entities; |
| | 2 | | using Microsoft.EntityFrameworkCore; |
| | 3 | | using Repository.Persistency.Abstractions; |
| | 4 | |
|
| | 5 | | namespace Repository.Persistency.Implementations; |
| | 6 | |
|
| | 7 | | public class GraficosRepositorioImpl : IGraficosRepositorio |
| | 8 | | { |
| 1 | 9 | | private static readonly string[] Meses = new[] |
| 1 | 10 | | { |
| 1 | 11 | | "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", |
| 1 | 12 | | "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro" |
| 1 | 13 | | }; |
| | 14 | |
|
| 3 | 15 | | public RegisterContext Context { get; } |
| | 16 | |
|
| 2 | 17 | | public GraficosRepositorioImpl(RegisterContext context) |
| 2 | 18 | | { |
| 2 | 19 | | Context = context; |
| 2 | 20 | | } |
| | 21 | |
|
| | 22 | | public async Task<Grafico> GetDadosGraficoByAno(Guid idUsuario, DateTime data) |
| 2 | 23 | | { |
| 2 | 24 | | int ano = data.Year; |
| | 25 | |
|
| | 26 | | try |
| 2 | 27 | | { |
| 2 | 28 | | var despesas = await Context.Despesa |
| 2 | 29 | | .Where(d => d.UsuarioId == idUsuario && d.Data.Year == ano) |
| 2 | 30 | | .GroupBy(d => d.Data.Month) |
| 2 | 31 | | .Select(g => new { Mes = g.Key, Total = g.Sum(x => x.Valor) }) |
| 2 | 32 | | .ToListAsync(); |
| | 33 | |
|
| 1 | 34 | | var receitas = await Context.Receita |
| 1 | 35 | | .Where(r => r.UsuarioId == idUsuario && r.Data.Year == ano) |
| 1 | 36 | | .GroupBy(r => r.Data.Month) |
| 1 | 37 | | .Select(g => new { Mes = g.Key, Total = g.Sum(x => x.Valor) }) |
| 1 | 38 | | .ToListAsync(); |
| | 39 | |
|
| | 40 | |
|
| 1 | 41 | | var somatorioDespesas = Enumerable.Range(1, 12) |
| 1 | 42 | | .ToDictionary( |
| 12 | 43 | | i => Meses[i - 1], |
| 80 | 44 | | i => despesas.FirstOrDefault(x => x.Mes == i)?.Total ?? 0m |
| 1 | 45 | | ); |
| | 46 | |
|
| 1 | 47 | | var somatorioReceitas = Enumerable.Range(1, 12) |
| 1 | 48 | | .ToDictionary( |
| 12 | 49 | | i => Meses[i - 1], |
| 80 | 50 | | i => receitas.FirstOrDefault(x => x.Mes == i)?.Total ?? 0m |
| 1 | 51 | | ); |
| | 52 | |
|
| 1 | 53 | | return new Grafico |
| 1 | 54 | | { |
| 1 | 55 | | SomatorioDespesasPorAno = somatorioDespesas, |
| 1 | 56 | | SomatorioReceitasPorAno = somatorioReceitas |
| 1 | 57 | | }; |
| | 58 | | } |
| 1 | 59 | | catch |
| 1 | 60 | | { |
| 1 | 61 | | return new Grafico |
| 1 | 62 | | { |
| 24 | 63 | | SomatorioDespesasPorAno = Meses.ToDictionary(m => m, _ => 0m), |
| 24 | 64 | | SomatorioReceitasPorAno = Meses.ToDictionary(m => m, _ => 0m) |
| 1 | 65 | | }; |
| | 66 | | } |
| 2 | 67 | | } |
| | 68 | | } |