Skip to content

Commit

Permalink
fix: made code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
wobedydob committed Jan 24, 2025
1 parent d487876 commit f746b17
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 45 deletions.
1 change: 0 additions & 1 deletion HAN.Services/Exporters/FileExporter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations;
using HAN.Services.DTOs;
using HAN.Services.Interfaces;
using ValidationException = HAN.Services.Exceptions.ValidationException;

namespace HAN.Services.Exporters;
Expand Down
47 changes: 23 additions & 24 deletions HAN.Services/Exporters/PdfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,34 @@
using HAN.Services.DTOs;
using QuestPDF.Helpers;

namespace HAN.Services.Exporters
namespace HAN.Services.Exporters;

public class PdfExporter : FileExporter
{
public class PdfExporter : FileExporter
public override FileDto Export(FileDto fileDto)
{
public override FileDto Export(FileDto fileDto)
{
ValidateFile(fileDto);
ValidateFile(fileDto);

var fileName = fileDto.Name.EndsWith(".pdf") ? fileDto.Name : $"{fileDto.Name}.pdf";
var filePath = GetExportFilePath(fileName);
var fileName = fileDto.Name.EndsWith(".pdf") ? fileDto.Name : $"{fileDto.Name}.pdf";
var filePath = GetExportFilePath(fileName);

Console.WriteLine($"Generating PDF file at: {filePath}");
Document.Create(container =>
Console.WriteLine($"Generating PDF file at: {filePath}");
Document.Create(container =>
{
container.Page(page =>
{
container.Page(page =>
page.Size(PageSizes.A4);
page.Margin(2, Unit.Centimetre);
page.Content().Column(column =>
{
page.Size(PageSizes.A4);
page.Margin(2, Unit.Centimetre);
page.Content().Column(column =>
{
column.Item().Text($"Title: {fileDto.Name}")
.FontSize(20)
.Bold();
column.Item().Text(fileDto.Content).FontSize(12);
});
column.Item().Text($"Title: {fileDto.Name}")
.FontSize(20)
.Bold();
column.Item().Text(fileDto.Content).FontSize(12);
});
}).GeneratePdf(filePath);
return fileDto;
}
});
}).GeneratePdf(filePath);

return fileDto;
}
}
}
38 changes: 18 additions & 20 deletions HAN.Services/Exporters/WordExporter.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using HAN.Services.DTOs;

namespace HAN.Services.Exporters
namespace HAN.Services.Exporters;

public class WordExporter : FileExporter
{
public class WordExporter : FileExporter
public override FileDto Export(FileDto fileDto)
{
public override FileDto Export(FileDto fileDto)
{
ValidateFile(fileDto);

var fileName = fileDto.Name.EndsWith(".docx") ? fileDto.Name : $"{fileDto.Name}.docx";
var filePath = GetExportFilePath(fileName);
ValidateFile(fileDto);

Console.WriteLine($"Generating Word file at: {filePath}");
using var wordDocument = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document);
var mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
var body = mainPart.Document.AppendChild(new Body());
var paragraph = body.AppendChild(new Paragraph());
var run = paragraph.AppendChild(new Run());
run.AppendChild(new Text(fileDto.Content));
var fileName = fileDto.Name.EndsWith(".docx") ? fileDto.Name : $"{fileDto.Name}.docx";
var filePath = GetExportFilePath(fileName);

return fileDto;
}
Console.WriteLine($"Generating Word file at: {filePath}");
using var wordDocument = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document);
var mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
var body = mainPart.Document.AppendChild(new Body());
var paragraph = body.AppendChild(new Paragraph());
var run = paragraph.AppendChild(new Run());
run.AppendChild(new Text(fileDto.Content));

return fileDto;
}

}

0 comments on commit f746b17

Please sign in to comment.