Initial efcore migration and updates to make sure upserting novels (mostly) works. still need to do chapter handling

This commit is contained in:
2022-07-14 23:12:12 -04:00
parent 5402923e9f
commit 5337e7ccb8
25 changed files with 962 additions and 64 deletions

View File

@@ -3,8 +3,13 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DBConnection;
using DBConnection.Models;
using DBConnection.Repositories;
using DBConnection.Repositories.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebNovelPortalAPI.DTO;
using WebNovelPortalAPI.Scrapers;
namespace WebNovelPortalAPI.Controllers
{
@@ -12,11 +17,42 @@ namespace WebNovelPortalAPI.Controllers
[ApiController]
public class NovelController : ControllerBase
{
private readonly AppDbContext _context;
private readonly INovelRepository _novelRepository;
private readonly IEnumerable<IScraper> _scrapers;
public NovelController(AppDbContext context)
public NovelController(IEnumerable<IScraper> scrapers, INovelRepository novelRepository)
{
_context = context;
_scrapers = scrapers;
_novelRepository = novelRepository;
}
private IScraper? MatchScraper(string novelUrl)
{
return _scrapers.FirstOrDefault(i => i.MatchesUrl(novelUrl));
}
[HttpPost]
[Route("scrapeNovel")]
public async Task<IActionResult> ScrapeNovel(ScrapeNovelRequest request)
{
var scraper = MatchScraper(request.NovelUrl);
if (scraper == null)
{
return BadRequest("Invalid url, no valid scraper configured");
}
Novel novel;
try
{
novel = scraper.ScrapeNovel(request.NovelUrl);
}
catch (Exception e)
{
return StatusCode(500, e);
}
var novelUpload = await _novelRepository.Upsert(novel);
return Ok(novelUpload);
}
}
}