From fa39400d36b0a33962789ccd78b9fff211207710 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 17 Dec 2021 21:16:53 +0330 Subject: [PATCH] #264 service method for upvoting a recitation (without recomputing order code) --- RMuseum/DbContext/RMuseumDbContext.cs | 9 ++++ RMuseum/RMuseum.xml | 48 ++++++++++++++++-- .../Implementation/RecitationService.cs | 50 +++++++++++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/RMuseum/DbContext/RMuseumDbContext.cs b/RMuseum/DbContext/RMuseumDbContext.cs index 23ea45b1..210a169a 100644 --- a/RMuseum/DbContext/RMuseumDbContext.cs +++ b/RMuseum/DbContext/RMuseumDbContext.cs @@ -163,6 +163,10 @@ namespace RMuseum.DbContext builder.Entity() .Property(c => c.Longitude) .HasColumnType("decimal(12,9)"); + + builder.Entity() + .HasIndex(v => new { v.RecitationId, v.UserId }) + .IsUnique(); } @@ -400,5 +404,10 @@ namespace RMuseum.DbContext /// public DbSet RecitationErrorReports { get; set; } + /// + /// recitation user up votes + /// + public DbSet RecitationUserUpVotes { get; set; } + } } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 95cacd66..2d2f42e9 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2288,6 +2288,11 @@ recitation error reports + + + recitation user up votes + + donation expenditures @@ -3490,15 +3495,15 @@ Final data is actually exported to a MySQL database which this auto increment field is its key - - Caution: this could become a relation with newly added table - but if we do this we lose the ability to drop and create without losing narration data - Ganjoor Post Id + + Caution: this could become a relation with newly added table + but if we do this we lose the ability to drop and create without losing narration data + @@ -3754,6 +3759,41 @@ Last Excecption + + + recitation user up vote (the records would be deleted manually if the owner user account gets deleted) + + + + + Id + + + + + Recitation Id + + + + + Recitation + + + + + User Id, defining it this way causes no cascade relation + + + + + User + + + + + date time + + Narration Profiles for users diff --git a/RMuseum/Services/Implementation/RecitationService.cs b/RMuseum/Services/Implementation/RecitationService.cs index 34113abf..58c68252 100644 --- a/RMuseum/Services/Implementation/RecitationService.cs +++ b/RMuseum/Services/Implementation/RecitationService.cs @@ -1869,6 +1869,56 @@ namespace RMuseum.Services.Implementationa } } + /// + /// up vote a recitation + /// + /// + /// + /// + public async Task> UpVoteRecitationAsync(int id, Guid userId) + { + try + { + var existingVote = await _context.RecitationUserUpVotes.Where(r => r.RecitationId == id && r.UserId == userId).SingleOrDefaultAsync(); + if(existingVote != null) + { + return new RServiceResult(false, "شما پیش‌تر به این خوانش رأی داده‌اید."); + } + var recitation = await _context.Recitations.Where(r => r.Id == id).SingleOrDefaultAsync(); + if(recitation == null) + { + return new RServiceResult(false, "خوانشی با این شناسه وجود ندارد."); + } + if(recitation.OwnerId == userId) + { + return new RServiceResult(false, "شما نمی‌توانید به خوانش خودتان رأی بدهید."); + } + if(recitation.ReviewStatus != AudioReviewStatus.Approved) + { + return new RServiceResult(false, "امکان رأی دادن به خوانش منتشر نشده وجود ندارد."); + } + + RecitationUserUpVote vote = new RecitationUserUpVote() + { + RecitationId = id, + UserId = userId, + DateTime = DateTime.Now + }; + + _context.RecitationUserUpVotes.Add(vote); + await _context.SaveChangesAsync(); + + //TODO compute new order for poem recitations here + + return new RServiceResult(true); + + } + catch (Exception exp) + { + return new RServiceResult(false, exp.ToString()); + } + } + /// /// Upload Enabled (temporary switch off/on for upload) ///