From 2caa82b8f4d182a12a332f83652a7939325ce40d Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Thu, 5 Jan 2023 10:32:21 -0500 Subject: [PATCH] convert to use service --- Controllers/ApiController.cs | 11 ++++++----- Controllers/HomeController.cs | 11 ++++++----- Program.cs | 12 +++--------- Services/FileNoteService.cs | 28 ++++++++++++++++++++++++++++ Services/INoteService.cs | 8 ++++++++ 5 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 Services/FileNoteService.cs create mode 100644 Services/INoteService.cs diff --git a/Controllers/ApiController.cs b/Controllers/ApiController.cs index cfbab0d..4cbeeab 100644 --- a/Controllers/ApiController.cs +++ b/Controllers/ApiController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using BinaryDad.Notes.Services; +using Microsoft.AspNetCore.Mvc; namespace BinaryDad.Notes.Controllers; @@ -6,11 +7,11 @@ namespace BinaryDad.Notes.Controllers; [Route("[controller]")] public class ApiController : ControllerBase { - private readonly string filePath; + private readonly INoteService noteService; - public ApiController(IConfiguration configuration) + public ApiController(INoteService noteService) { - filePath = configuration["ContentFilePath"]; + this.noteService = noteService; } [HttpPost] @@ -24,7 +25,7 @@ public class ApiController : ControllerBase content = await reader.ReadToEndAsync(); } - await System.IO.File.WriteAllTextAsync(filePath, content); + noteService.Save(content); return true; } diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 68c3514..c280efe 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -1,19 +1,20 @@ -using Microsoft.AspNetCore.Mvc; +using BinaryDad.Notes.Services; +using Microsoft.AspNetCore.Mvc; namespace BinaryDad.Notes.Controllers; public class HomeController : Controller { - private readonly string filePath; + private readonly INoteService noteService; - public HomeController(IConfiguration configuration) + public HomeController(INoteService noteService) { - filePath = configuration["ContentFilePath"]; + this.noteService = noteService; } public IActionResult Index() { - var content = System.IO.File.ReadAllText(filePath); + var content = noteService.Get(); return View((object)content); } diff --git a/Program.cs b/Program.cs index ae652a4..0f233fa 100644 --- a/Program.cs +++ b/Program.cs @@ -1,7 +1,10 @@ +using BinaryDad.Notes.Services; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); +builder.Services.AddSingleton(); var app = builder.Build(); @@ -13,15 +16,6 @@ if (!app.Environment.IsDevelopment()) app.UseHsts(); } -// create file -var configuration = app.Services.GetService(); -var filePath = configuration["ContentFilePath"]; - -if (!File.Exists(filePath)) -{ - File.WriteAllText(filePath, "Hi! Feel free to start typing. Everything will be saved soon after you are done typing."); -} - app.UseHttpsRedirection(); app.UseStaticFiles(); diff --git a/Services/FileNoteService.cs b/Services/FileNoteService.cs new file mode 100644 index 0000000..7cea087 --- /dev/null +++ b/Services/FileNoteService.cs @@ -0,0 +1,28 @@ +namespace BinaryDad.Notes.Services +{ + public class FileNoteService : INoteService + { + private readonly string? filePath; + + public FileNoteService(IConfiguration configuration) + { + filePath = configuration["ContentFilePath"]; + + // ensure initialized + if (!File.Exists(filePath)) + { + File.WriteAllText(filePath, "Hi! Feel free to start typing. Everything will be saved soon after you are done typing."); + } + } + + public string Get() + { + return File.ReadAllText(filePath); + } + + public void Save(string content) + { + File.WriteAllText(filePath, content); + } + } +} diff --git a/Services/INoteService.cs b/Services/INoteService.cs new file mode 100644 index 0000000..4aac958 --- /dev/null +++ b/Services/INoteService.cs @@ -0,0 +1,8 @@ +namespace BinaryDad.Notes.Services +{ + public interface INoteService + { + string Get(); + void Save(string content); + } +}