From 93cfd97f2584bbf332011a5523cb1d1bdbfc1588 Mon Sep 17 00:00:00 2001 From: Ingafen Date: Fri, 20 May 2022 21:19:18 +0500 Subject: [PATCH] Implemented db clearing --- .../Controllers/ConfigController.cs | 35 ++++++++++++++++ StarWarsKb.Back/Model/CharactersRepository.cs | 6 +++ StarWarsKb.Back/Model/ClearService.cs | 31 ++++++++++++++ StarWarsKb.Back/Model/IBaseRepository.cs | 1 + StarWarsKb.Back/Model/IClearService.cs | 7 ++++ StarWarsKb.Back/Model/PlanetsRepository.cs | 6 +++ StarWarsKb.Back/Model/StarshipsRepository.cs | 6 +++ .../Model/StubCharactersRepository.cs | 5 +++ StarWarsKb.Back/Model/UnitOfWork.cs | 2 +- StarWarsKb.Back/Startup.cs | 13 ++++++ .../Controllers/SettingsController.cs | 35 ++++++++++++++++ StarWarsKb.Front/Models/BackEndCaller.cs | 40 ++++++++++++++++++ StarWarsKb.Front/Models/IBackEndCaller.cs | 10 +++++ StarWarsKb.Front/Startup.cs | 1 + StarWarsKb.Front/Views/Settings/Index.cshtml | 42 +++++++++++++++++++ StarWarsKb.Front/Views/Shared/_Layout.cshtml | 4 ++ 16 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 StarWarsKb.Back/Controllers/ConfigController.cs create mode 100644 StarWarsKb.Back/Model/ClearService.cs create mode 100644 StarWarsKb.Back/Model/IClearService.cs create mode 100644 StarWarsKb.Front/Controllers/SettingsController.cs create mode 100644 StarWarsKb.Front/Models/BackEndCaller.cs create mode 100644 StarWarsKb.Front/Models/IBackEndCaller.cs create mode 100644 StarWarsKb.Front/Views/Settings/Index.cshtml diff --git a/StarWarsKb.Back/Controllers/ConfigController.cs b/StarWarsKb.Back/Controllers/ConfigController.cs new file mode 100644 index 0000000..2b2f9b6 --- /dev/null +++ b/StarWarsKb.Back/Controllers/ConfigController.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Mvc; +using StarWarsKb.Back.Model; + +namespace StarWarsKb.Back.Controllers +{ + [ApiController] + [Route("[controller]")] + public class ConfigController : Controller + { + private readonly IClearService _clearService; + private readonly IUpdateService _updateService; + + public ConfigController(IClearService clearService, IUpdateService updateService) + { + _clearService = clearService; + _updateService = updateService; + } + + [HttpGet] + [Route("[action]")] + public string Clear() + { + _clearService.ClearDB(); + return "Success"; + } + + [HttpGet] + [Route("[action]")] + public string Update() + { + _updateService.CreateData(); + return "Success!"; + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/CharactersRepository.cs b/StarWarsKb.Back/Model/CharactersRepository.cs index 027f75d..a1a1d97 100644 --- a/StarWarsKb.Back/Model/CharactersRepository.cs +++ b/StarWarsKb.Back/Model/CharactersRepository.cs @@ -35,5 +35,11 @@ public void Save(IList characters) { _context.Characters.AddRange(characters); } + + public void DeleteAll() + { + var all = _context.Characters.ToList(); + _context.Characters.RemoveRange(all); + } } } \ No newline at end of file diff --git a/StarWarsKb.Back/Model/ClearService.cs b/StarWarsKb.Back/Model/ClearService.cs new file mode 100644 index 0000000..2a8f9a0 --- /dev/null +++ b/StarWarsKb.Back/Model/ClearService.cs @@ -0,0 +1,31 @@ +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public class ClearService : IClearService + { + private readonly IBaseRepository _characterRepository; + private readonly IBaseRepository _planetRepository; + private readonly IBaseRepository _starshipRepository; + private readonly IUnitOfWork _unitOfWork; + + public ClearService(IUnitOfWork unitOfWork, + IBaseRepository characterRepository, + IBaseRepository planetRepository, + IBaseRepository starshipRepository) + { + _characterRepository = characterRepository; + _unitOfWork = unitOfWork; + _planetRepository = planetRepository; + _starshipRepository = starshipRepository; + } + + public void ClearDB() + { + _characterRepository.DeleteAll(); + _planetRepository.DeleteAll(); + _starshipRepository.DeleteAll(); + _unitOfWork.SaveChanges(); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/IBaseRepository.cs b/StarWarsKb.Back/Model/IBaseRepository.cs index 79421f3..09bb643 100644 --- a/StarWarsKb.Back/Model/IBaseRepository.cs +++ b/StarWarsKb.Back/Model/IBaseRepository.cs @@ -9,5 +9,6 @@ public interface IBaseRepository where T : IStarWarsEntity T GetById(int id); void Save(T entity); void Save(IList entities); + void DeleteAll(); } } \ No newline at end of file diff --git a/StarWarsKb.Back/Model/IClearService.cs b/StarWarsKb.Back/Model/IClearService.cs new file mode 100644 index 0000000..c50bb33 --- /dev/null +++ b/StarWarsKb.Back/Model/IClearService.cs @@ -0,0 +1,7 @@ +namespace StarWarsKb.Back.Model +{ + public interface IClearService + { + void ClearDB(); + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/PlanetsRepository.cs b/StarWarsKb.Back/Model/PlanetsRepository.cs index 8176bfc..ec39d9b 100644 --- a/StarWarsKb.Back/Model/PlanetsRepository.cs +++ b/StarWarsKb.Back/Model/PlanetsRepository.cs @@ -30,5 +30,11 @@ public void Save(IList entities) { _dbContext.Planets.AddRange(entities); } + + public void DeleteAll() + { + var all = _dbContext.Planets.ToList(); + _dbContext.Planets.RemoveRange(all); + } } } \ No newline at end of file diff --git a/StarWarsKb.Back/Model/StarshipsRepository.cs b/StarWarsKb.Back/Model/StarshipsRepository.cs index ae36b10..72b2745 100644 --- a/StarWarsKb.Back/Model/StarshipsRepository.cs +++ b/StarWarsKb.Back/Model/StarshipsRepository.cs @@ -32,5 +32,11 @@ public void Save(IList starships) { _dbContext.Starships.AddRange(starships); } + + public void DeleteAll() + { + var all = _dbContext.Starships.ToList(); + _dbContext.Starships.RemoveRange(all); + } } } \ No newline at end of file diff --git a/StarWarsKb.Back/Model/StubCharactersRepository.cs b/StarWarsKb.Back/Model/StubCharactersRepository.cs index ae7849e..7e73f56 100644 --- a/StarWarsKb.Back/Model/StubCharactersRepository.cs +++ b/StarWarsKb.Back/Model/StubCharactersRepository.cs @@ -78,5 +78,10 @@ public void Save(IList characters) { throw new System.NotImplementedException(); } + + public void DeleteAll() + { + throw new System.NotImplementedException(); + } } } \ No newline at end of file diff --git a/StarWarsKb.Back/Model/UnitOfWork.cs b/StarWarsKb.Back/Model/UnitOfWork.cs index 9fe7ac7..db1057e 100644 --- a/StarWarsKb.Back/Model/UnitOfWork.cs +++ b/StarWarsKb.Back/Model/UnitOfWork.cs @@ -6,7 +6,7 @@ public class UnitOfWork : IUnitOfWork { private readonly DbContext _dbContext; - public UnitOfWork(DbContext dbContext) + public UnitOfWork(ApplicationDbContext dbContext) { _dbContext = dbContext; } diff --git a/StarWarsKb.Back/Startup.cs b/StarWarsKb.Back/Startup.cs index 54a732b..ba70a38 100644 --- a/StarWarsKb.Back/Startup.cs +++ b/StarWarsKb.Back/Startup.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using StarWarsKb.Back.Model; +using StarWarsKb.Back.Model.POCO; using StarWarsKb.Infrastructure.Model; using StarWarsKb.Infrastructure.Services; @@ -28,6 +29,18 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient, StarshipsRepository>(); services.AddTransient, PlanetsRepository>(); services.AddTransient(); + services.AddTransient(); + services.AddTransient, WebCharacterPOCOReader>(); + services.AddTransient, WebStarshipPOCOReader>(); + services.AddTransient, WebPlanetPOCOReader>(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient, StarshipsRepository>(); + services.AddTransient, PlanetsRepository>(); + services.AddTransient, CharactersRepository>(); + services.AddTransient(); + services.AddTransient(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/StarWarsKb.Front/Controllers/SettingsController.cs b/StarWarsKb.Front/Controllers/SettingsController.cs new file mode 100644 index 0000000..b115aa5 --- /dev/null +++ b/StarWarsKb.Front/Controllers/SettingsController.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Mvc; +using StarWarsKb.Front.Models; + +namespace StarWarsKb.Front.Controllers +{ + public class SettingsController : Controller + { + private readonly IBackEndCaller _backEndCaller; + + public SettingsController(IBackEndCaller backEndCaller) + { + _backEndCaller = backEndCaller; + } + public IActionResult Index() + { + return View(); + } + + [HttpGet] + public IActionResult Clear() + { + var t = _backEndCaller.Clear(); + //t.Wait(60000); + return RedirectToAction("Index", "Home"); + } + + [HttpGet] + public IActionResult Update() + { + var t = _backEndCaller.Update(); + //t.Wait(60000); + return RedirectToAction("Index", "Home"); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Front/Models/BackEndCaller.cs b/StarWarsKb.Front/Models/BackEndCaller.cs new file mode 100644 index 0000000..c892256 --- /dev/null +++ b/StarWarsKb.Front/Models/BackEndCaller.cs @@ -0,0 +1,40 @@ +using System.Threading.Tasks; +using StarWarsKb.Infrastructure.Services; + +namespace StarWarsKb.Front.Models +{ + public class BackEndCaller : IBackEndCaller + { + private readonly IHttpReader _httpReader; + private readonly IParamService _paramService; + + public BackEndCaller(IHttpReader httpReader, IParamService paramService) + { + _httpReader = httpReader; + _paramService = paramService; + } + + public async Task Update() + { + return await CallBackEnd(@"/Config/Update/"); + } + + private async Task CallBackEnd(string address) + { + var url = _paramService.GetParam("SWKB-back-address") + address; + var str = await _httpReader.GetJsonStringByUrl(url); + + if (str == _httpReader.ErrorMessage) + { + return await new Task(() => "Failed"); + } + + return str; + } + + public async Task Clear() + { + return await CallBackEnd(@"/Config/Clear/"); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Front/Models/IBackEndCaller.cs b/StarWarsKb.Front/Models/IBackEndCaller.cs new file mode 100644 index 0000000..c399a17 --- /dev/null +++ b/StarWarsKb.Front/Models/IBackEndCaller.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; + +namespace StarWarsKb.Front.Models +{ + public interface IBackEndCaller + { + Task Update(); + Task Clear(); + } +} \ No newline at end of file diff --git a/StarWarsKb.Front/Startup.cs b/StarWarsKb.Front/Startup.cs index 66c34a4..e0a6cda 100644 --- a/StarWarsKb.Front/Startup.cs +++ b/StarWarsKb.Front/Startup.cs @@ -27,6 +27,7 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient(); services.AddHttpClient(); services.AddTransient(); + services.AddTransient(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/StarWarsKb.Front/Views/Settings/Index.cshtml b/StarWarsKb.Front/Views/Settings/Index.cshtml new file mode 100644 index 0000000..b2f899e --- /dev/null +++ b/StarWarsKb.Front/Views/Settings/Index.cshtml @@ -0,0 +1,42 @@ +@{ + Layout = "_Layout"; +} + + + + + + title + + +
+

Star Wars Knowledge Base settings

+
+
+
+ +
+
+

Clear database

+

Click this button to clear database

+ + May the Force be with you + +
+
+
+
+ +
+
+

Update database

+

Click this button to update database

+ + May the Force be with you + +
+
+
+
+ + \ No newline at end of file diff --git a/StarWarsKb.Front/Views/Shared/_Layout.cshtml b/StarWarsKb.Front/Views/Shared/_Layout.cshtml index 617a931..c3b06ce 100644 --- a/StarWarsKb.Front/Views/Shared/_Layout.cshtml +++ b/StarWarsKb.Front/Views/Shared/_Layout.cshtml @@ -24,6 +24,10 @@ + +