From e7d50a52b0b78c7de587b3160379381385cdaf84 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 10 Jan 2025 11:21:50 +0330 Subject: [PATCH] ganjoor museum: add an item to a book #457 --- RMuseum/Services/IArtifactService.cs | 9 +++ .../Implementation/ArtifactService.cs | 64 +++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/RMuseum/Services/IArtifactService.cs b/RMuseum/Services/IArtifactService.cs index 372457d3..20df466b 100644 --- a/RMuseum/Services/IArtifactService.cs +++ b/RMuseum/Services/IArtifactService.cs @@ -630,5 +630,14 @@ namespace RMuseum.Services /// Task OpenAIStartCreatingImagesForPoemsOfflineAsync(); + /// + /// add item to an artifact + /// + /// + /// + /// + /// + Task> AddItemToArtifactAsync(Guid artifactId, IFormFile file, Stream jpegStream); + } } diff --git a/RMuseum/Services/Implementation/ArtifactService.cs b/RMuseum/Services/Implementation/ArtifactService.cs index 2baafcbf..b44ecfa5 100644 --- a/RMuseum/Services/Implementation/ArtifactService.cs +++ b/RMuseum/Services/Implementation/ArtifactService.cs @@ -28,6 +28,9 @@ using System.Threading.Tasks; using DNTPersianUtils.Core; using FluentFTP; using RSecurityBackend.Models.Notification; +using RMuseum.Models.PDFLibrary; +using Microsoft.Identity.Client; +using Org.BouncyCastle.Utilities.Collections; namespace RMuseum.Services.Implementation { @@ -3631,6 +3634,67 @@ namespace RMuseum.Services.Implementation return new RServiceResult<(PaginationMetadata PagingMeta, RArtifactItemRecordViewModel[] Items)>((paginatedResult.PagingMeta, viewModels.ToArray())); } + /// + /// add item to an artifact + /// + /// + /// + /// + /// + public async Task> AddItemToArtifactAsync(Guid artifactId, IFormFile file, Stream jpegStream) + { + try + { + RArtifactMasterRecord book = await _context.Artifacts.Where(b => b.Id == artifactId).SingleAsync(); + RArtifactItemRecord firstItem = await _context.Items.AsNoTracking().Where(i => i.RArtifactMasterRecordId == artifactId).FirstOrDefaultAsync(); + int zeroPads = 4; + if(firstItem != null) + { + zeroPads = firstItem.FriendlyUrl.Length - 1; //removing p at the start + } + int order = 1 + await _context.Items.Where(i => i.RArtifactMasterRecordId == book.Id).CountAsync(); + + RArtifactItemRecord page = new RArtifactItemRecord() + { + Name = $"تصویر {order}", + NameInEnglish = $"Image {order} of {book.NameInEnglish}", + Description = "", + DescriptionInEnglish = "", + Order = order, + FriendlyUrl = $"p{$"{order}".PadLeft(zeroPads, '0')}", + LastModified = DateTime.Now, + RArtifactMasterRecordId = book.Id, + }; + + RServiceResult picture = await _pictureFileService.Add(page.Name, page.Description, 1, file, "", jpegStream, $"{order}".PadLeft(zeroPads, '0') + ".jpg", book.FriendlyUrl); + if (picture.Result == null) + { + return new RServiceResult(null, ($"_pictureFileService.Add : {picture.ExceptionString}")); + } + page.Images = [picture.Result]; + page.CoverImageIndex = 0; + _context.Add(page); + await _context.SaveChangesAsync(); + + book.ItemCount = order; + _context.Update(book); + await _context.SaveChangesAsync(); + + var resUpload = await _UploadArtifactPageToExternalServer(page, _context, book.FriendlyUrl, false); + if (resUpload.Result != true) + { + return new RServiceResult(null, resUpload.ExceptionString); + } + + return new RServiceResult(page); + + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + ///