using Microsoft.AspNetCore.Mvc; using ExpenseTracker.Application.Common.Models; using ExpenseTracker.Application.Accounts; using ExpenseTracker.Application.Accounts.Commands.Create; using ExpenseTracker.Application.Accounts.Queries.GetWithPagination; using ExpenseTracker.Application.Accounts.Queries.Get; using ExpenseTracker.Application.Accounts.Commands.Delete; using ExpenseTracker.Application.Accounts.Commands.Update; using ExpenseTracker.Application.Accounts.Queries.Charts; using ExpenseTracker.Application.Accounts.Queries.Charts.GetExpenseBarChart; using ExpenseTracker.Application.Accounts.Queries.Charts.GetIncomeBarChart; using ExpenseTracker.Application.Accounts.Queries.Charts.GetProfitBarChart; using ExpenseTracker.Application.Accounts.Queries.Charts.GetExpenseByCategoryPieChart; using ExpenseTracker.Application.Accounts.Queries.Export.Json.One; using ExpenseTracker.Application.Accounts.Queries.Export.Json.Many; using ExpenseTracker.Application.Accounts.Queries.Export.Csv.One; using ExpenseTracker.Application.Accounts.Commands.Import.Json.One; using ExpenseTracker.Application.Accounts.Commands.Import.Json.Many; namespace ExpenseTracker.Api.Controllers; [Route("accounts")] public class AccountController : BaseController { [HttpPost] public async Task Create([FromBody] CreateAccountCommand command, CancellationToken cancellationToken) { return await Mediator.Send(command, cancellationToken); } [HttpGet] public async Task> GetPage([FromQuery] GetAccountsWithPaginationQuery query, CancellationToken cancellationToken) { return await Mediator.Send(query, cancellationToken); } [HttpGet("{id}")] public async Task Get(string id, CancellationToken cancellationToken) { var query = new GetAccountQuery() { Id = id }; return await Mediator.Send(query, cancellationToken); } [HttpPut] public async Task Update([FromBody] UpdateAccountCommand command, CancellationToken cancellationToken) { return await Mediator.Send(command, cancellationToken); } [HttpDelete] public async Task Delete([FromBody] DeleteAccountCommand command, CancellationToken cancellationToken) { await Mediator.Send(command, cancellationToken); } [HttpGet("{id}/export/json")] public async Task ExportJsonOne(string id, CancellationToken cancellationToken) { var query = new ExportJsonAccountQuery() { Id = id }; var fileBytes = await Mediator.Send(query, cancellationToken); var mimeType = "application/json"; var fileName = $"account_{id}.json"; return new FileContentResult(fileBytes, mimeType) { FileDownloadName = fileName }; } [HttpGet("export/json")] public async Task ExportJsonMany(CancellationToken cancellationToken) { var query = new ExportJsonAccountsQuery(); var fileBytes = await Mediator.Send(query, cancellationToken); var mimeType = "application/json"; var fileName = $"accounts.json"; return new FileContentResult(fileBytes, mimeType) { FileDownloadName = fileName }; } [HttpGet("{id}/export/csv")] public async Task ExportCsvOne(string id, CancellationToken cancellationToken) { var query = new ExportCsvAccountQuery() { Id = id }; var fileBytes = await Mediator.Send(query, cancellationToken); var mimeType = "text/json"; var fileName = $"account_transactions_{id}.csv"; return new FileContentResult(fileBytes, mimeType) { FileDownloadName = fileName }; } [HttpPost("import/json/one")] public async Task ImportOneJson(IFormFile file, CancellationToken cancellationToken) { byte[] fileBytes; using (var binaryReader = new BinaryReader(file.OpenReadStream())) { fileBytes = binaryReader.ReadBytes((int)file.Length); } var query = new ImportJsonAccountQuery() { File = fileBytes }; await Mediator.Send(query, cancellationToken); } [HttpPost("import/json/many")] public async Task ImportManyJson(IFormFile file, CancellationToken cancellationToken) { byte[] fileBytes; using (var binaryReader = new BinaryReader(file.OpenReadStream())) { fileBytes = binaryReader.ReadBytes((int)file.Length); } var query = new ImportJsonAccountsQuery() { File = fileBytes }; await Mediator.Send(query, cancellationToken); } [HttpGet("{id}/expenseBarChart")] public async Task GetExpenseBarChart(string id, [FromQuery] GetExpenseBarChartQuery query, CancellationToken cancellationToken) { query.AccountId = id; return await Mediator.Send(query, cancellationToken); } [HttpGet("{id}/incomeBarChart")] public async Task GetIncomeBarChart(string id, [FromQuery] GetIncomeBarChartQuery query, CancellationToken cancellationToken) { query.AccountId = id; return await Mediator.Send(query, cancellationToken); } [HttpGet("{id}/profitBarChart")] public async Task GetProfitBarChart(string id, [FromQuery] GetProfitBarChartQuery query, CancellationToken cancellationToken) { query.AccountId = id; return await Mediator.Send(query, cancellationToken); } [HttpGet("{id}/expenseByCategoryPieChart")] public async Task> GetExpenseByCategoryPieChart(string id, [FromQuery] GetExpenseByCategoryPieChartQuery query, CancellationToken cancellationToken) { query.AccountId = id; var pieChart = await Mediator.Send(query, cancellationToken); var result = pieChart.ToDictionary(item => item.Key.ToString(), item => item.Value); return result; } }