From 7876b16d057989f88e582efb5d14aaced23d2477 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 22 Jul 2023 14:35:05 +0330 Subject: [PATCH 1/8] #399 refactoring --- .../PDFLibraryService-Partials/PDFLibraryService-LocalPDF.cs | 2 +- RMuseum/Services/Implementation/PDFLibraryService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/RMuseum/Services/Implementation/PDFLibraryService-Partials/PDFLibraryService-LocalPDF.cs b/RMuseum/Services/Implementation/PDFLibraryService-Partials/PDFLibraryService-LocalPDF.cs index f81cdb20..d0b438ca 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService-Partials/PDFLibraryService-LocalPDF.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService-Partials/PDFLibraryService-LocalPDF.cs @@ -20,7 +20,7 @@ namespace RMuseum.Services.Implementation { public partial class PDFLibraryService { - private async Task> ImportLocalPDFFile(RMuseumDbContext context, int bookId, int? volumeId, int volumeOrder, string filePath, string srcUrl, bool skipUpload) + private async Task> ImportLocalPDFFileAsync(RMuseumDbContext context, int bookId, int? volumeId, int volumeOrder, string filePath, string srcUrl, bool skipUpload) { string fileChecksum = PoemAudio.ComputeCheckSum(filePath); if ( diff --git a/RMuseum/Services/Implementation/PDFLibraryService.cs b/RMuseum/Services/Implementation/PDFLibraryService.cs index d10fc17c..aa236e6e 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService.cs @@ -29,7 +29,7 @@ namespace RMuseum.Services.Implementation { using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions())) { - var pdfRes = await ImportLocalPDFFile(context, model.BookId, model.MultiVolumePDFCollectionId, model.VolumeOrder, model.LocalImportingPDFFilePath, model.OriginalSourceUrl, model.SkipUpload); + var pdfRes = await ImportLocalPDFFileAsync(context, model.BookId, model.MultiVolumePDFCollectionId, model.VolumeOrder, model.LocalImportingPDFFilePath, model.OriginalSourceUrl, model.SkipUpload); if (pdfRes.Result != null) { var pdfBook = pdfRes.Result; From 42db898c214412849db7d813192a4437b07a61b0 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 22 Jul 2023 15:40:14 +0330 Subject: [PATCH 2/8] #399 add author + book --- .../Implementation/PDFLibraryService.cs | 65 ++++++++++++++++++- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/RMuseum/Services/Implementation/PDFLibraryService.cs b/RMuseum/Services/Implementation/PDFLibraryService.cs index aa236e6e..a429ea9e 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService.cs @@ -1,12 +1,16 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using RMuseum.DbContext; +using RMuseum.Models.Artifact; using RMuseum.Models.PDFLibrary; using RMuseum.Models.PDFLibrary.ViewModels; using RSecurityBackend.Models.Generic; using RSecurityBackend.Services; +using RSecurityBackend.Services.Implementation; +using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; namespace RMuseum.Services.Implementation { @@ -49,7 +53,7 @@ namespace RMuseum.Services.Implementation pdfBook.OriginalSourceName = model.OriginalSourceName; pdfBook.OriginalFileUrl = model.OriginalFileUrl; List roles = new List(); - if(model.WriterId != null) + if (model.WriterId != null) { roles.Add(new AuthorRole() { @@ -81,7 +85,7 @@ namespace RMuseum.Services.Implementation Role = "نویسنده", }); } - if(model.TranslatorId != null) + if (model.TranslatorId != null) { roles.Add(new AuthorRole() { @@ -129,7 +133,7 @@ namespace RMuseum.Services.Implementation Role = "مصحح", }); } - if(roles.Count > 0) + if (roles.Count > 0) { pdfBook.Contributers = roles; } @@ -145,6 +149,61 @@ namespace RMuseum.Services.Implementation return new RServiceResult(true); } + /// + /// add author + /// + /// + /// + public async Task> AddAuthorAsync(Author author) + { + try + { + _context.Add(author); + await _context.SaveChangesAsync(); + return new RServiceResult(author); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + + /// + /// get authors + /// + /// + /// + /// + public async Task> GetAuthorsAsync(PagingParameterModel paging, string authorName) + { + var source = + _context.Authors + .Where(a => string.IsNullOrEmpty(authorName) || (authorName.Contains(a.Name) || (!string.IsNullOrEmpty(a.NameInOriginalLanguage) && authorName.Contains(a.NameInOriginalLanguage)) )) + .AsQueryable(); + (PaginationMetadata PagingMeta, Author[] Items) paginatedResult = + await QueryablePaginator.Paginate(source, paging); + return new RServiceResult<(PaginationMetadata PagingMeta, Author[] Authors)>(paginatedResult); + } + + /// + /// add book + /// + /// + /// + public async Task> AddBookAsync(Book book) + { + try + { + _context.Add(book); + await _context.SaveChangesAsync(); + return new RServiceResult(book); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// Database Context /// From 74c5a5bbac85ae420eab06d11510fdcaf5d94d8c Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 22 Jul 2023 15:55:22 +0330 Subject: [PATCH 3/8] #399 some methods for retrieving data from PDF service --- RMuseum/RMuseum.xml | 43 +++++++++++ .../Implementation/PDFLibraryService.cs | 72 +++++++++++++++---- 2 files changed, 102 insertions(+), 13 deletions(-) diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index e9cfd8e2..6c780602 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -17407,6 +17407,13 @@ طول تصویر بندانگشتی + + + get pdf book by id + + + + start importing local pdf file @@ -17414,6 +17421,42 @@ + + + add author + + + + + + + get author by id + + + + + + + get authors + + + + + + + + add book + + + + + + + add multi volume pdf collection + + + + Database Context diff --git a/RMuseum/Services/Implementation/PDFLibraryService.cs b/RMuseum/Services/Implementation/PDFLibraryService.cs index a429ea9e..b9b4c587 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService.cs @@ -1,7 +1,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using RMuseum.DbContext; -using RMuseum.Models.Artifact; using RMuseum.Models.PDFLibrary; using RMuseum.Models.PDFLibrary.ViewModels; using RSecurityBackend.Models.Generic; @@ -19,6 +18,31 @@ namespace RMuseum.Services.Implementation /// public partial class PDFLibraryService : IPDFLibraryService { + /// + /// get pdf book by id + /// + /// + /// + public async Task> GetPDFBookByIdAsync(int id) + { + try + { + var pdfBook = await _context.PDFBooks.AsNoTracking() + .Include(b => b.Book) + .Include(b => b.MultiVolumePDFCollection) + .Include(b => b.Contributers) + .Include(b => b.Tags) + .Include(b => b.Pages) + .Where(b => b.Id == id) + .SingleOrDefaultAsync(); + return new RServiceResult(pdfBook); + + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } /// /// start importing local pdf file @@ -141,14 +165,10 @@ namespace RMuseum.Services.Implementation await context.SaveChangesAsync(); } } - } ); - - return new RServiceResult(true); } - /// /// add author /// @@ -167,7 +187,22 @@ namespace RMuseum.Services.Implementation return new RServiceResult(null, exp.ToString()); } } - + /// + /// get author by id + /// + /// + /// + public async Task> GetAuthorByIdAsync(int id) + { + try + { + return new RServiceResult(await _context.Authors.AsNoTracking().Where(a => a.Id == id).SingleOrDefaultAsync()); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } /// /// get authors /// @@ -184,7 +219,6 @@ namespace RMuseum.Services.Implementation await QueryablePaginator.Paginate(source, paging); return new RServiceResult<(PaginationMetadata PagingMeta, Author[] Authors)>(paginatedResult); } - /// /// add book /// @@ -203,28 +237,40 @@ namespace RMuseum.Services.Implementation return new RServiceResult(null, exp.ToString()); } } - + /// + /// add multi volume pdf collection + /// + /// + /// + public async Task> AddMultiVolumePDFCollection(MultiVolumePDFCollection multiVolumePDFCollection) + { + try + { + _context.Add(multiVolumePDFCollection); + await _context.SaveChangesAsync(); + return new RServiceResult(multiVolumePDFCollection); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } /// /// Database Context /// protected readonly RMuseumDbContext _context; - /// /// Background Task Queue Instance /// protected readonly IBackgroundTaskQueue _backgroundTaskQueue; - /// /// image file service /// protected readonly IImageFileService _imageFileService; - /// /// Configuration /// protected IConfiguration Configuration { get; } - - /// /// constructor /// From 8d25cb67de6321cfd9054455a2e04a0199f866a4 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 22 Jul 2023 17:01:36 +0330 Subject: [PATCH 4/8] #399 pdf service methods --- RMuseum/RMuseum.xml | 47 +++++++++++++++++- RMuseum/Services/IPDFLibraryService.cs | 49 ++++++++++++++++++- .../Implementation/PDFLibraryService.cs | 12 ++++- 3 files changed, 104 insertions(+), 4 deletions(-) diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 6c780602..6684162c 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -17407,11 +17407,12 @@ طول تصویر بندانگشتی - + get pdf book by id + @@ -18260,6 +18261,50 @@ PDF Library Services + + + get pdf book by id + + + + + + + + add author + + + + + + + get author by id + + + + + + + get authors + + + + + + + + add book + + + + + + + add multi volume pdf collection + + + + start importing local pdf file diff --git a/RMuseum/Services/IPDFLibraryService.cs b/RMuseum/Services/IPDFLibraryService.cs index 05e70a30..c98bbf36 100644 --- a/RMuseum/Services/IPDFLibraryService.cs +++ b/RMuseum/Services/IPDFLibraryService.cs @@ -1,5 +1,8 @@ -using RMuseum.Models.PDFLibrary.ViewModels; +using RMuseum.Models.Artifact; +using RMuseum.Models.PDFLibrary; +using RMuseum.Models.PDFLibrary.ViewModels; using RSecurityBackend.Models.Generic; +using System.Threading.Tasks; namespace RMuseum.Services { @@ -8,6 +11,50 @@ namespace RMuseum.Services /// public interface IPDFLibraryService { + /// + /// get pdf book by id + /// + /// + /// + /// + Task> GetPDFBookByIdAsync(int id, PublishStatus[] statusArray); + + /// + /// add author + /// + /// + /// + Task> AddAuthorAsync(Author author); + + /// + /// get author by id + /// + /// + /// + Task> GetAuthorByIdAsync(int id); + + /// + /// get authors + /// + /// + /// + /// + Task> GetAuthorsAsync(PagingParameterModel paging, string authorName); + + /// + /// add book + /// + /// + /// + Task> AddBookAsync(Book book); + + /// + /// add multi volume pdf collection + /// + /// + /// + Task> AddMultiVolumePDFCollection(MultiVolumePDFCollection multiVolumePDFCollection); + /// /// start importing local pdf file /// diff --git a/RMuseum/Services/Implementation/PDFLibraryService.cs b/RMuseum/Services/Implementation/PDFLibraryService.cs index b9b4c587..a8852b72 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using RMuseum.DbContext; +using RMuseum.Models.Artifact; using RMuseum.Models.PDFLibrary; using RMuseum.Models.PDFLibrary.ViewModels; using RSecurityBackend.Models.Generic; @@ -22,8 +23,9 @@ namespace RMuseum.Services.Implementation /// get pdf book by id /// /// + /// /// - public async Task> GetPDFBookByIdAsync(int id) + public async Task> GetPDFBookByIdAsync(int id, PublishStatus[] statusArray) { try { @@ -33,7 +35,7 @@ namespace RMuseum.Services.Implementation .Include(b => b.Contributers) .Include(b => b.Tags) .Include(b => b.Pages) - .Where(b => b.Id == id) + .Where(b => statusArray.Contains(b.Status) &&b.Id == id) .SingleOrDefaultAsync(); return new RServiceResult(pdfBook); @@ -169,6 +171,7 @@ namespace RMuseum.Services.Implementation ); return new RServiceResult(true); } + /// /// add author /// @@ -187,6 +190,7 @@ namespace RMuseum.Services.Implementation return new RServiceResult(null, exp.ToString()); } } + /// /// get author by id /// @@ -203,6 +207,7 @@ namespace RMuseum.Services.Implementation return new RServiceResult(null, exp.ToString()); } } + /// /// get authors /// @@ -219,6 +224,7 @@ namespace RMuseum.Services.Implementation await QueryablePaginator.Paginate(source, paging); return new RServiceResult<(PaginationMetadata PagingMeta, Author[] Authors)>(paginatedResult); } + /// /// add book /// @@ -237,6 +243,7 @@ namespace RMuseum.Services.Implementation return new RServiceResult(null, exp.ToString()); } } + /// /// add multi volume pdf collection /// @@ -255,6 +262,7 @@ namespace RMuseum.Services.Implementation return new RServiceResult(null, exp.ToString()); } } + /// /// Database Context /// From f8b5d125c1701e13683139b851d096e31476154c Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 22 Jul 2023 17:28:28 +0330 Subject: [PATCH 5/8] #399 PDFLibraryController --- RMuseum/Controllers/PDFLibraryController.cs | 24 +++++++++++++++++++ .../Auth/Memory/RMuseumSecurableItem.cs | 14 +++++++++++ RMuseum/RMuseum.xml | 16 +++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 RMuseum/Controllers/PDFLibraryController.cs diff --git a/RMuseum/Controllers/PDFLibraryController.cs b/RMuseum/Controllers/PDFLibraryController.cs new file mode 100644 index 00000000..5037b9f9 --- /dev/null +++ b/RMuseum/Controllers/PDFLibraryController.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Mvc; +using RMuseum.Services; + +namespace RMuseum.Controllers +{ + [Produces("application/json")] + [Route("api/pdf")] + public class PDFLibraryController : Controller + { + /// + /// PDF Service + /// + protected readonly IPDFLibraryService _pdfService; + + /// + /// constructor + /// + /// + public PDFLibraryController(IPDFLibraryService pdfService) + { + _pdfService = pdfService; + } + } +} diff --git a/RMuseum/Models/Auth/Memory/RMuseumSecurableItem.cs b/RMuseum/Models/Auth/Memory/RMuseumSecurableItem.cs index d1c8e9cc..7d2783f7 100644 --- a/RMuseum/Models/Auth/Memory/RMuseumSecurableItem.cs +++ b/RMuseum/Models/Auth/Memory/RMuseumSecurableItem.cs @@ -108,6 +108,11 @@ namespace RMuseum.Models.Auth.Memory /// public const string ModeratePoetPhotos = "photos"; + /// + /// pdf + /// + public const string PDFLibraryEntityShortName = "pdf"; + /// /// list of forms and their permissions /// @@ -194,6 +199,15 @@ namespace RMuseum.Models.Auth.Memory new SecurableItemOperation(ModerateOperationShortName, "مدیریت", false), } }, + new SecurableItem() + { + ShortName = PDFLibraryEntityShortName, + Description = "نسک‌بان", + Operations = new SecurableItemOperation[] + { + new SecurableItemOperation(ModifyOperationShortName, "ویرایش محتوا", false), + } + }, diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 6684162c..1d9ff8da 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2343,6 +2343,17 @@ + + + PDF Service + + + + + constructor + + + returns paginated published recitations (if poetId or catId is non-zero its ordered by poemId ascending if not it is ordered by publish date descending) @@ -4375,6 +4386,11 @@ photos + + + pdf + + list of forms and their permissions From 78ab602e50138bbc7bf009812060a166378c06dd Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 22 Jul 2023 17:57:33 +0330 Subject: [PATCH 6/8] #399 AddMultiVolumePDFCollection --- RMuseum/Controllers/PDFLibraryController.cs | 61 ++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/RMuseum/Controllers/PDFLibraryController.cs b/RMuseum/Controllers/PDFLibraryController.cs index 5037b9f9..528d7be2 100644 --- a/RMuseum/Controllers/PDFLibraryController.cs +++ b/RMuseum/Controllers/PDFLibraryController.cs @@ -1,5 +1,11 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using RMuseum.Models.Auth.Memory; +using RMuseum.Models.PDFLibrary; using RMuseum.Services; +using RSecurityBackend.Models.Auth.Memory; +using System.Net; +using System.Threading.Tasks; namespace RMuseum.Controllers { @@ -7,6 +13,59 @@ namespace RMuseum.Controllers [Route("api/pdf")] public class PDFLibraryController : Controller { + /// + /// add a new book + /// + /// + /// + + [HttpPost("book")] + [Authorize(Policy = RMuseumSecurableItem.PDFLibraryEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(Book))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task AddBookAsync([FromBody] Book book) + { + var res = await _pdfService.AddBookAsync(book); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(res.Result); + } + + /// + /// add a new author + /// + /// + /// + + [HttpPost("author")] + [Authorize(Policy = RMuseumSecurableItem.PDFLibraryEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(Author))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task AddAuthorAsync([FromBody] Author author) + { + var res = await _pdfService.AddAuthorAsync(author); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(res.Result); + } + + /// + /// add a new mukti volume pdf collection + /// + /// + /// + [HttpPost("volumes")] + [Authorize(Policy = RMuseumSecurableItem.PDFLibraryEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(MultiVolumePDFCollection))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task AddMultiVolumePDFCollection([FromBody] MultiVolumePDFCollection volumes) + { + var res = await _pdfService.AddMultiVolumePDFCollection(volumes); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(res.Result); + } + /// /// PDF Service /// From 134fccd22a48a7951bba701a34e8d9ae215ff0c3 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 22 Jul 2023 18:00:44 +0330 Subject: [PATCH 7/8] #399 StartImportingLocalPDF --- RMuseum/Controllers/PDFLibraryController.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/RMuseum/Controllers/PDFLibraryController.cs b/RMuseum/Controllers/PDFLibraryController.cs index 528d7be2..148d5e1c 100644 --- a/RMuseum/Controllers/PDFLibraryController.cs +++ b/RMuseum/Controllers/PDFLibraryController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using RMuseum.Models.Auth.Memory; using RMuseum.Models.PDFLibrary; +using RMuseum.Models.PDFLibrary.ViewModels; using RMuseum.Services; using RSecurityBackend.Models.Auth.Memory; using System.Net; @@ -13,6 +14,23 @@ namespace RMuseum.Controllers [Route("api/pdf")] public class PDFLibraryController : Controller { + /// + /// start importing a local pdf file + /// + /// + /// + [HttpPost] + [Authorize(Policy = RMuseumSecurableItem.PDFLibraryEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public IActionResult StartImportingLocalPDF([FromBody] NewPDFBookViewModel model) + { + var res = _pdfService.StartImportingLocalPDF(model); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(); + } + /// /// add a new book /// From c4f7fa1d9528216ace09ab934c5f16e2e6979cb2 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Sat, 22 Jul 2023 18:16:28 +0330 Subject: [PATCH 8/8] #399 basic pdf library controller --- RMuseum/Controllers/PDFLibraryController.cs | 138 +++++++++++++++++- .../Auth/Memory/RMuseumSecurableItem.cs | 1 + RMuseum/RMuseum.xml | 50 ++++++- 3 files changed, 187 insertions(+), 2 deletions(-) diff --git a/RMuseum/Controllers/PDFLibraryController.cs b/RMuseum/Controllers/PDFLibraryController.cs index 148d5e1c..add8596a 100644 --- a/RMuseum/Controllers/PDFLibraryController.cs +++ b/RMuseum/Controllers/PDFLibraryController.cs @@ -1,12 +1,18 @@ using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using RMuseum.Models.Artifact; using RMuseum.Models.Auth.Memory; using RMuseum.Models.PDFLibrary; using RMuseum.Models.PDFLibrary.ViewModels; using RMuseum.Services; using RSecurityBackend.Models.Auth.Memory; +using RSecurityBackend.Models.Generic; +using System.Linq; +using System; using System.Net; using System.Threading.Tasks; +using RSecurityBackend.Services; namespace RMuseum.Controllers { @@ -31,6 +37,129 @@ namespace RMuseum.Controllers return Ok(); } + /// + /// secure get a pdf book + /// + /// + /// + [HttpGet("secure/{id}")] + [Authorize] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PDFBook))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + public async Task GetUserVisible(int id) + { + RServiceResult v = await _GetUserVisibleArtifactStatusSet + ( + new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value), + new Guid(User.Claims.FirstOrDefault(c => c.Type == "SessionId").Value) + ); + if (!string.IsNullOrEmpty(v.ExceptionString)) + return BadRequest(v.ExceptionString); + PublishStatus[] visibleItems = v.Result; + RServiceResult bookRes = null; + if (visibleItems.Length == 1 && visibleItems[0] == PublishStatus.Published) + { + bookRes = await _pdfService.GetPDFBookByIdAsync(id, new PublishStatus[] { PublishStatus.Published }); + if (!string.IsNullOrEmpty(bookRes.ExceptionString)) + { + return BadRequest(bookRes.ExceptionString); + } + if (bookRes.Result == null) + return NotFound(); + } + if (bookRes == null) + { + bookRes = await _pdfService.GetPDFBookByIdAsync(id, visibleItems); + } + + if (!string.IsNullOrEmpty(bookRes.ExceptionString)) + { + return BadRequest(bookRes.ExceptionString); + } + if (bookRes.Result == null) + return NotFound(); + + Response.GetTypedHeaders().LastModified = bookRes.Result.LastModified; + + var requestHeaders = Request.GetTypedHeaders(); + if (requestHeaders.IfModifiedSince.HasValue && + requestHeaders.IfModifiedSince.Value >= bookRes.Result.LastModified) + { + return StatusCode(StatusCodes.Status304NotModified); + } + + + return Ok(bookRes.Result); + } + + private async Task> _GetUserVisibleArtifactStatusSet(Guid loggedOnUserId, Guid sessionId) + { + RServiceResult + canView = + await _userPermissionChecker.Check + ( + loggedOnUserId, + sessionId, + RMuseumSecurableItem.PDFLibraryEntityShortName, + RMuseumSecurableItem.ViewDraftOperationShortName + ); + if (!string.IsNullOrEmpty(canView.ExceptionString)) + return new RServiceResult(null, canView.ExceptionString); + + PublishStatus[] visibleItems = + canView.Result + ? + new PublishStatus[] + { + PublishStatus.Published, + PublishStatus.Restricted, + PublishStatus.Draft, + PublishStatus.Awaiting + } + : + new PublishStatus[] + { + PublishStatus.Published + }; + + return new RServiceResult(visibleItems); + } + + /// + /// get published PDF Book by id + /// + /// + /// + [HttpGet("{id}")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PDFBook))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + public async Task GetPDFBookByIdAsync(int id) + { + var bookRes = await _pdfService.GetPDFBookByIdAsync(id, new PublishStatus[] { PublishStatus.Published }); + + if (!string.IsNullOrEmpty(bookRes.ExceptionString)) + { + return BadRequest(bookRes.ExceptionString); + } + if (bookRes.Result == null) + return NotFound(); + + Response.GetTypedHeaders().LastModified = bookRes.Result.LastModified; + + var requestHeaders = Request.GetTypedHeaders(); + if (requestHeaders.IfModifiedSince.HasValue && + requestHeaders.IfModifiedSince.Value >= bookRes.Result.LastModified) + { + return StatusCode(StatusCodes.Status304NotModified); + } + + + return Ok(bookRes.Result); + } + /// /// add a new book /// @@ -89,13 +218,20 @@ namespace RMuseum.Controllers /// protected readonly IPDFLibraryService _pdfService; + /// + /// IUserPermissionChecker instance + /// + protected IUserPermissionChecker _userPermissionChecker; + /// /// constructor /// /// - public PDFLibraryController(IPDFLibraryService pdfService) + /// + public PDFLibraryController(IPDFLibraryService pdfService, IUserPermissionChecker userPermissionChecker) { _pdfService = pdfService; + _userPermissionChecker = userPermissionChecker; } } } diff --git a/RMuseum/Models/Auth/Memory/RMuseumSecurableItem.cs b/RMuseum/Models/Auth/Memory/RMuseumSecurableItem.cs index 7d2783f7..4b104c18 100644 --- a/RMuseum/Models/Auth/Memory/RMuseumSecurableItem.cs +++ b/RMuseum/Models/Auth/Memory/RMuseumSecurableItem.cs @@ -206,6 +206,7 @@ namespace RMuseum.Models.Auth.Memory Operations = new SecurableItemOperation[] { new SecurableItemOperation(ModifyOperationShortName, "ویرایش محتوا", false), + new SecurableItemOperation(ViewDraftOperationShortName, "مشاهدهٔ پیش‌نویس‌ها", false), } }, diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 1d9ff8da..36ff4a55 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2343,16 +2343,64 @@ + + + start importing a local pdf file + + + + + + + secure get a pdf book + + + + + + + get published PDF Book by id + + + + + + + add a new book + + + + + + + add a new author + + + + + + + add a new mukti volume pdf collection + + + + PDF Service - + + + IUserPermissionChecker instance + + + constructor +