|
1 | | -using Microsoft.AspNetCore.Mvc; |
| 1 | +using Inshapardaz.Domain.Ports.Command.Library.Book.Chapter; |
| 2 | +using Inshapardaz.Domain.Ports.Query.Library.Book.Chapter; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Paramore.Brighter; |
| 5 | +using Paramore.Darker; |
2 | 6 |
|
3 | 7 | namespace Inshapardaz.Api.Controllers; |
4 | 8 |
|
5 | 9 | public class HomeController : Controller |
6 | 10 | { |
| 11 | + private readonly IAmACommandProcessor _commandProcessor; |
| 12 | + private readonly IQueryProcessor _queryProcessor; |
| 13 | + |
| 14 | + public HomeController(IAmACommandProcessor commandProcessor, |
| 15 | + IQueryProcessor queryProcessor) |
| 16 | + { |
| 17 | + _commandProcessor = commandProcessor; |
| 18 | + _queryProcessor = queryProcessor; |
| 19 | + } |
| 20 | + |
7 | 21 | [HttpGet("health/check")] |
8 | 22 | public IActionResult GetHeathCheck() |
9 | 23 | { |
10 | 24 | return Ok(); |
11 | 25 | } |
| 26 | + |
| 27 | + [HttpPost("/libraries/{libraryId}/books/{bookId}/migrateChapters")] |
| 28 | + public async Task<IActionResult> MigrateBook(int libraryId, int bookId, CancellationToken token) |
| 29 | + { |
| 30 | + var query = new GetChaptersByBookQuery(libraryId, bookId); |
| 31 | + var chapters = await _queryProcessor.ExecuteAsync(query, cancellationToken: token); |
| 32 | + foreach (var chapter in chapters) |
| 33 | + { |
| 34 | + foreach (var content in chapter.Contents) |
| 35 | + { |
| 36 | + var chapterContentQuery = new GetChapterContentQuery(libraryId, bookId, chapter.ChapterNumber, content.Language); |
| 37 | + |
| 38 | + var chapterContents = await _queryProcessor.ExecuteAsync(chapterContentQuery, cancellationToken: token); |
| 39 | + |
| 40 | + if (chapterContents != null && chapterContents.FileId == null) |
| 41 | + { |
| 42 | + var request = new UpdateChapterContentRequest(libraryId, bookId, |
| 43 | + chapter.ChapterNumber, chapterContents.Text, chapterContents.Language); |
| 44 | + await _commandProcessor.SendAsync(request, cancellationToken: token); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return Ok("Migrated chapters successfully."); |
| 50 | + } |
12 | 51 | } |
13 | 52 |
|
0 commit comments