From d21eca913665d31fe5acb3ce9577203e67d00ea7 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Tue, 27 Oct 2020 20:30:32 +0330 Subject: [PATCH] fixes and improvements --- .../ViewModels/PoemNarrationViewModel.cs | 11 +++---- RMuseum/RMuseum.xml | 8 ++--- .../Implementation/AudioNarrationService.cs | 29 +++++++++---------- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/RMuseum/Models/GanjoorAudio/ViewModels/PoemNarrationViewModel.cs b/RMuseum/Models/GanjoorAudio/ViewModels/PoemNarrationViewModel.cs index e18e2932..97d907b4 100644 --- a/RMuseum/Models/GanjoorAudio/ViewModels/PoemNarrationViewModel.cs +++ b/RMuseum/Models/GanjoorAudio/ViewModels/PoemNarrationViewModel.cs @@ -1,4 +1,5 @@ using RMuseum.Models.Ganjoor; +using RSecurityBackend.Models.Auth.Db; using RSecurityBackend.Models.Auth.ViewModels; using System; using System.Collections.Generic; @@ -14,12 +15,12 @@ namespace RMuseum.Models.GanjoorAudio.ViewModels /// constructor /// /// + /// /// - public PoemNarrationViewModel(PoemNarration src, GanjoorPoem poem) + public PoemNarrationViewModel(PoemNarration src, RAppUser owner, GanjoorPoem poem) { Id = src.Id; - OwnerId = src.OwnerId; - Owner = src.Owner == null ? null : new PublicRAppUser(src.Owner); + Owner = new PublicRAppUser(owner); GanjoorAudioId = src.GanjoorAudioId; GanjoorPostId = src.GanjoorPostId; AudioOrder = src.AudioOrder; @@ -77,10 +78,6 @@ namespace RMuseum.Models.GanjoorAudio.ViewModels /// public PublicRAppUser Owner { get; set; } - /// - /// Owner Id - /// - public Guid OwnerId { get; set; } /// /// Final data is actually exported to a MySQL database which this auto increment field is its key diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 53d36eed..ec8bef4e 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2046,11 +2046,12 @@ Poem Narration view model - + constructor + @@ -2063,11 +2064,6 @@ Owner User - - - Owner Id - - Final data is actually exported to a MySQL database which this auto increment field is its key diff --git a/RMuseum/Services/Implementation/AudioNarrationService.cs b/RMuseum/Services/Implementation/AudioNarrationService.cs index b206fe66..6cc710dd 100644 --- a/RMuseum/Services/Implementation/AudioNarrationService.cs +++ b/RMuseum/Services/Implementation/AudioNarrationService.cs @@ -10,6 +10,7 @@ using RMuseum.Models.Ganjoor; using RMuseum.Models.GanjoorAudio; using RMuseum.Models.GanjoorAudio.ViewModels; using RMuseum.Models.UploadSession; +using RSecurityBackend.Models.Auth.Db; using RSecurityBackend.Models.Auth.ViewModels; using RSecurityBackend.Models.Generic; using RSecurityBackend.Services.Implementation; @@ -39,32 +40,24 @@ namespace RMuseum.Services.Implementation { try { - + //whenever I had not a reference to audio.Owner in the final selection it became null, so this strange arrangement is not all because of my stupidity! var source = - _context.AudioFiles.Include(a => a.Owner) + from audio in _context.AudioFiles + .Include(a => a.Owner) .Where(a => (filteredUserId == Guid.Empty || a.OwnerId == filteredUserId) && (status == AudioReviewStatus.All || a.ReviewStatus == status) ) .OrderByDescending(a => a.UploadDate) - .Select(a => new PoemNarrationViewModel(a, null)); + join poem in _context.GanjoorPoems + on audio.GanjoorPostId equals poem.Id + select new PoemNarrationViewModel(audio, audio.Owner, poem); (PaginationMetadata PagingMeta, PoemNarrationViewModel[] Items) paginatedResult = await QueryablePaginator.Paginate(source, paging); - var commonOwner = filteredUserId == Guid.Empty ? null : new PublicRAppUser(await _context.Users.Where(u => u.Id == filteredUserId).SingleOrDefaultAsync()); - foreach (var item in paginatedResult.Items) - { - GanjoorPoem poem = await _context.GanjoorPoems.Where(p => p.Id == item.GanjoorPostId).SingleOrDefaultAsync(); - if(poem != null) - { - item.PoemFullTitle = poem.FullTitle; - item.PoemFullUrl = poem.FullUrl; - } - item.Owner = filteredUserId == Guid.Empty ? new PublicRAppUser(await _context.Users.Where(u => u.Id == item.OwnerId).SingleOrDefaultAsync()) : commonOwner; - } - return new RServiceResult<(PaginationMetadata PagingMeta, PoemNarrationViewModel[] Items)>((paginatedResult.PagingMeta, paginatedResult.Items)); + return new RServiceResult<(PaginationMetadata PagingMeta, PoemNarrationViewModel[] Items)>(paginatedResult); } catch (Exception exp) { @@ -81,13 +74,14 @@ namespace RMuseum.Services.Implementation { try { + //whenever I had not a reference to audio.Owner in the final selection it became null, so this strange arrangement is not all because of my stupidity! var source = from audio in _context.AudioFiles .Include(a => a.Owner) .Where(a => a.Id == id) join poem in _context.GanjoorPoems on audio.GanjoorPostId equals poem.Id - select new PoemNarrationViewModel(audio, poem); + select new PoemNarrationViewModel(audio, audio.Owner, poem); var narration = await source.SingleOrDefaultAsync(); return new RServiceResult(narration); @@ -147,6 +141,9 @@ namespace RMuseum.Services.Implementation )) { connection.Open(); + //I thought that result Id fields would become corresponant to order of selection (and later insertions) but it is not + //the case in batch insertion, so this ORDER BY clause is useless unless we do save every time we insert a record + //which I guess might take much longer using(MySqlDataAdapter src = new MySqlDataAdapter( "SELECT audio_ID, audio_post_ID, audio_order, audio_xml, audio_ogg, audio_mp3, " + "audio_title, audio_artist, audio_artist_url, audio_src, audio_src_url, audio_guid, " +