forked from MDCPS/DamageAssessment_Backend
		
	314: updated document multi language output format
This commit is contained in:
		| @ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||||
| using System; | ||||
| using System.Diagnostics.Eventing.Reader; | ||||
| using System.Reflection.Metadata; | ||||
|  | ||||
| namespace DamageAssesment.Api.Documents.Providers | ||||
| { | ||||
| @ -54,7 +55,36 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|                 DocumentDbContext.SaveChanges(); | ||||
|             } | ||||
|         } | ||||
|         public async Task<(bool IsSuccess, IEnumerable<Models.Document> documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string Language, string LinkType) | ||||
|         public List<Models.DocumentsTranslation> GetDocumentTranslations(int id, string? language) | ||||
|         { | ||||
|             List<Models.DocumentsTranslation> QuestionTranslations; | ||||
|             if (string.IsNullOrEmpty(language)) | ||||
|             { | ||||
|                 QuestionTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>( | ||||
|                     DocumentDbContext.DocumentsTranslations.AsNoTracking().Where(a => a.DocumentId == id).ToList()); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 QuestionTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>( | ||||
|                 DocumentDbContext.DocumentsTranslations.AsNoTracking().Where(a => a.DocumentId == id && a.Language == language).ToList()); | ||||
|             } | ||||
|             return QuestionTranslations; | ||||
|         } | ||||
|         public MultiLanDocument CreateMultiLanguageObject(List<Models.DocumentsTranslation> questions) | ||||
|         { | ||||
|             MultiLanDocument MultiLanguage = new MultiLanDocument(); | ||||
|             Dictionary<string, string> dicttitle = new Dictionary<string, string>(); | ||||
|             Dictionary<string, string> dictdesc = new Dictionary<string, string>(); | ||||
|             foreach (Models.DocumentsTranslation item in questions) | ||||
|             { | ||||
|                 dicttitle.Add(item.Language, item.title); | ||||
|                 dictdesc.Add(item.Language, item.description); | ||||
|             } | ||||
|             MultiLanguage.titles = dicttitle; | ||||
|             MultiLanguage.description = dictdesc; | ||||
|             return MultiLanguage; | ||||
|         } | ||||
|         public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? language, string? LinkType) | ||||
|         { | ||||
|  | ||||
|             try | ||||
| @ -68,15 +98,12 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|                      q.linkTypeId == (DocumentDbContext.LinkTypes.AsNoTracking().Where(a => a.TypeText.ToLower() == LinkType.ToLower()).Select(a => a.Id).FirstOrDefault())).ToListAsync(); | ||||
|                 if (documents != null) | ||||
|                 { | ||||
|                     var result = mapper.Map<IEnumerable<Db.Document>, IEnumerable<Models.Document>>(documents); | ||||
|                     var result = mapper.Map<List<Db.Document>, List<Models.MultiLanDocument>>(documents); | ||||
|                     foreach (var item in result) | ||||
|                     { | ||||
|                         if(!String.IsNullOrEmpty(Language)) | ||||
|                             item.documentsTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>( | ||||
|                                     DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id && (a.Language.ToLower() == Language.ToLower())).ToList()); | ||||
|                         else | ||||
|                         item.documentsTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>( | ||||
|                                 DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id).ToList()); | ||||
|                         var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language)); | ||||
|                         item.titles = multilan.titles; | ||||
|                         item.description = multilan.description; | ||||
|                     } | ||||
|                     return (true, result, null); | ||||
|                 } | ||||
| @ -89,7 +116,7 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|             } | ||||
|  | ||||
|         } | ||||
|         public async Task<(bool IsSuccess, IEnumerable<Models.Document> documents, string ErrorMessage)> GetDocumnetsAsync() | ||||
|         public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsAsync(string? language) | ||||
|         { | ||||
|  | ||||
|             try | ||||
| @ -99,11 +126,12 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|                 if (documents != null) | ||||
|                 { | ||||
|                     logger?.LogInformation($"{documents.Count} Document(s) found"); | ||||
|                     var result = mapper.Map<IEnumerable<Db.Document>, IEnumerable<Models.Document>>(documents); | ||||
|                     var result = mapper.Map<List<Db.Document>, List<Models.MultiLanDocument>>(documents); | ||||
|                     foreach (var item in result) | ||||
|                     { | ||||
|                         item.documentsTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>( | ||||
|                                 DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id).ToList()); | ||||
|                         var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language)); | ||||
|                         item.titles = multilan.titles; | ||||
|                         item.description = multilan.description; | ||||
|                     } | ||||
|                     return (true, result, null); | ||||
|                 } | ||||
| @ -116,16 +144,16 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|             } | ||||
|  | ||||
|         } | ||||
|         public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentAsync(int Id) | ||||
|         public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentByidAsync(int id) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Query LinkType"); | ||||
|                 var Document = await DocumentDbContext.Documents.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id && q.IsActive); | ||||
|                 var Document = await DocumentDbContext.Documents.AsNoTracking().FirstOrDefaultAsync(q => q.Id == id && q.IsActive); | ||||
|                 if (Document != null) | ||||
|                 { | ||||
|                     logger?.LogInformation($"{Document} customer(s) found"); | ||||
|                     var result = mapper.Map<Db.Document, Models.Document>(Document); | ||||
|                     var result = mapper.Map<Db.Document, Models.Document>(Document);  | ||||
|                     result.documentsTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>( | ||||
|                             DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == result.Id).ToList()); | ||||
|                     return (true, result, null); | ||||
| @ -138,7 +166,30 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|         public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document) | ||||
|         public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> GetDocumentAsync(int id,string? language) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Query LinkType"); | ||||
|                 var Document = await DocumentDbContext.Documents.AsNoTracking().FirstOrDefaultAsync(q => q.Id == id && q.IsActive); | ||||
|                 if (Document != null) | ||||
|                 { | ||||
|                     logger?.LogInformation($"{Document} customer(s) found"); | ||||
|                     var result = mapper.Map<Db.Document, Models.MultiLanDocument>(Document); | ||||
|                     var multilan = CreateMultiLanguageObject(GetDocumentTranslations(result.Id, language)); | ||||
|                     result.titles = multilan.titles; | ||||
|                     result.description = multilan.description; | ||||
|                     return (true, result, null); | ||||
|                 } | ||||
|                 return (false, null, "Not found"); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError(ex.ToString()); | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|         public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
| @ -149,8 +200,11 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|                 dbtranslation.ForEach(i => i.DocumentId = document.Id); | ||||
|                 DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation); | ||||
|                 DocumentDbContext.SaveChanges(); | ||||
|                 Document.Id = document.Id; | ||||
|                 return (true, Document, null); | ||||
|                 var result = mapper.Map<Db.Document, Models.MultiLanDocument>(document); | ||||
|                 var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, "")); | ||||
|                 result.titles = multilan.titles; | ||||
|                 result.description = multilan.description; | ||||
|                 return (true, result, null); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
| @ -159,15 +213,16 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> UpdateDocumentAsync(Models.Document Document) | ||||
|         public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> UpdateDocumentAsync(int id,Models.Document Document) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 if (Document != null) | ||||
|                 { | ||||
|                     var existing = DocumentDbContext.Documents.AsNoTracking().FirstOrDefault(x => x.Id == Document.Id); | ||||
|                     var existing = DocumentDbContext.Documents.AsNoTracking().FirstOrDefault(x => x.Id == id); | ||||
|                     if (existing != null) | ||||
|                     { | ||||
|                         Document.Id = existing.Id; | ||||
|                         var document = mapper.Map<Models.Document, Db.Document>(Document); | ||||
|                         DocumentDbContext.Documents.Update(document); | ||||
|                         DocumentDbContext.SaveChanges(); | ||||
| @ -176,7 +231,13 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|                             DocumentDbContext.DocumentsTranslations.RemoveRange(oldtranslations); | ||||
|                         var dbtranslation = mapper.Map<List<Models.DocumentsTranslation>, List<Db.DocumentsTranslation>>(Document.documentsTranslations); | ||||
|                         dbtranslation.ForEach(i => i.DocumentId = Document.Id); | ||||
|                         return (true, Document, "Successful"); | ||||
|                         DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation); | ||||
|                         DocumentDbContext.SaveChanges(); | ||||
|                         var result = mapper.Map<Db.Document, Models.MultiLanDocument>(document); | ||||
|                         var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, "")); | ||||
|                         result.titles = multilan.titles; | ||||
|                         result.description = multilan.description; | ||||
|                         return (true, result, "Successful"); | ||||
|                     } | ||||
|                     else | ||||
|                     { | ||||
| @ -198,20 +259,24 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|         public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> DeleteDocumentAsync(int Id) | ||||
|         public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> DeleteDocumentAsync(int id) | ||||
|         { | ||||
|  | ||||
|             try | ||||
|             { | ||||
|                 Db.Document Document = DocumentDbContext.Documents.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault(); | ||||
|                 Db.Document Document = DocumentDbContext.Documents.AsNoTracking().Where(a => a.Id == id).FirstOrDefault(); | ||||
|                 if (Document == null) | ||||
|                 { | ||||
|                     return (false, null, "Not Found"); | ||||
|                 } | ||||
|                 var result = mapper.Map<Db.Document, Models.MultiLanDocument>(Document); | ||||
|                 var multilan = CreateMultiLanguageObject(GetDocumentTranslations(Document.Id, "")); | ||||
|                 result.titles = multilan.titles; | ||||
|                 result.description = multilan.description; | ||||
|                 Document.IsActive = false; | ||||
|                 DocumentDbContext.Documents.Update(Document); | ||||
|                 DocumentDbContext.SaveChanges(); | ||||
|                 return (true, mapper.Map<Db.Document, Models.Document>(Document), $"DocumentId {Id} deleted Successfuly"); | ||||
|                 return (true, result, $"DocumentId {id} deleted Successfuly"); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
| @ -257,7 +322,6 @@ namespace DamageAssesment.Api.Documents.Providers | ||||
|             } | ||||
|  | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, Models.LinkType LinkType, string ErrorMessage)> GetLinkTypeAsync(int Id) | ||||
|         { | ||||
|             try | ||||
|  | ||||
		Reference in New Issue
	
	Block a user