#156 api is working and complete

This commit is contained in:
Hamid Reza Mohammadi 2021-06-19 20:14:33 +04:30
parent ef6c96d4dd
commit b21ba055d1
4 changed files with 68 additions and 7 deletions

View File

@ -1175,7 +1175,24 @@ namespace RMuseum.Controllers
return BadRequest(res.ExceptionString);
}
/// <summary>
/// export a poet to sqlite database
/// </summary>
/// <param name="poetId"></param>
/// <returns></returns>
[HttpGet]
[Route("sqliteexport/{poetId}")]
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(FileStreamResult))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> ExportToSqlite(int poetId)
{
RServiceResult<string> res =
await _ganjoorService.ExportToSqlite(poetId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return new FileStreamResult(new FileStream(res.Result, FileMode.Open, FileAccess.Read), "application/octet-stream");
}
/// <summary>

View File

@ -1002,6 +1002,13 @@
<param name="poetId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.ExportToSqlite(System.Int32)">
<summary>
export a poet to sqlite database
</summary>
<param name="poetId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.ImportFromMySql">
<summary>
imports data from ganjoor MySql database
@ -7432,6 +7439,13 @@
<param name="file"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.ExportToSqlite(System.Int32)">
<summary>
export to sqlite
</summary>
<param name="poetId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.ImportFromMySql">
<summary>
import GanjoorPage entity data from MySql

View File

@ -305,6 +305,13 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<bool>> ImportFromSqlite(int poetId, IFormFile file);
/// <summary>
/// export to sqlite
/// </summary>
/// <param name="poetId"></param>
/// <returns></returns>
Task<RServiceResult<string>> ExportToSqlite(int poetId);
/// <summary>
/// import GanjoorPage entity data from MySql
/// </summary>

View File

@ -34,13 +34,13 @@ namespace RMuseum.Services.Implementation
try
{
var poet = await _context.GanjoorPoets.AsNoTracking().Where(p => p.Id == poetId).SingleAsync();
var cat = await _context.GanjoorCategories.AsNoTracking().Where(c => c.PoetId == poetId && c.ParentId == null).SingleAsync();
var catPoet = await _context.GanjoorCategories.AsNoTracking().Where(c => c.PoetId == poetId && c.ParentId == null).SingleAsync();
string dir = Path.Combine($"{_configuration.GetSection("PictureFileService")["StoragePath"]}", "SQLiteExports");
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string filePath = Path.Combine(dir, $"{cat.UrlSlug}.gdb");
string filePath = Path.Combine(dir, $"{catPoet.UrlSlug}.gdb");
if (File.Exists(filePath))
File.Delete(filePath);
@ -53,17 +53,22 @@ namespace RMuseum.Services.Implementation
{
await sqliteConnection.OpenAsync();
string sql = "BEGIN TRANSACTION;" +
string q = "BEGIN TRANSACTION;" +
"CREATE TABLE [cat] ([id] INTEGER PRIMARY KEY NOT NULL,[poet_id] INTEGER NULL,[text] NVARCHAR(100) NULL,[parent_id] INTEGER NULL,[url] NVARCHAR(255) NULL);"
+
"CREATE TABLE poem (id INTEGER PRIMARY KEY, cat_id INTEGER, title NVARCHAR(255), url NVARCHAR(255));"
"CREATE TABLE [poem] (id INTEGER PRIMARY KEY, cat_id INTEGER, title NVARCHAR(255), url NVARCHAR(255));"
+
"CREATE TABLE [poet] ([id] INTEGER PRIMARY KEY NOT NULL,[name] NVARCHAR(20) NULL,[cat_id] INTEGER NULL NULL);"
"CREATE TABLE [poet] ([id] INTEGER PRIMARY KEY NOT NULL,[name] NVARCHAR(20) NULL,[cat_id] INTEGER NULL NULL, [description] TEXT);"
+
"CREATE TABLE [verse] ([poem_id] INTEGER NULL,[vorder] INTEGER NULL,[position] INTEGER NULL,[text] TEXT NULL);"
+
"COMMIT;";
await sqliteConnection.ExecuteAsync(sql);
await sqliteConnection.ExecuteAsync(q);
await sqliteConnection.ExecuteAsync("BEGIN;");
await sqliteConnection.ExecuteAsync($"INSERT INTO poet (id, name, cat_id, description) VALUES ({poet.Id}, '{poet.Nickname}', {catPoet.Id}, '{poet.Description}');");
await ExportCarToSqlite(sqliteConnection, catPoet);
await sqliteConnection.ExecuteAsync("COMMIT;");
}
@ -75,6 +80,24 @@ namespace RMuseum.Services.Implementation
}
}
private async Task ExportCarToSqlite(SqliteConnection sqliteConnection, GanjoorCat cat)
{
int parentId = cat.ParentId == null ? 0 : (int)cat.ParentId;
await sqliteConnection.ExecuteAsync($"INSERT INTO cat (id, poet_id, text, parent_id, url) VALUES ({cat.Id}, {cat.PoetId}, '{cat.Title}', {parentId}, 'https://ganjoor.net{cat.FullUrl}');");
var poems = await _context.GanjoorPoems.AsNoTracking().Where(p => p.CatId == cat.Id).ToListAsync();
foreach(var poem in poems)
{
await sqliteConnection.ExecuteAsync($"INSERT INTO poem (id, cat_id, title, url) VALUES ({poem.Id}, {poem.CatId}, '{poem.Title}', 'https://ganjoor.net{poem.FullUrl}');");
foreach (var verse in await _context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == poem.Id).OrderBy(v => v.VOrder).ToListAsync())
await sqliteConnection.ExecuteAsync($"INSERT INTO verse (poem_id, vorder, position, text) VALUES ({poem.Id}, {verse.VOrder}, {(int)verse.VersePosition}, '{verse.Text}');");
}
foreach (var child in await _context.GanjoorCategories.AsNoTracking().Where(c => c.ParentId == cat.Id).ToListAsync())
await ExportCarToSqlite(sqliteConnection, child);
}
/// <summary>
/// import from sqlite
/// </summary>