From 48af2f3623518b8c5429b107b1bebb1d2c26a844 Mon Sep 17 00:00:00 2001 From: Vijay Uppu <913468@dadeschools.net> Date: Thu, 30 Nov 2023 20:00:09 -0500 Subject: [PATCH] added multi language support for doculink attachments --- .../MockData.cs | 3 + .../Db/DoculinkAttachments.cs | 1 + .../Models/DoculinkAttachments.cs | 1 + .../Models/ReqDoculink.cs | 1 + .../Providers/AzureBlobService.cs | 28 +++---- .../Providers/DoculinkProvider.cs | 79 ++++++------------- .../Providers/UploadService.cs | 40 ++++++---- 7 files changed, 68 insertions(+), 85 deletions(-) diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/MockData.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/MockData.cs index 92c10b1..6a0fab0 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/MockData.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/MockData.cs @@ -34,6 +34,7 @@ namespace DamageAssesment.Api.DocuLinks.Test docName = "", Path = "www.google.com", IsAttachments = false, + Language = "en", CustomOrder = 1 }); list.Add(new DocuLinks.Models.ResDoculink() @@ -140,6 +141,7 @@ namespace DamageAssesment.Api.DocuLinks.Test docName = "", Path = "www.google.com", IsAttachments = false, + Language = "en", CustomOrder = 1 }); return new Models.Doculink @@ -167,6 +169,7 @@ namespace DamageAssesment.Api.DocuLinks.Test docName = "", Path = "www.google.com", IsAttachments = false, + Language="en", CustomOrder = 1 }); List DocuLinks = new List(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Db/DoculinkAttachments.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Db/DoculinkAttachments.cs index b6eac5c..f277570 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Db/DoculinkAttachments.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Db/DoculinkAttachments.cs @@ -15,5 +15,6 @@ namespace DamageAssesment.Api.DocuLinks.Db public string Path { get; set; } public bool IsAttachments { get; set; } public int CustomOrder { get; set; } + public string Language { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/DoculinkAttachments.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/DoculinkAttachments.cs index ce87857..04a6970 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/DoculinkAttachments.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/DoculinkAttachments.cs @@ -8,6 +8,7 @@ namespace DamageAssesment.Api.DocuLinks.Models public string docName { get; set; } public string Path { get; set; } public bool IsAttachments { get; set; } + public string Language { get; set; } public int CustomOrder { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/ReqDoculink.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/ReqDoculink.cs index 0f2ea17..fbafc86 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/ReqDoculink.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/ReqDoculink.cs @@ -17,6 +17,7 @@ namespace DamageAssesment.Api.DocuLinks.Models public string? FileExtension { get; set; } public int CustomOrder { get; set; } public string url { get;set; } + public string Language { get; set; } public bool IsAttachments { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/AzureBlobService.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/AzureBlobService.cs index 9931de2..79b3ff6 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/AzureBlobService.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/AzureBlobService.cs @@ -15,7 +15,7 @@ using System.Threading.Tasks; namespace DamageAssesment.Api.DocuLinks.Providers { - public class AzureBlobService: IAzureBlobService + public class AzureBlobService : IAzureBlobService { BlobServiceClient _blobClient; BlobContainerClient _containerClient; @@ -32,7 +32,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers public async Task UploadDocument(int counter, ReqDoculink documentInfo) { Models.Doculink Documents = new Models.Doculink(); - List attachments = new List(); + List attachments = new List(); try { string path = "", UserfileName = ""; @@ -55,7 +55,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers } else path = item.url; - attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder }); + attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder, Language = item.Language }); } } Documents = new Models.Doculink() @@ -84,7 +84,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers { foreach (var item in document.doclinksAttachments) { - Movefile(item.Path); + Movefile(item.Path); } string path = "", UserfileName = ""; List attachments = new List(); @@ -104,7 +104,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers } else path = item.url; - attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder }); + attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder, Language = item.Language }); } Models.Doculink Documents = new Models.Doculink() { @@ -148,7 +148,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers { var list = movefilename.Split('.'); if (list.Length > 0) - list[list.Length - 1] = DateTime.Now.ToShortDateString().Replace("/", "_") +"_"+ DateTime.Now.ToShortTimeString().Replace("/", "_")+"." + list[list.Length - 1]; + list[list.Length - 1] = DateTime.Now.ToShortDateString().Replace("/", "_") + "_" + DateTime.Now.ToShortTimeString().Replace("/", "_") + "." + list[list.Length - 1]; return string.Join("_", list); } public void Movefile(string path) @@ -162,28 +162,28 @@ namespace DamageAssesment.Api.DocuLinks.Providers BlobClient sourceBlobClient = _containerClient.GetBlobClient(path); BlobClient destinationBlobClient = _containerClient.GetBlobClient(MovePath); // Start the copy operation from the source to the destination - destinationBlobClient.StartCopyFromUri(sourceBlobClient.Uri); + destinationBlobClient.StartCopyFromUri(sourceBlobClient.Uri); // Check if the copy operation completed successfully - WaitForCopyToComplete(destinationBlobClient); + WaitForCopyToComplete(destinationBlobClient); // Delete the source blob after a successful copy - sourceBlobClient.DeleteIfExists(); + sourceBlobClient.DeleteIfExists(); } } - catch(Exception ex) + catch (Exception ex) { } } - static void WaitForCopyToComplete(BlobClient blobClient) + static void WaitForCopyToComplete(BlobClient blobClient) { - BlobProperties properties = blobClient.GetProperties(); + BlobProperties properties = blobClient.GetProperties(); while (properties.CopyStatus == CopyStatus.Pending) { - Task.Delay(TimeSpan.FromSeconds(1)); - properties = blobClient.GetProperties(); + Task.Delay(TimeSpan.FromSeconds(1)); + properties = blobClient.GetProperties(); } } public void DeleteFile(string url) diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/DoculinkProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/DoculinkProvider.cs index 7fce286..48e20f3 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/DoculinkProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/DoculinkProvider.cs @@ -6,10 +6,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System; -using System.Collections.Immutable; using System.Diagnostics.Eventing.Reader; using System.Reflection.Metadata; -using System.Xml; using System.Xml.Linq; namespace DamageAssesment.Api.DocuLinks.Providers @@ -67,20 +65,13 @@ namespace DamageAssesment.Api.DocuLinks.Providers int counter = 0; for (int i = 1; i <= 4; i++) { - int linkTypeId = 2; - FileModel fileModel = new FileModel(); - if (i < 3) - { - linkTypeId = 1; - - fileModel = new FileModel() { FileName = "Sample" + i, FileExtension = ".txt", FileContent = "c2FtcGxl", IsAttachments = true, CustomOrder = 1 }; - } - else - fileModel = new FileModel() { url = "www.google" + i + ".com", IsAttachments = false, CustomOrder = 1 }; + FileModel fileModel = new FileModel() { url = "www.google" + i + ".com", IsAttachments = false, CustomOrder = 1 }; ReqDoculink documentInfo = new ReqDoculink() { linkTypeId = i, CustomOrder = i, Files = new List() { fileModel } }; + Db.DoculinkTranslation documents = new Db.DoculinkTranslation { DocumentId = i, title = "Test" + i, description = "ss" + i, Language = "en" }; Models.Doculink document = await azureBlobService.UploadDocument(counter, documentInfo); DocumentDbContext.Documents.Add(mapper.Map(document)); DocumentDbContext.SaveChanges(); + DocumentDbContext.DocumentsTranslations.AddRange(documents); var dbattachments = mapper.Map, List>(document.doclinksAttachments); dbattachments.ForEach(a => a.DocumentId = i); DocumentDbContext.DoclinksAttachments.AddRange(dbattachments); @@ -88,34 +79,6 @@ namespace DamageAssesment.Api.DocuLinks.Providers counter++; } } - if (!DocumentDbContext.DocumentsTranslations.Any()) - { - string[] titles = { - "Mobile App Damage Assessment Instructions", - "PC Damage Assessment Instructions", - "Emergency Evacuation centers", - "Mobile App Damage Assessment Instructions" }; - string[] esTranslations = { - "Instrucciones de Evaluación de Daños de la Aplicación Móvil", - "Instrucciones de Evaluación de Daños del PC", - "Centros de Evacuación de Emergencia", - "Instrucciones de Evaluación de Daños de la Aplicación Móvil" }; - string[] frTranslations = { - "Instructions d'Évaluation des Dommages de l'Application Mobile", - "Instructions d'Évaluation des Dommages du PC", - "Centres d'Évacuation d'Urgence", - "Instructions d'Évaluation des Dommages de l'Application Mobile" }; - List documents = new List(); - for (int i = 0; i < 4; i++) - { - documents.Add(new Db.DoculinkTranslation { DocumentId = i + 1, title = titles[i], description = titles[i], Language = "en" }); - documents.Add(new Db.DoculinkTranslation { DocumentId = i + 1, title = esTranslations[i], description = esTranslations[i], Language = "es" }); - documents.Add(new Db.DoculinkTranslation { DocumentId = i + 1, title = frTranslations[i], description = frTranslations[i], Language = "fr" }); - } - DocumentDbContext.DocumentsTranslations.AddRange(documents); - DocumentDbContext.SaveChanges(); - } - } public List GetDocumentTranslations(int id, string? language) { @@ -172,7 +135,19 @@ namespace DamageAssesment.Api.DocuLinks.Providers MultiLanguage = dicttitle; return MultiLanguage; } - + private List GetDocumentAttachment(int id, string? language) + { + if (string.IsNullOrEmpty(language)) + { + return mapper.Map, List>( + DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id).ToList()); + } + else + { + return mapper.Map, List>( + DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id && a.Language == language).ToList()); + } + } public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetdocumentsByLinkTypeIdAsync(int? linkTypeId, string? language, bool? isactive) { @@ -180,7 +155,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers { logger?.LogInformation("Query Question"); var documents = new List(); - if (linkTypeId==null) + if (linkTypeId == null) documents = await DocumentDbContext.Documents.AsNoTracking().Where(q => (isactive == null || q.IsActive == isactive.Value)).ToListAsync(); else documents = await DocumentDbContext.Documents.AsNoTracking().Where(q => (isactive == null || q.IsActive == isactive.Value) && @@ -194,10 +169,9 @@ namespace DamageAssesment.Api.DocuLinks.Providers item.titles = multilan.titles; item.description = multilan.description; item.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(item.linkTypeId, language)); - item.doclinksAttachments = mapper.Map, List>( - DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == item.Id).ToList()); + item.doclinksAttachments = GetDocumentAttachment(item.Id, language); } - // List doculinks = result.GroupBy(a => a.linkTypeId).Select(a => new ResDoculinks() { linkTypeId = a.Key, doculinks = a.ToList() }).ToList(); + // List doculinks = result.GroupBy(a => a.linkTypeId).Select(a => new ResDoculinks() { linkTypeId = a.Key, doculinks = a.ToList() }).ToList(); return (true, result, null); } return (false, null, "Not found"); @@ -230,10 +204,8 @@ namespace DamageAssesment.Api.DocuLinks.Providers item.titles = multilan.titles; item.description = multilan.description; item.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(item.linkTypeId, language)); - item.doclinksAttachments = mapper.Map, List>( - DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == item.Id).ToList()); + item.doclinksAttachments = GetDocumentAttachment(item.Id, language); } - //List doculinks = result.GroupBy(a => a.linkTypeId).Select(a => new ResDoculinks() { linkTypeId = a.Key, doculinks = a.ToList() }).ToList(); return (true, result, null); } return (false, null, "Not found"); @@ -286,8 +258,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers result.documentsTranslations = mapper.Map, List>( DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == result.Id).ToList()); - result.doclinksAttachments = mapper.Map, List>( - DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id).ToList()); + result.doclinksAttachments = GetDocumentAttachment(id, ""); return (true, result, null); } return (false, null, "Not found"); @@ -319,8 +290,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers result.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(result.linkTypeId, language)); result.titles = multilan.titles; result.description = multilan.description; - result.doclinksAttachments = mapper.Map, List>( - DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id).ToList()); + result.doclinksAttachments = GetDocumentAttachment(id, language); return (true, result, null); } return (false, null, "Not found"); @@ -431,8 +401,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers result.titles = multilan.titles; result.description = multilan.description; result.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(result.linkTypeId, "")); - result.doclinksAttachments = mapper.Map, List>( - DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id).ToList()); + result.doclinksAttachments = GetDocumentAttachment(id, ""); Document.IsActive = false; DocumentDbContext.Documents.Update(Document); DocumentDbContext.SaveChanges(); @@ -450,7 +419,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers { try { - int AttachmentId = DocumentDbContext.DoclinksAttachments.Max(a => a.Id); + int AttachmentId = DocumentDbContext.Documents.Max(a => a.Id); return (true, AttachmentId, ""); } catch (Exception ex) diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/UploadService.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/UploadService.cs index 0e71850..a0aa777 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/UploadService.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/UploadService.cs @@ -30,9 +30,9 @@ namespace DamageAssesment.Api.DocuLinks.Providers { Models.Doculink Documents = new Models.Doculink(); List attachments = new List(); - try + try { - string path = "", UserfileName=""; + string path = "", UserfileName = ""; var fullDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), uploadpath); if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist Directory.CreateDirectory(fullDirectoryPath); @@ -51,20 +51,27 @@ namespace DamageAssesment.Api.DocuLinks.Providers } else path = item.url; - attachments.Add(new Models.DoculinkAttachments { docName=UserfileName,Path=path,IsAttachments=item.IsAttachments,CustomOrder=item.CustomOrder }); + attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder, Language = item.Language }); } } - Documents=new Models.Doculink (){ linkTypeId = documentInfo.linkTypeId, - documentsTranslations = documentInfo.documentsTranslations,doclinksAttachments=attachments, - IsDeleted=false,CustomOrder=documentInfo.CustomOrder, IsActive =true}; - + Documents = new Models.Doculink() + { + linkTypeId = documentInfo.linkTypeId, + documentsTranslations = documentInfo.documentsTranslations, + doclinksAttachments = attachments, + IsDeleted = false, + CustomOrder = documentInfo.CustomOrder, + IsActive = true + }; + return Documents; } - catch (Exception ex) { + catch (Exception ex) + { return new Models.Doculink(); } - + } public Models.Doculink UpdateDocuments(int counter, Models.Doculink document, ReqDoculink documentInfo) { @@ -93,15 +100,15 @@ namespace DamageAssesment.Api.DocuLinks.Providers } else path = item.url; - attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path,IsAttachments=item.IsAttachments,CustomOrder=item.CustomOrder }); + attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path, IsAttachments = item.IsAttachments, CustomOrder = item.CustomOrder, Language = item.Language }); } Models.Doculink Documents = new Models.Doculink() { Id = documentInfo.Id, linkTypeId = documentInfo.linkTypeId, - documentsTranslations=documentInfo.documentsTranslations, + documentsTranslations = documentInfo.documentsTranslations, IsActive = true, - IsDeleted=false, + IsDeleted = false, CustomOrder = documentInfo.CustomOrder, doclinksAttachments = attachments }; @@ -109,7 +116,8 @@ namespace DamageAssesment.Api.DocuLinks.Providers return Documents; } - catch (Exception ex) { + catch (Exception ex) + { return new Models.Doculink(); } } @@ -118,7 +126,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers if (path != "") { FileInfo file = new FileInfo(path); - if (file?.Exists??false)//check file exsit or not + if (file?.Exists ?? false)//check file exsit or not { file.Delete(); } @@ -134,8 +142,8 @@ namespace DamageAssesment.Api.DocuLinks.Providers FileInfo file = new FileInfo(path); if (file?.Exists ?? false)//check file exsit or not { - string filename = file.Name.Replace(file.Extension, " ") + DateTime.Now.ToShortDateString().Replace("/","_") + file.Extension; - file.MoveTo(pathToSave+"\\"+ filename); + string filename = file.Name.Replace(file.Extension, " ") + DateTime.Now.ToShortDateString().Replace("/", "_") + file.Extension; + file.MoveTo(pathToSave + "\\" + filename); } } }