// *********************************************************************** // Assembly : Ultimate // Author : frede // Created : 2023 06 11 15:57 // // Last Modified By : frede // Last Modified On : 2023 06 11 15:57 // *********************************************************************** // // Copyright (C) 2023-2023 FCS Frede's Computer Services. // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see [https://www.gnu.org/licenses] // // // *********************************************************************** using System.Text; using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.Net.Http.Headers; using Shared.DataTransferObjects; namespace Ultimate; public class CsvOutputFormatter : TextOutputFormatter { public CsvOutputFormatter() { SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/csv")); SupportedEncodings.Add(Encoding.UTF8); SupportedEncodings.Add(Encoding.Unicode); } protected override bool CanWriteType(Type? type) { if (typeof(CompanyDto).IsAssignableFrom(type) || typeof(IEnumerable).IsAssignableFrom(type)) { return base.CanWriteType(type); } return false; } public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) { var response = context.HttpContext.Response; var buffer = new StringBuilder(); if (context.Object is IEnumerable) { foreach (var company in (IEnumerable) context.Object) { FormatCsv(buffer, company); } } else { FormatCsv(buffer, (CompanyDto) context.Object); } await response.WriteAsync(buffer.ToString()); } private static void FormatCsv(StringBuilder buffer, CompanyDto company) { buffer.AppendLine($"{company.CompanyId},\"{company.Name}\",\"{company.FullAddress}\""); } }