#65 importing mysql comments code

This commit is contained in:
Hamid Reza Mohammadi 2021-02-04 15:16:56 +03:30
parent 6a930a6a79
commit c4fcaf7e76
4 changed files with 118 additions and 11 deletions

View File

@ -229,7 +229,12 @@ namespace RMuseum.DbContext
/// PoemMusicTracks
/// </summary>
public DbSet<PoemMusicTrack> GanjoorPoemMusicTracks { get; set; }
/// <summary>
/// Ganjoor Comments
/// </summary>
public DbSet<GanjoorComment> GanjoorComments { get; set; }
}
}

View File

@ -17,12 +17,12 @@ namespace RMuseum.Models.Ganjoor
/// <summary>
/// Ganjoor Poem Id
/// </summary>
public int GanjoorPoemId { get; set; }
public int PoemId { get; set; }
/// <summary>
/// Ganjoor Poem Id
/// </summary>
public GanjoorPoem GanjoorPoem { get; set; }
public GanjoorPoem Poem { get; set; }
/// <summary>
/// user id
@ -52,7 +52,7 @@ namespace RMuseum.Models.Ganjoor
/// <summary>
/// author IP address (MySql imported field)
/// </summary>
public string AutherIpAddress { get; set; }
public string AuthorIpAddress { get; set; }
/// <summary>
/// comment date
@ -67,12 +67,12 @@ namespace RMuseum.Models.Ganjoor
/// <summary>
/// In Reply to Other Comment
/// </summary>
public virtual GanjoorComment ReferenceComment { get; set; }
public virtual GanjoorComment InReplyTo { get; set; }
/// <summary>
/// Reference Comment Id
/// </summary>
public Guid? ReferenceCommentId { get; set; }
public Guid? InReplyToId { get; set; }
/// <summary>
/// publish status

View File

@ -1283,6 +1283,11 @@
PoemMusicTracks
</summary>
</member>
<member name="P:RMuseum.DbContext.RMuseumDbContext.GanjoorComments">
<summary>
Ganjoor Comments
</summary>
</member>
<member name="T:RMuseum.Models.Artifact.PublishStatus">
<summary>
Items or pictures status
@ -3539,12 +3544,12 @@
Id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.GanjoorPoemId">
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.PoemId">
<summary>
Ganjoor Poem Id
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.GanjoorPoem">
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.Poem">
<summary>
Ganjoor Poem Id
</summary>
@ -3574,7 +3579,7 @@
author url (MySql imported field)
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.AutherIpAddress">
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.AuthorIpAddress">
<summary>
author IP address (MySql imported field)
</summary>
@ -3589,12 +3594,12 @@
comment
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.ReferenceComment">
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.InReplyTo">
<summary>
In Reply to Other Comment
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.ReferenceCommentId">
<member name="P:RMuseum.Models.Ganjoor.GanjoorComment.InReplyToId">
<summary>
Reference Comment Id
</summary>

View File

@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using MySql.Data.MySqlClient;
using RMuseum.DbContext;
using RMuseum.Models.Artifact;
using RMuseum.Models.Ganjoor;
using RMuseum.Models.MusicCatalogue;
using RSecurityBackend.Models.Generic;
@ -40,6 +41,12 @@ namespace RMuseum.Services.Implementation
var job = (await jobProgressServiceEF.NewJob("GanjoorService:ImportFromMySql", "pre open connection")).Result;
var resComments = await _ImportCommentsDataFromMySql(context, jobProgressServiceEF, job);
if(resComments.Result)//temporary
{
return;
}
MusicCatalogueService catalogueService = new MusicCatalogueService(Configuration, context);
RServiceResult<bool> musicCatalogueRes = await catalogueService.ImportFromMySql(jobProgressServiceEF, job);
@ -468,6 +475,96 @@ namespace RMuseum.Services.Implementation
}
private async Task<RServiceResult<bool>> _ImportCommentsDataFromMySql(RMuseumDbContext context, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job)
{
try
{
job = (await jobProgressServiceEF.UpdateJob(job.Id, 0, "phase comments - pre mysql data fetch")).Result;
string connectionString =
$"server={Configuration.GetSection("AudioMySqlServer")["Server"]};uid={Configuration.GetSection("AudioMySqlServer")["Username"]};pwd={Configuration.GetSection("AudioMySqlServer")["Password"]};database={Configuration.GetSection("AudioMySqlServer")["Database"]};charset=utf8;convert zero datetime=True";
using (MySqlConnection connection = new MySqlConnection
(
connectionString
))
{
connection.Open();
using (MySqlDataAdapter src = new MySqlDataAdapter(
"SELECT comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content, comment_approved FROM ganja_comments WHERE comment_type <> 'pingback' ORDER BY comment_ID",
connection))
{
job = (await jobProgressServiceEF.UpdateJob(job.Id, 0, "phase comments - mysql")).Result;
using (DataTable data = new DataTable())
{
await src.FillAsync(data);
job = (await jobProgressServiceEF.UpdateJob(job.Id, 0, "phase comments - processing approved poem songs")).Result;
int count = data.Rows.Count;
int i = 0;
int percent = -1;
foreach (DataRow row in data.Rows)
{
GanjoorComment comment = new GanjoorComment()
{
PoemId = int.Parse(row["comment_post_ID"].ToString()),
AuthorName = row["comment_author"].ToString(),
AuthorEmail = row["comment_author_email"].ToString(),
AuthorUrl = row["comment_author_url"].ToString(),
AuthorIpAddress = row["comment_author_IP"].ToString(),
CommentDate = (DateTime)row["comment_date"],
HtmlComment = row["comment_content"].ToString(),
Status = row["comment_approved"].ToString() == "1" ? PublishStatus.Published : PublishStatus.Awaiting
};
context.GanjoorComments.Add(comment);
await context.SaveChangesAsync(); //keeping original order of comments by saving things here
i++;
if(i * 100 / count > percent)
{
percent = i * 100 / count;
job = (await jobProgressServiceEF.UpdateJob(job.Id, 0, $"phase comments - {i} of {count}")).Result;
}
}
}
}
}
job = (await jobProgressServiceEF.UpdateJob(job.Id, 0, "phase comments - assigning comments to users")).Result;
foreach (var user in await context.Users.ToListAsync())
{
foreach(var comment in await context.GanjoorComments.Where(u => u.AuthorEmail == user.Email).ToListAsync())
{
comment.UserId = user.Id;
context.GanjoorComments.Update(comment);
}
}
await context.SaveChangesAsync();
job = (await jobProgressServiceEF.UpdateJob(job.Id, 0, "phase comments - finished")).Result;
return new RServiceResult<bool>(true);
}
catch(Exception exp)
{
return new RServiceResult<bool>(false, exp.ToString());
}
}
private List<GanjoorVerse> _extractVersesFromPoemHtmlText(string poemtext)
{
List<GanjoorVerse> verses = new List<GanjoorVerse>();