From 798edb14e24a59554858be70dbcd5477a0703b5d Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 25 Sep 2021 18:19:58 +0330 Subject: [PATCH] #208 bookmark related methods --- RMuseum/DbContext/RMuseumDbContext.cs | 7 +- RMuseum/Models/Bookmark/RBookmarkType.cs | 6 +- ...joorBookmark.cs => GanjoorUserBookmark.cs} | 2 +- RMuseum/RMuseum.xml | 223 +++++++++++++----- RMuseum/Services/IGanjoorService.cs | 46 +++- .../GanjoorService-Bookmarking.cs | 121 ++++++++++ 6 files changed, 341 insertions(+), 64 deletions(-) rename RMuseum/Models/Ganjoor/{GanjoorBookmark.cs => GanjoorUserBookmark.cs} (97%) create mode 100644 RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Bookmarking.cs diff --git a/RMuseum/DbContext/RMuseumDbContext.cs b/RMuseum/DbContext/RMuseumDbContext.cs index 34b63de5..4cf9c0b6 100644 --- a/RMuseum/DbContext/RMuseumDbContext.cs +++ b/RMuseum/DbContext/RMuseumDbContext.cs @@ -334,6 +334,11 @@ namespace RMuseum.DbContext /// poem translations /// public DbSet GanjoorPoemTranslations { get; set; } - + + /// + /// ganjoor bookmarks + /// + public DbSet GanjoorUserBookmarks { get; set; } + } } diff --git a/RMuseum/Models/Bookmark/RBookmarkType.cs b/RMuseum/Models/Bookmark/RBookmarkType.cs index f577c325..a2593dc2 100644 --- a/RMuseum/Models/Bookmark/RBookmarkType.cs +++ b/RMuseum/Models/Bookmark/RBookmarkType.cs @@ -12,6 +12,10 @@ /// /// Public Favorite /// - Favorite = 1 + Favorite = 1, + /// + /// Private Note (GanjoorComment) + /// + Note = 2 } } diff --git a/RMuseum/Models/Ganjoor/GanjoorBookmark.cs b/RMuseum/Models/Ganjoor/GanjoorUserBookmark.cs similarity index 97% rename from RMuseum/Models/Ganjoor/GanjoorBookmark.cs rename to RMuseum/Models/Ganjoor/GanjoorUserBookmark.cs index 575aff2e..35341d8f 100644 --- a/RMuseum/Models/Ganjoor/GanjoorBookmark.cs +++ b/RMuseum/Models/Ganjoor/GanjoorUserBookmark.cs @@ -7,7 +7,7 @@ namespace RMuseum.Models.Ganjoor /// /// Ganjoor Bookmark /// - public class GanjoorBookmark + public class GanjoorUserBookmark { /// /// Id diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index e719e4f4..24e44cd9 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -1916,6 +1916,11 @@ poem translations + + + ganjoor bookmarks + + donation expenditures @@ -2934,6 +2939,11 @@ Public Favorite + + + Private Note (GanjoorComment) + + User Bookmarks @@ -4359,66 +4369,6 @@ correction review result - - - Ganjoor Bookmark - - - - - Id - - - - - User Id - - - - - User - - - - - Poem Id - - - - - Poem - - - - - Verse Id - - - - - Verse - - - - - DateTime - - - - - Type - - - - - rating - - - - - User Note - - Ganjoor Category @@ -5293,6 +5243,66 @@ ganjoor TOC options + + + Ganjoor Bookmark + + + + + Id + + + + + User Id + + + + + User + + + + + Poem Id + + + + + Poem + + + + + Verse Id + + + + + Verse + + + + + DateTime + + + + + Type + + + + + rating + + + + + User Note + + Ganjoor Verse @@ -8582,6 +8592,51 @@ + + + Bookmark Verse + + + + + + + + + + get user ganjoor bookmarks + + + + + + + + + get verse bookmarks + + + + + + + + + delete user bookmark + /// + + to make sure a user can not delete another user's bookmarks + + + + + get user bookmarks (artifacts and items) + + + + + + translation service implementation @@ -9620,6 +9675,54 @@ IGanjoorService implementation + + IGanjoorService implementation + + + + + Bookmark Verse + + + + + + + + + + get user ganjoor bookmarks + + + + + + + + + get verse bookmarks + + + + + + + + + delete user bookmark + /// + + to make sure a user can not delete another user's bookmarks + + + + + get user bookmarks (artifacts and items) + + + + + diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index d4600fcc..4dd29292 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Http; +using RMuseum.Models.Bookmark; using RMuseum.Models.Ganjoor; using RMuseum.Models.Ganjoor.ViewModels; using RMuseum.Models.GanjoorAudio.ViewModels; @@ -566,6 +567,49 @@ namespace RMuseum.Services /// RServiceResult StartUpdatingMundexPage(Guid editingUserId); - + /// + /// Bookmark Verse + /// + /// + /// + /// + /// + /// + Task> BookmarkVerse(int poemId, int verseId, Guid userId, RBookmarkType type); + + /// + /// get user ganjoor bookmarks + /// + /// + /// + /// + /// + Task> GetPoemGanjoorUserBookmarks(int poemId, Guid userId, RBookmarkType type); + + /// + /// get verse bookmarks + /// + /// + /// + /// + /// + Task> GetVerseGanjoorUserBookmarks(int poemId, int verseId, Guid userId); + + /// + /// delete user bookmark + /// /// + /// + /// to make sure a user can not delete another user's bookmarks + /// + Task> DeleteGanjoorBookmark(Guid bookmarkId, Guid userId); + + /// + /// get user bookmarks (artifacts and items) + /// + /// + /// + /// + /// + Task> GetBookmarks(PagingParameterModel paging, Guid userId, RBookmarkType type); } } diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Bookmarking.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Bookmarking.cs new file mode 100644 index 00000000..31575ba1 --- /dev/null +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Bookmarking.cs @@ -0,0 +1,121 @@ +using Microsoft.EntityFrameworkCore; +using RMuseum.Models.Ganjoor; +using RSecurityBackend.Models.Generic; +using System; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using RMuseum.Models.Artifact; +using RSecurityBackend.Services.Implementation; +using RMuseum.Models.Bookmark; + +namespace RMuseum.Services.Implementation +{ + /// + /// IGanjoorService implementation + /// + public partial class GanjoorService : IGanjoorService + { + /// + /// Bookmark Verse + /// + /// + /// + /// + /// + /// + public async Task> BookmarkVerse(int poemId, int verseId, Guid userId, RBookmarkType type) + { + if ((await _context.GanjoorUserBookmarks.Where(b => b.UserId == userId && b.PoemId == poemId && b.VerseId == verseId && b.RBookmarkType == type).SingleOrDefaultAsync()) != null) + { + return new RServiceResult(null, "Artifact is already bookmarked/faved."); + } + + GanjoorUserBookmark bookmark = + new GanjoorUserBookmark() + { + UserId = userId, + PoemId = poemId, + VerseId = verseId, + RBookmarkType = type, + DateTime = DateTime.Now, + Note = "" + }; + + _context.GanjoorUserBookmarks.Add(bookmark); + await _context.SaveChangesAsync(); + + return new RServiceResult(bookmark); + } + + /// + /// get user ganjoor bookmarks + /// + /// + /// + /// + /// + public async Task> GetPoemGanjoorUserBookmarks(int poemId, Guid userId, RBookmarkType type) + { + GanjoorUserBookmark[] bookmarks = await _context.GanjoorUserBookmarks.Where(b => b.PoemId == poemId && b.UserId == userId && b.RBookmarkType == type).ToArrayAsync(); + return new RServiceResult(bookmarks); + } + + /// + /// get verse bookmarks + /// + /// + /// + /// + /// + public async Task> GetVerseGanjoorUserBookmarks(int poemId, int verseId, Guid userId) + { + GanjoorUserBookmark[] bookmarks = await _context.GanjoorUserBookmarks.Where(b => b.PoemId == poemId && b.VerseId == verseId && b.UserId == userId).ToArrayAsync(); + return new RServiceResult(bookmarks); + } + + + /// + /// delete user bookmark + /// /// + /// + /// to make sure a user can not delete another user's bookmarks + /// + public async Task> DeleteGanjoorBookmark(Guid bookmarkId, Guid userId) + { + GanjoorUserBookmark bookmark = await _context.GanjoorUserBookmarks.Where(b => b.Id == bookmarkId && b.UserId == userId).SingleOrDefaultAsync(); + if (bookmark == null) + { + return new RServiceResult(false, "bookmark not found"); + } + _context.GanjoorUserBookmarks.Remove(bookmark); + await _context.SaveChangesAsync(); + return new RServiceResult(true); + } + + /// + /// get user bookmarks (artifacts and items) + /// + /// + /// + /// + /// + public async Task> GetBookmarks(PagingParameterModel paging, Guid userId, RBookmarkType type) + { + var source = + _context.GanjoorUserBookmarks + .Include(b => b.Poem) + .Include(b => b.Verse) + .Where(b => b.UserId == userId && b.RBookmarkType == type) + .OrderByDescending(b => b.DateTime) + .AsQueryable(); + + (PaginationMetadata PagingMeta, GanjoorUserBookmark[] Bookmarks) paginatedResult = + await QueryablePaginator.Paginate(source, paging); + + + + return new RServiceResult<(PaginationMetadata PagingMeta, GanjoorUserBookmark[] Bookmarks)>(paginatedResult); + } + } +} \ No newline at end of file