#34 delete recitation (untested)

This commit is contained in:
Hamid Reza Mohammadi 2020-11-19 16:58:55 +03:30
parent 1321455283
commit dbe2cdc16c
4 changed files with 178 additions and 2 deletions

View File

@ -258,6 +258,27 @@ namespace RMuseum.Controllers
return Ok(res.Result);
}
/// <summary>
/// Delete a recitation
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete("{id}")]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
public async Task<IActionResult> DeleteRecitation(int id)
{
Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
RServiceResult<bool> res = await _audioService.Delete(id, loggedOnUserId);
if (!string.IsNullOrEmpty(res.ExceptionString))
{
return BadRequest(res.ExceptionString);
}
return Ok(res.Result);
}
/// <summary>
@ -319,7 +340,7 @@ namespace RMuseum.Controllers
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
[ProducesResponseType((int)HttpStatusCode.Forbidden, Type = typeof(string))]
public async Task<IActionResult> ReciationNarrations(bool replace = false)
public async Task<IActionResult> UploadReciation(bool replace = false)
{
try
{
@ -469,6 +490,7 @@ namespace RMuseum.Controllers
/// <returns></returns>
[HttpDelete("profile/{id}")]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Forbidden)]

View File

@ -753,6 +753,13 @@
<param name="model"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.RecitationController.DeleteRecitation(System.Int32)">
<summary>
Delete a recitation
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.RecitationController.GetUploads(RSecurityBackend.Models.Generic.PagingParameterModel,System.Boolean)">
<summary>
Get User Uploads
@ -761,7 +768,7 @@
<param name="allUsers">default: false, user must have narration::moderate permission to be able to see all users uploads</param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.RecitationController.ReciationNarrations(System.Boolean)">
<member name="M:RMuseum.Controllers.RecitationController.UploadReciation(System.Boolean)">
<summary>
Reciation Upload
</summary>
@ -4973,6 +4980,14 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.RecitationService.Delete(System.Int32,System.Guid)">
<summary>
Delete recitation
</summary>
<param name="id"></param>
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.RecitationService.GetPoemNarrationVerseSyncArray(System.Int32)">
<summary>
Gets Verse Sync Range Information
@ -5299,6 +5314,14 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IRecitationService.Delete(System.Int32,System.Guid)">
<summary>
Delete recitation (recitation should belong to userId)
</summary>
<param name="id"></param>
<param name="userId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IRecitationService.GetPoemNarrationVerseSyncArray(System.Int32)">
<summary>
Gets Verse Sync Range Information

View File

@ -30,6 +30,14 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<RecitationViewModel>> Get(int id);
/// <summary>
/// Delete recitation (recitation should belong to userId)
/// </summary>
/// <param name="id"></param>
/// <param name="userId"></param>
/// <returns></returns>
Task<RServiceResult<bool>> Delete(int id, Guid userId);
/// <summary>
/// Gets Verse Sync Range Information
/// </summary>

View File

@ -97,6 +97,77 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// Delete recitation
/// </summary>
/// <param name="id"></param>
/// <param name="userId"></param>
/// <returns></returns>
public async Task<RServiceResult<bool>> Delete(int id, Guid userId)
{
try
{
Recitation recitation = await _context.Recitations.Where(a => a.Id == id && a.OwnerId == userId).FirstOrDefaultAsync();
if(recitation == null)
{
return new RServiceResult<bool>(false, "404");
}
if (recitation.ReviewStatus == AudioReviewStatus.Approved)
{
recitation.AudioSyncStatus = (int)AudioSyncStatus.Deleted;
_context.Recitations.Update(recitation);
await _context.SaveChangesAsync();
_backgroundTaskQueue.QueueBackgroundWorkItem
(
async token =>
{
using (RMuseumDbContext context = new RMuseumDbContext(Configuration)) //this is long running job, so _context might be already been freed/collected by GC
{
await _DeleteNarrationFromRemote(recitation, context);
}
});
}
else
{
await _FinalizeDelete(_context, recitation);
}
return new RServiceResult<bool>(true);
}
catch (Exception exp)
{
return new RServiceResult<bool>(false, exp.ToString());
}
}
private async Task<string> _FinalizeDelete(RMuseumDbContext context, Recitation recitation)
{
try
{
string mp3 = recitation.LocalMp3FilePath;
string xml = recitation.LocalXmlFilePath;
context.Recitations.Remove(recitation);
await context.SaveChangesAsync();
File.Delete(mp3);
File.Delete(xml);
return "";
}
catch(Exception exp)
{
return exp.ToString();
}
}
/// <summary>
/// Gets Verse Sync Range Information
/// </summary>
@ -945,6 +1016,58 @@ namespace RMuseum.Services.Implementation
}
}
private async Task _DeleteNarrationFromRemote(Recitation narration, RMuseumDbContext context)
{
using var client = new SftpClient
(
Configuration.GetSection("AudioSFPServer")["Host"],
int.Parse(Configuration.GetSection("AudioSFPServer")["Port"]),
Configuration.GetSection("AudioSFPServer")["Username"],
Configuration.GetSection("AudioSFPServer")["Password"]
);
try
{
client.Connect();
client.DeleteFile($"{Configuration.GetSection("AudioSFPServer")["RootPath"]}{narration.RemoteXMLFilePath}");
client.DeleteFile($"{Configuration.GetSection("AudioSFPServer")["RootPath"]}{narration.RemoteMp3FilePath}");
string sql = $"DELETE FROM ganja_gaudio WHERE audio_post_ID = {narration.GanjoorPostId} AND audio_order = {narration.AudioOrder} AND audio_guid = '{narration.LegacyAudioGuid}'";
using (MySqlConnection connection = new MySqlConnection
(
$"server={Configuration.GetSection("AudioMySqlServer")["Server"]};uid={Configuration.GetSection("AudioMySqlServer")["Username"]};pwd={Configuration.GetSection("AudioMySqlServer")["Password"]};database={Configuration.GetSection("AudioMySqlServer")["Database"]};charset=utf8"
))
{
await connection.OpenAsync();
await connection.ExecuteAsync(sql);
}
//We are using two database for different purposes on the remote
using (MySqlConnection connection = new MySqlConnection
(
$"server={Configuration.GetSection("AudioMySqlServer")["Server"]};uid={Configuration.GetSection("AudioMySqlServer")["2ndUsername"]};pwd={Configuration.GetSection("AudioMySqlServer")["2ndPassword"]};database={Configuration.GetSection("AudioMySqlServer")["2ndDatabase"]};charset=utf8"
))
{
await connection.OpenAsync();
await connection.ExecuteAsync(sql);
}
await _FinalizeDelete(context, narration);
}
catch
{
//if an error occurs, narration.AudioSyncStatus is not updated and narration can be idetified later to do "retrypublish" attempts
}
finally
{
client.Disconnect();
}
}
/// <summary>
/// retry publish unpublished narrations
/// </summary>