diff --git a/RMuseum/Controllers/PDFLibraryController.cs b/RMuseum/Controllers/PDFLibraryController.cs index 6f89ad5b..d0e08756 100644 --- a/RMuseum/Controllers/PDFLibraryController.cs +++ b/RMuseum/Controllers/PDFLibraryController.cs @@ -1,7 +1,6 @@ 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; @@ -16,6 +15,7 @@ using RSecurityBackend.Services; using RMuseum.Services.Implementation; using System.Collections.Generic; using Newtonsoft.Json; +using RMuseum.Models.Artifact; namespace RMuseum.Controllers { @@ -62,20 +62,54 @@ namespace RMuseum.Controllers } /// - /// start importing a local pdf file + /// get all pdf books visible by user (including CoverImage info but not items info) /// - /// + /// /// - [HttpPost] - [Authorize(Policy = RMuseumSecurableItem.PDFLibraryEntityShortName + ":" + SecurableItem.AddOperationShortName)] - [ProducesResponseType((int)HttpStatusCode.OK)] + [HttpGet] + [Route("secure")] + [Authorize] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task StartImportingLocalPDFAsync([FromBody] NewPDFBookViewModel model) + public async Task GetUserVisible([FromQuery] PagingParameterModel paging) { - var res = await _pdfService.StartImportingLocalPDFAsync(model); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return BadRequest(res.ExceptionString); - return Ok(); + RServiceResult v = await _GetUserVisiblePDFBooksStatusSet + ( + 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; + + if (visibleItems.Length == 1 && visibleItems[0] == PublishStatus.Published) //Caching + { + return await GetAllPDFBooks(paging); + } + + RServiceResult<(PaginationMetadata PagingMeta, PDFBook[] Books)> itemsInfo = await _pdfService.GetAllPDFBooks(paging, visibleItems); + if (!string.IsNullOrEmpty(itemsInfo.ExceptionString)) + { + return BadRequest(itemsInfo.ExceptionString); + } + + if (itemsInfo.Result.Books.Count() > 0) + { + DateTime lastModification = itemsInfo.Result.Books.Max(i => i.LastModified); + Response.GetTypedHeaders().LastModified = lastModification; + + var requestHeaders = Request.GetTypedHeaders(); + if (requestHeaders.IfModifiedSince.HasValue && + requestHeaders.IfModifiedSince.Value >= lastModification) + { + return StatusCode(StatusCodes.Status304NotModified); + } + } + + // Paging Header + HttpContext.Response.Headers.Add("paging-headers", JsonConvert.SerializeObject(itemsInfo.Result.PagingMeta)); + + return Ok(itemsInfo.Result.Books); } /// @@ -90,7 +124,7 @@ namespace RMuseum.Controllers [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetUserVisible(int id) { - RServiceResult v = await _GetUserVisibleArtifactStatusSet + RServiceResult v = await _GetUserVisiblePDFBooksStatusSet ( new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value), new Guid(User.Claims.FirstOrDefault(c => c.Type == "SessionId").Value) @@ -134,7 +168,7 @@ namespace RMuseum.Controllers return Ok(bookRes.Result); } - private async Task> _GetUserVisibleArtifactStatusSet(Guid loggedOnUserId, Guid sessionId) + private async Task> _GetUserVisiblePDFBooksStatusSet(Guid loggedOnUserId, Guid sessionId) { RServiceResult canView = @@ -201,6 +235,25 @@ namespace RMuseum.Controllers return Ok(bookRes.Result); } + + /// + /// start importing a local pdf file + /// + /// + /// + [HttpPost] + [Authorize(Policy = RMuseumSecurableItem.PDFLibraryEntityShortName + ":" + SecurableItem.AddOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task StartImportingLocalPDFAsync([FromBody] NewPDFBookViewModel model) + { + var res = await _pdfService.StartImportingLocalPDFAsync(model); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(); + } + + /// /// edit pdf book master record (user should have additional permissions pdf:awaiting and pdf:publish to change status of pdf book) /// @@ -275,6 +328,26 @@ namespace RMuseum.Controllers return Ok(); } + /// + /// remove unpublished pdf book + /// + /// + /// + [HttpDelete("{bookId}")] + [Authorize(Policy = RMuseumSecurableItem.PDFLibraryEntityShortName + ":" + SecurableItem.DeleteOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.Forbidden)] + public async Task RemovePDFBookAsync(int bookId) + { + RServiceResult res = await _pdfService.RemovePDFBookAsync(bookId); + if (!string.IsNullOrEmpty(res.ExceptionString)) + { + return BadRequest(res.ExceptionString); + } + return Ok(res.Result); + } + /// /// add a new book /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 694a4e20..cb62b4ec 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2350,11 +2350,11 @@ - + - start importing a local pdf file + get all pdf books visible by user (including CoverImage info but not items info) - + @@ -2371,6 +2371,13 @@ + + + start importing a local pdf file + + + + edit pdf book master record (user should have additional permissions pdf:awaiting and pdf:publish to change status of pdf book) @@ -2389,6 +2396,13 @@ + + + remove unpublished pdf book + + + + add a new book @@ -17553,6 +17567,13 @@ + + + an incomplete prototype for removing PDF books + + + + add author @@ -18408,6 +18429,13 @@ + + + an incomplete prototype for removing PDF books + + + + add author diff --git a/RMuseum/Services/IPDFLibraryService.cs b/RMuseum/Services/IPDFLibraryService.cs index 21b03739..ad6a4b12 100644 --- a/RMuseum/Services/IPDFLibraryService.cs +++ b/RMuseum/Services/IPDFLibraryService.cs @@ -27,6 +27,13 @@ namespace RMuseum.Services /// Task> GetAllPDFBooks(PagingParameterModel paging, PublishStatus[] statusArray); + /// + /// an incomplete prototype for removing PDF books + /// + /// + /// + Task> RemovePDFBookAsync(int pdfBookId); + /// /// add author /// diff --git a/RMuseum/Services/Implementation/PDFLibraryService.cs b/RMuseum/Services/Implementation/PDFLibraryService.cs index cc3db26a..d57cb6f8 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService.cs @@ -337,6 +337,63 @@ namespace RMuseum.Services.Implementation } } + /// + /// an incomplete prototype for removing PDF books + /// + /// + /// + public async Task> RemovePDFBookAsync(int pdfBookId) + { + try + { + PDFBook record = await _context.PDFBooks + .Include(a => a.Pages).ThenInclude(i => i.ThumbnailImage) + .Include(a => a.Pages).ThenInclude(i => i.Tags) + .Include(a => a.Tags) + .Where(a => a.Id == pdfBookId) + .SingleOrDefaultAsync(); + if (record == null) + { + return new RServiceResult(false, "PDFBook not found."); + } + if (record.Status == PublishStatus.Published) + { + return new RServiceResult(false, "Can not delete published pdf book"); + } + + + + string artifactFolder = Path.Combine(_imageFileService.ImageStoragePath, record.StorageFolderName); + + foreach (PDFPage pages in record.Pages) + { + _context.TagValues.RemoveRange(pages.Tags); + } + + _context.RemoveRange(record.Pages); + _context.TagValues.RemoveRange(record.Tags); + _context.Remove(record); + await _context.SaveChangesAsync(); + + if (!string.IsNullOrEmpty(artifactFolder)) + { + try + { + Directory.Delete(artifactFolder, true); + } + catch + { + //ignore errors + } + } + } + catch (Exception exp) + { + return new RServiceResult(false, exp.ToString()); + } + return new RServiceResult(true); + } + /// /// add author ///