From b21ba055d19e82e9973659af5037f72683e4f3d2 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 19 Jun 2021 20:14:33 +0430 Subject: [PATCH] #156 api is working and complete --- RMuseum/Controllers/GanjoorController.cs | 19 +++++++++- RMuseum/RMuseum.xml | 14 ++++++++ RMuseum/Services/IGanjoorService.cs | 7 ++++ .../GanjoorService-SQLiteImport.cs | 35 +++++++++++++++---- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 5bccde79..8db39e9e 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -1175,7 +1175,24 @@ namespace RMuseum.Controllers return BadRequest(res.ExceptionString); } - + /// + /// export a poet to sqlite database + /// + /// + /// + [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 ExportToSqlite(int poetId) + { + RServiceResult 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"); + } /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 32dd3145..84f42794 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -1002,6 +1002,13 @@ + + + export a poet to sqlite database + + + + imports data from ganjoor MySql database @@ -7432,6 +7439,13 @@ + + + export to sqlite + + + + import GanjoorPage entity data from MySql diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index 273a4b17..37e839f3 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -305,6 +305,13 @@ namespace RMuseum.Services /// Task> ImportFromSqlite(int poetId, IFormFile file); + /// + /// export to sqlite + /// + /// + /// + Task> ExportToSqlite(int poetId); + /// /// import GanjoorPage entity data from MySql /// diff --git a/RMuseum/Services/Implementation/GanjoorService-Imports/GanjoorService-SQLiteImport.cs b/RMuseum/Services/Implementation/GanjoorService-Imports/GanjoorService-SQLiteImport.cs index 0760608e..0686093e 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Imports/GanjoorService-SQLiteImport.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Imports/GanjoorService-SQLiteImport.cs @@ -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); + + } + /// /// import from sqlite ///