diff --git a/Wave/Services/FileSystemService.cs b/Wave/Services/FileSystemService.cs index 14f6bc5..745a03e 100644 --- a/Wave/Services/FileSystemService.cs +++ b/Wave/Services/FileSystemService.cs @@ -7,49 +7,64 @@ public class FileSystemService(ILogger logger) { private ILogger Logger { get; } = logger; - public async Task GetEmailTemplateAsync(string name, string? defaultTemplate = null) { + public Task GetEmailTemplateAsync(string name, string? defaultTemplate = null) { string path = Path.Combine(ConfigurationDirectory, "templates", "email", name + ".mjml"); - - if (!File.Exists(path)) { - try { - Directory.CreateDirectory(Path.GetDirectoryName(path)!); - if (!string.IsNullOrWhiteSpace(defaultTemplate)) { - await File.WriteAllTextAsync(path, defaultTemplate, Encoding.UTF8); - } - } catch (Exception ex) { - Logger.LogError(ex, "File system access failed trying write default E-Mail template '{template}'", name); - return defaultTemplate; - } - } - - try { - return await File.ReadAllTextAsync(path, Encoding.UTF8); - } catch (Exception ex) { - Logger.LogError(ex, "File system access failed trying to retrieve E-Mail template '{template}'", name); - return defaultTemplate; - } + return GetFileContentAsync(path, defaultTemplate); } public string? GetEmailTemplate(string name, string? defaultTemplate = null) { string path = Path.Combine(ConfigurationDirectory, "templates", "email", name + ".mjml"); + return GetFileContent(path, defaultTemplate); + } + + public Task GetPartialTemplateAsync(string name, string? defaultTemplate = null) { + string path = Path.Combine(ConfigurationDirectory, "templates", "partials", name + ".html"); + return GetFileContentAsync(path, defaultTemplate); + } + public string? GetPartialTemplate(string name, string? defaultTemplate = null) { + string path = Path.Combine(ConfigurationDirectory, "templates", "partials", name + ".html"); + return GetFileContent(path, defaultTemplate); + } + + private string? GetFileContent(string path, string? defaultContent = null) { if (!File.Exists(path)) { try { Directory.CreateDirectory(Path.GetDirectoryName(path)!); - if (!string.IsNullOrWhiteSpace(defaultTemplate)) { - File.WriteAllText(path, defaultTemplate, Encoding.UTF8); + if (!string.IsNullOrWhiteSpace(defaultContent)) { + File.WriteAllText(path, defaultContent, Encoding.UTF8); } } catch (Exception ex) { - Logger.LogError(ex, "File system access failed trying write default E-Mail template '{template}'", name); - return defaultTemplate; + Logger.LogError(ex, "File system access failed trying write default content of '{template}'", path); + return defaultContent; } } try { return File.ReadAllText(path, Encoding.UTF8); } catch (Exception ex) { - Logger.LogError(ex, "File system access failed trying to retrieve E-Mail template '{template}'", name); - return defaultTemplate; + Logger.LogError(ex, "File system access failed trying to retrieve content of '{template}'", path); + return defaultContent; + } + } + private async Task GetFileContentAsync(string path, string? defaultContent = null) { + if (!File.Exists(path)) { + try { + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + if (!string.IsNullOrWhiteSpace(defaultContent)) { + await File.WriteAllTextAsync(path, defaultContent, Encoding.UTF8); + } + } catch (Exception ex) { + Logger.LogError(ex, "File system access failed trying write default content of '{template}'", path); + return defaultContent; + } + } + + try { + return await File.ReadAllTextAsync(path, Encoding.UTF8); + } catch (Exception ex) { + Logger.LogError(ex, "File system access failed trying to retrieve content of '{template}'", path); + return defaultContent; } } } \ No newline at end of file