docs: #298 upload new poems for a category from SQLite
This commit is contained in:
parent
67c21dd056
commit
a2a7d6d52a
@ -118,7 +118,7 @@
|
||||
@if (!string.IsNullOrEmpty(Model.LastMessage))
|
||||
{
|
||||
<p>
|
||||
@Model.LastMessage
|
||||
@Html.Raw(Model.LastMessage)
|
||||
</p>
|
||||
}
|
||||
|
||||
@ -437,6 +437,19 @@
|
||||
</form>
|
||||
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>بارگذاری پایگاه دادهها شامل یک دستهبندی از اشعار:</td>
|
||||
<td>
|
||||
<form method="post" enctype="multipart/form-data" action="/Admin/CatUtils?Handler=UploadDb&url=@Model.Cat.Cat.FullUrl">
|
||||
<input type="file" asp-for="SQLiteDb" />
|
||||
<input type="submit" value="بارگذاری" />
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.Net.Http;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using GanjooRazor.Utils;
|
||||
@ -72,6 +73,10 @@ namespace GanjooRazor.Areas.Admin.Pages
|
||||
[BindProperty]
|
||||
public GanjoorNumbering NumberingModel { get; set; }
|
||||
|
||||
|
||||
[BindProperty]
|
||||
public IFormFile SQLiteDb { get; set; }
|
||||
|
||||
private async Task<bool> GetInformationAsync()
|
||||
{
|
||||
var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/cat?url={Request.Query["url"]}&poems=true");
|
||||
@ -349,5 +354,37 @@ namespace GanjooRazor.Areas.Admin.Pages
|
||||
return new OkObjectResult(false);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostUploadDbAsync(IFormFile SQLiteDb)
|
||||
{
|
||||
await GetInformationAsync();
|
||||
|
||||
|
||||
using (HttpClient secureClient = new HttpClient())
|
||||
{
|
||||
await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response);
|
||||
|
||||
MultipartFormDataContent form = new MultipartFormDataContent();
|
||||
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
await SQLiteDb.CopyToAsync(stream);
|
||||
var fileContent = stream.ToArray();
|
||||
form.Add(new ByteArrayContent(fileContent, 0, fileContent.Length), Cat.Poet.Nickname, SQLiteDb.FileName);
|
||||
|
||||
HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/ganjoor/sqlite/import/cat/{Cat.Cat.Id}", form);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
LastMessage = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
|
||||
return Page();
|
||||
}
|
||||
|
||||
LastMessage = $"پایگاه دادهها بارگذاری شد. <a role=\"button\" href=\"/Admin/CatUtils?url={Cat.Cat.FullUrl}\" class=\"actionlink\">برگشت به صفحهٔ ویرایش دسته</a>";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return Page();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,8 +8,8 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"GoogleAnalyticsCode": "",
|
||||
"APIRoot": "https://api.ganjoor.net",
|
||||
"GlobalAPIRoot": "https://api.ganjoor.net",
|
||||
"APIRoot": "http://localhost:3439",
|
||||
"GlobalAPIRoot": "http://localhost:3439",
|
||||
"SiteUrl": "http://localhost:33081",
|
||||
"MockSpotify": "False",
|
||||
"Spotify": {
|
||||
|
||||
@ -1963,6 +1963,27 @@ namespace RMuseum.Controllers
|
||||
return BadRequest(res.ExceptionString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// import category data from ganjoor SQLite database (form file)
|
||||
/// </summary>
|
||||
/// <param name="catId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[Route("sqlite/import/cat/{catId}")]
|
||||
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)]
|
||||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
||||
public async Task<IActionResult> ImportCategoryFromSqlite(int catId)
|
||||
{
|
||||
IFormFile file = Request.Form.Files[0];
|
||||
|
||||
RServiceResult<bool> res =
|
||||
await _ganjoorService.ImportCategoryFromSqlite(catId, file);
|
||||
if (res.Result)
|
||||
return Ok();
|
||||
return BadRequest(res.ExceptionString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply corrections from sqlite
|
||||
/// </summary>
|
||||
|
||||
@ -1420,6 +1420,13 @@
|
||||
<param name="poetId"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.GanjoorController.ImportCategoryFromSqlite(System.Int32)">
|
||||
<summary>
|
||||
import category data from ganjoor SQLite database (form file)
|
||||
</summary>
|
||||
<param name="catId"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.GanjoorController.ApplyCorrectionsFromSqlite(System.Int32,System.String)">
|
||||
<summary>
|
||||
Apply corrections from sqlite
|
||||
@ -10920,6 +10927,14 @@
|
||||
<param name="file"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IGanjoorService.ImportCategoryFromSqlite(System.Int32,Microsoft.AspNetCore.Http.IFormFile)">
|
||||
<summary>
|
||||
import a catgory from sqlite
|
||||
</summary>
|
||||
<param name="catId"></param>
|
||||
<param name="file"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IGanjoorService.ApplyCorrectionsFromSqlite(System.Int32,Microsoft.AspNetCore.Http.IFormFile,System.String)">
|
||||
<summary>
|
||||
Apply corrections from sqlite
|
||||
@ -12998,6 +13013,14 @@
|
||||
<param name="file"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.ImportCategoryFromSqlite(System.Int32,Microsoft.AspNetCore.Http.IFormFile)">
|
||||
<summary>
|
||||
import a catgory from sqlite
|
||||
</summary>
|
||||
<param name="catId"></param>
|
||||
<param name="file"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.PrepareHtmlText(System.Collections.Generic.List{RMuseum.Models.Ganjoor.GanjoorVerse})">
|
||||
<summary>
|
||||
make html text
|
||||
|
||||
@ -487,6 +487,14 @@ namespace RMuseum.Services
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<bool>> ImportFromSqlite(int poetId, IFormFile file);
|
||||
|
||||
/// <summary>
|
||||
/// import a catgory from sqlite
|
||||
/// </summary>
|
||||
/// <param name="catId"></param>
|
||||
/// <param name="file"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<bool>> ImportCategoryFromSqlite(int catId, IFormFile file);
|
||||
|
||||
/// <summary>
|
||||
/// Apply corrections from sqlite
|
||||
/// </summary>
|
||||
|
||||
@ -589,9 +589,97 @@ namespace RMuseum.Services.Implementation
|
||||
{
|
||||
return new RServiceResult<bool>(false, exp.ToString());
|
||||
}
|
||||
|
||||
return new RServiceResult<bool>(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// import a catgory from sqlite
|
||||
/// </summary>
|
||||
/// <param name="catId"></param>
|
||||
/// <param name="file"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RServiceResult<bool>> ImportCategoryFromSqlite(int catId, IFormFile file)
|
||||
{
|
||||
try
|
||||
{
|
||||
string dir = Path.Combine($"{Configuration.GetSection("PictureFileService")["StoragePath"]}", "SQLiteImports");
|
||||
if (!Directory.Exists(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
string filePath = Path.Combine(dir, file.FileName);
|
||||
if (File.Exists(filePath))
|
||||
File.Delete(filePath);
|
||||
using (FileStream fsMain = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
await file.CopyToAsync(fsMain);
|
||||
}
|
||||
|
||||
_backgroundTaskQueue.QueueBackgroundWorkItem
|
||||
(
|
||||
async token =>
|
||||
{
|
||||
using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //this is long running job, so _context might be already been freed/collected by GC
|
||||
{
|
||||
LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context);
|
||||
var job = (await jobProgressServiceEF.NewJob("ImportFromSqlite", "Query data")).Result;
|
||||
|
||||
try
|
||||
{
|
||||
SqliteConnectionStringBuilder connectionStringBuilder = new SqliteConnectionStringBuilder();
|
||||
connectionStringBuilder.DataSource = filePath;
|
||||
using (SqliteConnection sqliteConnection = new SqliteConnection(connectionStringBuilder.ToString()))
|
||||
{
|
||||
await sqliteConnection.OpenAsync();
|
||||
IDbConnection sqlite = sqliteConnection;
|
||||
var poets = (await sqlite.QueryAsync("SELECT * FROM poet")).ToList();
|
||||
if (poets.Count != 1)
|
||||
{
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, "poets count in sqlite db is not equal to 1");
|
||||
}
|
||||
|
||||
var cats = (await sqlite.QueryAsync("SELECT * FROM cat")).ToList();
|
||||
if (cats.Count != 1)
|
||||
{
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, "cats count in sqlite db is not equal to 1");
|
||||
}
|
||||
|
||||
var cat = await context.GanjoorCategories.AsNoTracking().Where(c => c.Id == catId).SingleAsync();
|
||||
var poet = await context.GanjoorPoets.AsNoTracking().Where(p => p.Id == cat.PoetId).SingleAsync();
|
||||
var catPage = await context.GanjoorPages.AsNoTracking().Where(p => p.FullUrl == cat.FullUrl).SingleAsync();
|
||||
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 0, $"Importing");
|
||||
|
||||
var resImport = await _ImportSQLiteCatChildren(context, sqlite, poet.Id, await sqlite.QuerySingleAsync<int>($"SELECT id FROM cat WHERE parent_id = 0"), cat, poet.Nickname, jobProgressServiceEF, job, catPage.Id);
|
||||
|
||||
if (string.IsNullOrEmpty(resImport))
|
||||
{
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, resImport);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
File.Delete(filePath);
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
return new RServiceResult<bool>(false, exp.ToString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return new RServiceResult<bool>(true);
|
||||
}
|
||||
private async Task<string> _ImportSQLiteCatChildren(RMuseumDbContext context, IDbConnection sqlite, int poetId, int sqliteParentCatId, GanjoorCat parentCat, string parentFullTitle, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job, int parentPagId)
|
||||
@ -764,6 +852,10 @@ namespace RMuseum.Services.Implementation
|
||||
var poemId = 1 + maxPoemId;
|
||||
|
||||
int poemNumber = 0;
|
||||
if(await context.GanjoorPoems.AsNoTracking().Where(c => c.CatId == parentCat.Id).AnyAsync())
|
||||
{
|
||||
poemNumber = await context.GanjoorPoems.AsNoTracking().Where(c => c.CatId == parentCat.Id).CountAsync();
|
||||
}
|
||||
foreach (var poem in await sqlite.QueryAsync($"SELECT * FROM poem WHERE cat_id = {sqliteParentCatId} ORDER BY id"))
|
||||
{
|
||||
poemNumber++;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user