convert to use service
This commit is contained in:
parent
596065b7c8
commit
2caa82b8f4
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using BinaryDad.Notes.Services;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace BinaryDad.Notes.Controllers;
|
namespace BinaryDad.Notes.Controllers;
|
||||||
|
|
||||||
@ -6,11 +7,11 @@ namespace BinaryDad.Notes.Controllers;
|
|||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class ApiController : ControllerBase
|
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]
|
[HttpPost]
|
||||||
@ -24,7 +25,7 @@ public class ApiController : ControllerBase
|
|||||||
content = await reader.ReadToEndAsync();
|
content = await reader.ReadToEndAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
await System.IO.File.WriteAllTextAsync(filePath, content);
|
noteService.Save(content);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,20 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using BinaryDad.Notes.Services;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace BinaryDad.Notes.Controllers;
|
namespace BinaryDad.Notes.Controllers;
|
||||||
|
|
||||||
public class HomeController : Controller
|
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()
|
public IActionResult Index()
|
||||||
{
|
{
|
||||||
var content = System.IO.File.ReadAllText(filePath);
|
var content = noteService.Get();
|
||||||
|
|
||||||
return View((object)content);
|
return View((object)content);
|
||||||
}
|
}
|
||||||
|
12
Program.cs
12
Program.cs
@ -1,7 +1,10 @@
|
|||||||
|
using BinaryDad.Notes.Services;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddControllersWithViews();
|
builder.Services.AddControllersWithViews();
|
||||||
|
builder.Services.AddSingleton<INoteService, FileNoteService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
@ -13,15 +16,6 @@ if (!app.Environment.IsDevelopment())
|
|||||||
app.UseHsts();
|
app.UseHsts();
|
||||||
}
|
}
|
||||||
|
|
||||||
// create file
|
|
||||||
var configuration = app.Services.GetService<IConfiguration>();
|
|
||||||
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.UseHttpsRedirection();
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
|
||||||
|
28
Services/FileNoteService.cs
Normal file
28
Services/FileNoteService.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
Services/INoteService.cs
Normal file
8
Services/INoteService.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace BinaryDad.Notes.Services
|
||||||
|
{
|
||||||
|
public interface INoteService
|
||||||
|
{
|
||||||
|
string Get();
|
||||||
|
void Save(string content);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user