From a20fb0cd8349aaa9b81386cf04df83ccb961daab Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Mon, 24 Jul 2023 17:44:54 +0330 Subject: [PATCH] #400 pdf book tagging api --- RMuseum/Controllers/PDFLibraryController.cs | 72 +++++++ RMuseum/RMuseum.xml | 78 ++++++++ RMuseum/Services/IPDFLibraryService.cs | 26 +++ .../Implementation/PDFLibraryService.cs | 181 ++++++++++++++++-- 4 files changed, 343 insertions(+), 14 deletions(-) diff --git a/RMuseum/Controllers/PDFLibraryController.cs b/RMuseum/Controllers/PDFLibraryController.cs index f6a7c24a..3a880946 100644 --- a/RMuseum/Controllers/PDFLibraryController.cs +++ b/RMuseum/Controllers/PDFLibraryController.cs @@ -347,6 +347,78 @@ namespace RMuseum.Controllers } return Ok(res.Result); } + + /// + /// add new tag value to artifact + /// + /// + /// only name is processed + /// + [HttpPost("tagvalue/{pdfBookId}")] + [Authorize(Policy = RMuseumSecurableItem.ArtifactEntityShortName + ":" + RMuseumSecurableItem.EditTagValueOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(RTagValue))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + public async Task TagPDFBookAsync(int pdfBookId, [FromBody] RTag tag) + { + RServiceResult res = await _pdfService.TagPDFBookAsync(pdfBookId, tag); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(res.Result); + } + + /// + /// edit pdf book attribute value + /// + /// + /// editable fields are limited + /// + /// + /// + /// apply on all same value tags + /// + [HttpPut("tagvalue/{pdfBookId}/{global=true}")] + [Authorize(Policy = RMuseumSecurableItem.ArtifactEntityShortName + ":" + RMuseumSecurableItem.EditTagValueOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + public async Task EditPDFBookTagValueAsync(int pdfBookId, bool global, [FromBody] RTagValue tagvalue) + { + + RServiceResult itemInfo = await _pdfService.EditPDFBookTagValueAsync(pdfBookId, tagvalue, global); + if (!string.IsNullOrEmpty(itemInfo.ExceptionString)) + { + return BadRequest(itemInfo.ExceptionString); + } + + if (itemInfo == null) + { + return NotFound(); + } + + return Ok(); ; + } + + /// + /// remove tag from pdf book + /// + /// + /// + /// + [HttpDelete("tagvalue/{pdfBookId}/{tagValueId}")] + [Authorize(Policy = RMuseumSecurableItem.ArtifactEntityShortName + ":" + RMuseumSecurableItem.EditTagValueOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.Forbidden)] + public async Task UnTagPDFBookAsync(int pdfBookId, Guid tagValueId) + { + RServiceResult res = await _pdfService.UnTagPDFBookAsync(pdfBookId, tagValueId); + if (!string.IsNullOrEmpty(res.ExceptionString)) + { + return BadRequest(res.ExceptionString); + } + return Ok(res.Result); + } + /// /// get tagged publish pdfbooks (including CoverImage info but not pages or tagibutes info) /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 15eb8c81..f30235d3 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2403,6 +2403,34 @@ + + + add new tag value to artifact + + + only name is processed + + + + + edit pdf book attribute value + + + editable fields are limited + + + + apply on all same value tags + + + + + remove tag from pdf book + + + + + get tagged publish pdfbooks (including CoverImage info but not pages or tagibutes info) @@ -17591,6 +17619,31 @@ + + + add pdf book tag value + + + + + + + + remove pdf book tag value + + + + + + + + edit pdf book tag value + + + + apply on all same value tags + + add author @@ -18453,6 +18506,31 @@ + + + add pdf book tag value + + + + + + + + remove pdf book tag value + + + + + + + + edit pdf book tag value + + + + apply on all same value tags + + get tagged publish pdfbooks (including CoverImage info but not pages or tagibutes info) diff --git a/RMuseum/Services/IPDFLibraryService.cs b/RMuseum/Services/IPDFLibraryService.cs index c0354c78..ccec9f78 100644 --- a/RMuseum/Services/IPDFLibraryService.cs +++ b/RMuseum/Services/IPDFLibraryService.cs @@ -2,6 +2,7 @@ using RMuseum.Models.PDFLibrary; using RMuseum.Models.PDFLibrary.ViewModels; using RSecurityBackend.Models.Generic; +using System; using System.Threading.Tasks; namespace RMuseum.Services @@ -34,6 +35,31 @@ namespace RMuseum.Services /// Task> RemovePDFBookAsync(int pdfBookId); + /// + /// add pdf book tag value + /// + /// + /// + /// + Task> TagPDFBookAsync(int pdfBookId, RTag rTag); + + /// + /// remove pdf book tag value + /// + /// + /// + /// + Task> UnTagPDFBookAsync(int pdfBookId, Guid tagValueId); + + /// + /// edit pdf book tag value + /// + /// + /// + /// apply on all same value tags + /// + Task> EditPDFBookTagValueAsync(int pdfBookId, RTagValue edited, bool global); + /// /// get tagged publish pdfbooks (including CoverImage info but not pages or tagibutes info) /// diff --git a/RMuseum/Services/Implementation/PDFLibraryService.cs b/RMuseum/Services/Implementation/PDFLibraryService.cs index c92f6786..bd13fa6a 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService.cs @@ -361,7 +361,7 @@ namespace RMuseum.Services.Implementation return new RServiceResult(false, "Can not delete published pdf book"); } - + string artifactFolder = Path.Combine(_imageFileService.ImageStoragePath, record.StorageFolderName); @@ -403,26 +403,179 @@ namespace RMuseum.Services.Implementation /// public async Task> GetPDFBookByTagValueAsync(string tagUrl, string valueUrl, PublishStatus[] statusArray) { - RTag tag = + try + { + RTag tag = await _context.Tags .Where(a => a.FriendlyUrl == tagUrl) .SingleOrDefaultAsync(); - if (tag == null) - return new RServiceResult(new PDFBook[] { }); + if (tag == null) + return new RServiceResult(new PDFBook[] { }); - PDFBook[] taggedItems = - await _context.PDFBooks.Include(a => a.Tags) - .Include(a => a.CoverImage) - .Where(a => statusArray.Contains(a.Status) && a.Tags != null && a.Tags.Any(v => v.RTagId == tag.Id && v.FriendlyUrl == valueUrl)) - .OrderByDescending(t => t.DateTime) - .AsNoTracking() - .ToArrayAsync(); + PDFBook[] taggedItems = + await _context.PDFBooks.Include(a => a.Tags) + .Include(a => a.CoverImage) + .Where(a => statusArray.Contains(a.Status) && a.Tags != null && a.Tags.Any(v => v.RTagId == tag.Id && v.FriendlyUrl == valueUrl)) + .OrderByDescending(t => t.DateTime) + .AsNoTracking() + .ToArrayAsync(); - foreach (PDFBook taggedItem in taggedItems) - taggedItem.Tags = null; + foreach (PDFBook taggedItem in taggedItems) + taggedItem.Tags = null; - return new RServiceResult(taggedItems); + return new RServiceResult(taggedItems); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + + } + + /// + /// add pdf book tag value + /// + /// + /// + /// + public async Task> TagPDFBookAsync(int pdfBookId, RTag rTag) + { + try + { + RTag type = await _context.Tags.Where(a => a.Id == rTag.Id).SingleOrDefaultAsync(); + + PDFBook item = await _context.PDFBooks.Include(i => i.Tags).Where(i => i.Id == pdfBookId).SingleOrDefaultAsync(); + + int order = item.Tags.Where(t => t.RTagId == type.Id).Count() == 0 ? 1 : item.Tags.Where(t => t.RTagId == type.Id).OrderByDescending(t => t.Order).FirstOrDefault().Order + 1; + + RTagValue tag = + new RTagValue() + { + Order = order, + Value = "", + ValueInEnglish = "", + ValueSupplement = "", + RTag = type, + Status = PublishStatus.Published + }; + + item.Tags.Add(tag); + item.LastModified = DateTime.Now; + _context.Update(item); + await _context.SaveChangesAsync(); + + return new RServiceResult(tag); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + + /// + /// remove pdf book tag value + /// + /// + /// + /// + public async Task> UnTagPDFBookAsync(int pdfBookId, Guid tagValueId) + { + try + { + PDFBook item = await _context.PDFBooks.Include(i => i.Tags).Where(i => i.Id == pdfBookId).SingleOrDefaultAsync(); + item.Tags.Remove(item.Tags.Where(t => t.Id == tagValueId).SingleOrDefault()); + item.LastModified = DateTime.Now; + _context.Update(item); + await _context.SaveChangesAsync(); + + return new RServiceResult(true); + } + catch (Exception exp) + { + return new RServiceResult(false, exp.ToString()); + } + } + + + /// + /// edit pdf book tag value + /// + /// + /// + /// apply on all same value tags + /// + public async Task> EditPDFBookTagValueAsync(int pdfBookId, RTagValue edited, bool global) + { + if (string.IsNullOrEmpty(edited.Value)) + { + return new RServiceResult(null, "Value could not be empty."); + } + + PDFBook pdfBook = + await _context.PDFBooks + .Include(a => a.Tags) + .Where(a => a.Id == pdfBookId) + .SingleOrDefaultAsync(); + if (pdfBook == null) + return new RServiceResult(null); + + RTagValue tag = + pdfBook.Tags.Where(a => a.Id == edited.Id) + .SingleOrDefault(); + + + if (tag != null) + { + tag.Order = edited.Order; + tag.ValueSupplement = edited.ValueSupplement; + _context.Update(tag); + + if (global) + { + RTagValue[] sameValueTags = await _context.TagValues.Where(v => v.Value == tag.Value && v.RTagId == tag.RTagId).ToArrayAsync(); + foreach (RTagValue sameValueTag in sameValueTags) + { + sameValueTag.Value = edited.Value; + sameValueTag.ValueInEnglish = edited.ValueInEnglish; + sameValueTag.Status = edited.Status; + sameValueTag.FriendlyUrl = edited.FriendlyUrl; + _context.Update(sameValueTag); + + RArtifactMasterRecord correspondingArtifact = + await _context.Artifacts.Include(a => a.Tags).Where(a => a.Tags.Contains(sameValueTag)).SingleOrDefaultAsync(); + if (correspondingArtifact != null) + { + correspondingArtifact.LastModified = DateTime.Now; + _context.Update(correspondingArtifact); + } + + RArtifactItemRecord correspondingItem = + await _context.Items.Include(a => a.Tags).Where(a => a.Tags.Contains(sameValueTag)).SingleOrDefaultAsync(); + if (correspondingItem != null) + { + correspondingItem.LastModified = DateTime.Now; + _context.Update(correspondingItem); + } + + } + await _context.SaveChangesAsync(); + } + else + { + tag.Value = edited.Value; + tag.ValueInEnglish = edited.ValueInEnglish; + tag.Order = edited.Order; + tag.FriendlyUrl = edited.FriendlyUrl; + tag.Status = edited.Status; + tag.ValueSupplement = edited.ValueSupplement; + _context.Update(tag); + pdfBook.LastModified = DateTime.Now; + _context.Update(pdfBook); + await _context.SaveChangesAsync(); + } + } + return new RServiceResult(tag); } ///