Skip to content

Commit

Permalink
Implemented db clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingafen committed May 20, 2022
1 parent c00164f commit 93cfd97
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 1 deletion.
35 changes: 35 additions & 0 deletions StarWarsKb.Back/Controllers/ConfigController.cs
Original file line number Diff line number Diff line change
@@ -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!";
}
}
}
6 changes: 6 additions & 0 deletions StarWarsKb.Back/Model/CharactersRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@ public void Save(IList<Character> characters)
{
_context.Characters.AddRange(characters);
}

public void DeleteAll()
{
var all = _context.Characters.ToList();
_context.Characters.RemoveRange(all);
}
}
}
31 changes: 31 additions & 0 deletions StarWarsKb.Back/Model/ClearService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using StarWarsKb.Infrastructure.Model;

namespace StarWarsKb.Back.Model
{
public class ClearService : IClearService
{
private readonly IBaseRepository<Character> _characterRepository;
private readonly IBaseRepository<Planet> _planetRepository;
private readonly IBaseRepository<Starship> _starshipRepository;
private readonly IUnitOfWork _unitOfWork;

public ClearService(IUnitOfWork unitOfWork,
IBaseRepository<Character> characterRepository,
IBaseRepository<Planet> planetRepository,
IBaseRepository<Starship> starshipRepository)
{
_characterRepository = characterRepository;
_unitOfWork = unitOfWork;
_planetRepository = planetRepository;
_starshipRepository = starshipRepository;
}

public void ClearDB()
{
_characterRepository.DeleteAll();
_planetRepository.DeleteAll();
_starshipRepository.DeleteAll();
_unitOfWork.SaveChanges();
}
}
}
1 change: 1 addition & 0 deletions StarWarsKb.Back/Model/IBaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public interface IBaseRepository<T> where T : IStarWarsEntity
T GetById(int id);
void Save(T entity);
void Save(IList<T> entities);
void DeleteAll();
}
}
7 changes: 7 additions & 0 deletions StarWarsKb.Back/Model/IClearService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace StarWarsKb.Back.Model
{
public interface IClearService
{
void ClearDB();
}
}
6 changes: 6 additions & 0 deletions StarWarsKb.Back/Model/PlanetsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@ public void Save(IList<Planet> entities)
{
_dbContext.Planets.AddRange(entities);
}

public void DeleteAll()
{
var all = _dbContext.Planets.ToList();
_dbContext.Planets.RemoveRange(all);
}
}
}
6 changes: 6 additions & 0 deletions StarWarsKb.Back/Model/StarshipsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@ public void Save(IList<Starship> starships)
{
_dbContext.Starships.AddRange(starships);
}

public void DeleteAll()
{
var all = _dbContext.Starships.ToList();
_dbContext.Starships.RemoveRange(all);
}
}
}
5 changes: 5 additions & 0 deletions StarWarsKb.Back/Model/StubCharactersRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ public void Save(IList<Character> characters)
{
throw new System.NotImplementedException();
}

public void DeleteAll()
{
throw new System.NotImplementedException();
}
}
}
2 changes: 1 addition & 1 deletion StarWarsKb.Back/Model/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class UnitOfWork : IUnitOfWork
{
private readonly DbContext _dbContext;

public UnitOfWork(DbContext dbContext)
public UnitOfWork(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
Expand Down
13 changes: 13 additions & 0 deletions StarWarsKb.Back/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,6 +29,18 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<IBaseRepository<Starship>, StarshipsRepository>();
services.AddTransient<IBaseRepository<Planet>, PlanetsRepository>();
services.AddTransient<IReportGenerator, ReportGenerator>();
services.AddTransient<IUpdateService, UpdateService>();
services.AddTransient<IWebReader<CharacterPOCO>, WebCharacterPOCOReader>();
services.AddTransient<IWebReader<StarshipPOCO>, WebStarshipPOCOReader>();
services.AddTransient<IWebReader<PlanetPOCO>, WebPlanetPOCOReader>();
services.AddTransient<ICharacterService, CharacterService>();
services.AddTransient<IStarshipService, StarshipService>();
services.AddTransient<IUnitOfWork, UnitOfWork>();
services.AddTransient<IBaseRepository<Starship>, StarshipsRepository>();
services.AddTransient<IBaseRepository<Planet>, PlanetsRepository>();
services.AddTransient<IBaseRepository<Character>, CharactersRepository>();
services.AddTransient<IPlanetService, PlanetService>();
services.AddTransient<IClearService, ClearService>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
35 changes: 35 additions & 0 deletions StarWarsKb.Front/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
40 changes: 40 additions & 0 deletions StarWarsKb.Front/Models/BackEndCaller.cs
Original file line number Diff line number Diff line change
@@ -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<string> Update()
{
return await CallBackEnd(@"/Config/Update/");
}

private async Task<string> 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<string>(() => "Failed");
}

return str;
}

public async Task<string> Clear()
{
return await CallBackEnd(@"/Config/Clear/");
}
}
}
10 changes: 10 additions & 0 deletions StarWarsKb.Front/Models/IBackEndCaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;

namespace StarWarsKb.Front.Models
{
public interface IBackEndCaller
{
Task<string> Update();
Task<string> Clear();
}
}
1 change: 1 addition & 0 deletions StarWarsKb.Front/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<IReportReader, ReportReader>();
services.AddHttpClient();
services.AddTransient<IHttpReader, HttpReader>();
services.AddTransient<IBackEndCaller, BackEndCaller>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
42 changes: 42 additions & 0 deletions StarWarsKb.Front/Views/Settings/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@{
Layout = "_Layout";
}

<!DOCTYPE html>

<html>
<head>
<title>title</title>
</head>
<body>
<div class="container px-4 py-5" id="hanging-icons">
<h2 class="pb-2 border-bottom">Star Wars Knowledge Base settings</h2>
<div class="row g-4 py-5 row-cols-1 row-cols-lg-3">
<div class="col d-flex align-items-start">
<div class="icon-square bg-light text-dark flex-shrink-0 me-3">
<svg class="bi" width="1em" height="1em"><use xlink:href="#toggles2"/></svg>
</div>
<div>
<h2>Clear database</h2>
<p>Click this button to clear database</p>
<a class="btn btn-primary" asp-area="" asp-controller="Settings" asp-action="Clear">
May the Force be with you
</a>
</div>
</div>
<div class="col d-flex align-items-start">
<div class="icon-square bg-light text-dark flex-shrink-0 me-3">
<svg class="bi" width="1em" height="1em"><use xlink:href="#cpu-fill"/></svg>
</div>
<div>
<h2>Update database</h2>
<p>Click this button to update database</p>
<a class="btn btn-primary" asp-area="" asp-controller="Settings" asp-action="Update">
May the Force be with you
</a>
</div>
</div>
</div>
</div>
</body>
</html>
4 changes: 4 additions & 0 deletions StarWarsKb.Front/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Swkb" asp-action="Report">Ships report</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Settings" asp-action="Index">Configure</a>
</li>

</ul>
</div>
</div>
Expand Down

0 comments on commit 93cfd97

Please sign in to comment.