using AutoMapper; using DamageAssesment.Api.DocuLinks.Db; using DamageAssesment.Api.DocuLinks.Interfaces; using DamageAssesment.Api.DocuLinks.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System; using System.Diagnostics.Eventing.Reader; using System.Reflection.Metadata; using System.Xml.Linq; namespace DamageAssesment.Api.DocuLinks.Providers { public class DoculinkProvider : IDoculinkProvider { private DoculinkDbContext DocumentDbContext; private ILogger logger; private IUploadService uploadservice; private IMapper mapper; public DoculinkProvider(DoculinkDbContext DocumentDbContext, ILogger logger, IMapper mapper, IUploadService uploadservice) { this.DocumentDbContext = DocumentDbContext; this.logger = logger; this.mapper = mapper; this.uploadservice = uploadservice; SeedData(); } private void SeedData() { if (!DocumentDbContext.LinkTypes.Any()) { DocumentDbContext.LinkTypes.Add(new Db.LinkType() { IsActive = true, CustomOrder = 1 }); DocumentDbContext.LinkTypes.Add(new Db.LinkType() { IsActive = true, CustomOrder = 2 }); DocumentDbContext.LinkTypes.Add(new Db.LinkType() { IsActive = true, CustomOrder = 3 }); DocumentDbContext.LinkTypes.Add(new Db.LinkType() { IsActive = true, CustomOrder = 4 }); DocumentDbContext.SaveChanges(); } if (!DocumentDbContext.LinksTranslations.Any()) { DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Forms", Language = "en", LinkTypeId = 1 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Communiques", Language = "en", LinkTypeId = 2 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Memos", Language = "en", LinkTypeId = 3 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Trainings", Language = "en", LinkTypeId = 4 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Formularios", Language = "es", LinkTypeId = 1 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Comunicados", Language = "es", LinkTypeId = 2 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "notas", Language = "es", LinkTypeId = 3 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Entrenamientos", Language = "es", LinkTypeId = 4 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Formes", Language = "fr", LinkTypeId = 1 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Communiqués", Language = "fr", LinkTypeId = 2 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Mémos", Language = "fr", LinkTypeId = 3 }); DocumentDbContext.LinksTranslations.Add(new Db.LinksTranslation() { TypeText = "Formations", Language = "fr", LinkTypeId = 4 }); DocumentDbContext.SaveChanges(); } if (!DocumentDbContext.Documents.Any()) { int counter = 0; for (int i = 1; i <= 4; i++) { 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 = uploadservice.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); DocumentDbContext.SaveChanges(); counter++; } } } public List GetDocumentTranslations(int id, string? language) { List QuestionTranslations; if (string.IsNullOrEmpty(language)) { QuestionTranslations = mapper.Map, List>( DocumentDbContext.DocumentsTranslations.AsNoTracking().Where(a => a.DocumentId == id).ToList()); } else { QuestionTranslations = mapper.Map, List>( DocumentDbContext.DocumentsTranslations.AsNoTracking().Where(a => a.DocumentId == id && a.Language == language).ToList()); } return QuestionTranslations; } public ResDoculink CreateMultiLanguageObject(List questions) { ResDoculink MultiLanguage = new ResDoculink(); Dictionary dicttitle = new Dictionary(); Dictionary dictdesc = new Dictionary(); foreach (Models.DoculinkTranslation item in questions) { dicttitle.Add(item.Language, item.title); dictdesc.Add(item.Language, item.description); } MultiLanguage.titles = dicttitle; MultiLanguage.description = dictdesc; return MultiLanguage; } public List GetLinkTypeTranslations(int id, string? language) { List linksTranslations; if (string.IsNullOrEmpty(language)) { linksTranslations = mapper.Map, List>( DocumentDbContext.LinksTranslations.AsNoTracking().Where(a => a.LinkTypeId == id).ToList()); } else { linksTranslations = mapper.Map, List>( DocumentDbContext.LinksTranslations.AsNoTracking().Where(a => a.LinkTypeId == id && a.Language == language).ToList()); } return linksTranslations; } public object CreateMultiLanguageLinkTypeObject(List links) { object MultiLanguage = new object(); Dictionary dicttitle = new Dictionary(); foreach (Models.LinksTranslation item in links) { dicttitle.Add(item.Language, item.TypeText); } MultiLanguage = dicttitle; return MultiLanguage; } public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language, bool? isactive) { try { logger?.LogInformation("Query Question"); var documents = new List(); if (String.IsNullOrEmpty(linkType)) 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) && q.linkTypeId == (DocumentDbContext.LinksTranslations.AsNoTracking().Where(a => a.TypeText.ToLower() == linkType.ToLower()).Select(a => a.Id).FirstOrDefault())).ToListAsync(); if (documents != null) { var result = mapper.Map, List>(documents); foreach (var item in result) { var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language)); 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()); } 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, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync(string? language) //{ // try // { // logger?.LogInformation("Query Question"); // var documents = await DocumentDbContext.Documents.Include(a => a.LinkType).AsNoTracking().Where(q => q.IsActive).ToListAsync(); // if (documents != null) // { // logger?.LogInformation($"{documents.Count} Document(s) found"); // var result = mapper.Map, List>(documents); // foreach (var item in result) // { // var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language)); // item.titles = multilan.titles; // item.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.Doculink Document, string ErrorMessage)> GetDocumentByidAsync(int id) { 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(Document); 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()); return (true, result, null); } return (false, null, "Not found"); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } //added linktype filter public async Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> GetDocumentAsync(int id, string? linkType, string? language) { try { logger?.LogInformation("Query LinkType"); //var Document = await DocumentDbContext.Documents.Include(a => a.LinkType).AsNoTracking().FirstOrDefaultAsync(q => q.Id == id && q.IsActive); var Document = new Db.Doculink(); if (String.IsNullOrEmpty(linkType)) Document = await DocumentDbContext.Documents.AsNoTracking().Where(q => q.IsActive && q.Id == id).FirstOrDefaultAsync(); else Document = await DocumentDbContext.Documents.AsNoTracking().Where(q => q.IsActive && q.Id == id && q.linkTypeId == (DocumentDbContext.LinksTranslations.AsNoTracking().Where(a => a.TypeText.ToLower() == linkType.ToLower()).Select(a => a.Id).FirstOrDefault())).FirstOrDefaultAsync(); if (Document != null) { logger?.LogInformation($"{Document} customer(s) found"); var result = mapper.Map(Document); var multilan = CreateMultiLanguageObject(GetDocumentTranslations(result.Id, language)); 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()); 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.ResDoculink Document, string ErrorMessage)> PostDocumentAsync(Models.Doculink Document) { try { var document = mapper.Map(Document); document.dateCreated = DateTime.Now; document.dateUpdated = DateTime.Now; DocumentDbContext.Documents.Add(document); DocumentDbContext.SaveChanges(); var dbtranslation = mapper.Map, List>(Document.documentsTranslations); dbtranslation.ForEach(i => i.DocumentId = document.Id); DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation); var dbattachments = mapper.Map, List>(Document.doclinksAttachments); dbattachments.ForEach(i => i.DocumentId = document.Id); DocumentDbContext.DoclinksAttachments.AddRange(dbattachments); DocumentDbContext.SaveChanges(); var result = mapper.Map(document); var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, "")); result.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(Document.linkTypeId, "")); result.titles = multilan.titles; result.description = multilan.description; result.doclinksAttachments = Document.doclinksAttachments; return (true, result, null); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } public async Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> UpdateDocumentAsync(int id, Models.Doculink Document) { try { if (Document != null) { var existing = DocumentDbContext.Documents.AsNoTracking().FirstOrDefault(x => x.Id == id); if (existing != null) { Document.Id = existing.Id; var document = mapper.Map(Document); document.dateUpdated = DateTime.Now; DocumentDbContext.Documents.Update(document); DocumentDbContext.SaveChanges(); var oldtranslations = DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == id).ToList(); if (oldtranslations != null) DocumentDbContext.DocumentsTranslations.RemoveRange(oldtranslations); var oldattachments = DocumentDbContext.DoclinksAttachments.Where(a => a.DocumentId == id).ToList(); if (oldattachments != null) DocumentDbContext.DoclinksAttachments.RemoveRange(oldattachments); var dbtranslation = mapper.Map, List>(Document.documentsTranslations); dbtranslation.ForEach(i => i.DocumentId = Document.Id); DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation); var dbattachments = mapper.Map, List>(Document.doclinksAttachments); dbattachments.ForEach(i => i.DocumentId = document.Id); DocumentDbContext.DoclinksAttachments.AddRange(dbattachments); DocumentDbContext.SaveChanges(); var result = mapper.Map(document); var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, "")); result.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(document.linkTypeId, "")); result.titles = multilan.titles; result.description = multilan.description; result.doclinksAttachments = Document.doclinksAttachments; return (true, result, "Successful"); } else { logger?.LogInformation($"{Document} Not found"); return (false, null, "Not Found"); } } else { logger?.LogInformation($"{Document} Bad Request"); return (false, null, "Bad request"); } } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } public async Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> DeleteDocumentAsync(int id) { try { Db.Doculink Document = DocumentDbContext.Documents.AsNoTracking().Where(a => a.Id == id).FirstOrDefault(); if (Document == null) { return (false, null, "Not Found"); } var result = mapper.Map(Document); var multilan = CreateMultiLanguageObject(GetDocumentTranslations(Document.Id, "")); 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()); Document.IsActive = false; DocumentDbContext.Documents.Update(Document); DocumentDbContext.SaveChanges(); return (true, result, $"DocumentId {id} deleted Successfuly"); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } public async Task<(bool IsSuccess, int counter, string message)> GetDocumentCounter() { try { int AttachmentId = DocumentDbContext.DoclinksAttachments.Max(a => a.Id); return (true, AttachmentId, ""); } catch (Exception ex) { return (false, 0, ex.Message); } } //Link Type methods public async Task<(bool IsSuccess, IEnumerable LinkTypes, string ErrorMessage)> GetLinkTypesAsync(string? language) { try { logger?.LogInformation("Query Question"); var LinkType = await DocumentDbContext.LinkTypes.AsNoTracking().Where(q => q.IsActive).ToListAsync(); if (LinkType != null) { logger?.LogInformation($"{LinkType.Count} LinkTypes(s) found"); var result = mapper.Map, IEnumerable>(LinkType); foreach (var item in result) { item.titles = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(item.Id, language)); } 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.ResLinkType LinkType, string ErrorMessage)> GetLinkTypeAsync(int Id, string? language) { try { logger?.LogInformation("Query LinkType"); var LinkType = await DocumentDbContext.LinkTypes.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id && q.IsActive); if (LinkType != null) { logger?.LogInformation($"{LinkType} customer(s) found"); var result = mapper.Map(LinkType); result.titles = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(result.Id, language)); 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.ResLinkType LinkType, string ErrorMessage)> PostLinkTypeAsync(Models.LinkType LinkType) { try { logger?.LogInformation("Query LinkType"); if (!LinkTypeExists(LinkType.Id)) { var dbLink = mapper.Map(LinkType); DocumentDbContext.LinkTypes.Add(dbLink); DocumentDbContext.SaveChanges(); var dbtranslation = mapper.Map, List>(LinkType.linksTranslations); dbtranslation.ForEach(i => i.LinkTypeId = dbLink.Id); DocumentDbContext.LinksTranslations.AddRange(dbtranslation); DocumentDbContext.SaveChanges(); var result = mapper.Map(dbLink); result.titles = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(result.Id, "")); return (true, result, null); } return (false, null, "LinkType is already exits"); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } public async Task<(bool IsSuccess, Models.ResLinkType LinkType, string ErrorMessage)> UpdateLinkTypeAsync(int id, Models.LinkType LinkType) { try { if (LinkType != null) { var existing = DocumentDbContext.LinkTypes.AsNoTracking().FirstOrDefault(x => x.Id == id); if (existing != null) { var dbLink = mapper.Map(LinkType); DocumentDbContext.LinkTypes.Update(dbLink); DocumentDbContext.SaveChanges(); var oldtranslations = DocumentDbContext.LinksTranslations.Where(a => a.LinkTypeId == id).ToList(); if (oldtranslations != null) DocumentDbContext.LinksTranslations.RemoveRange(oldtranslations); var dbtranslation = mapper.Map, List>(LinkType.linksTranslations); dbtranslation.ForEach(i => i.LinkTypeId = dbLink.Id); DocumentDbContext.LinksTranslations.AddRange(dbtranslation); DocumentDbContext.SaveChanges(); var result = mapper.Map(dbLink); result.titles = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(result.Id, "")); return (true, result, "Successful"); } else { logger?.LogInformation($"{LinkType} Not found"); return (false, null, "Not Found"); } } else { logger?.LogInformation($"{LinkType} Bad Request"); return (false, null, "Bad request"); } } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } public async Task<(bool IsSuccess, Models.ResLinkType LinkType, string ErrorMessage)> DeleteLinkTypeAsync(int id) { try { Db.LinkType LinkType = DocumentDbContext.LinkTypes.AsNoTracking().Where(a => a.Id == id).FirstOrDefault(); if (LinkType == null) { return (false, null, "Not Found"); } LinkType.IsActive = false; var result = mapper.Map(LinkType); result.titles = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(result.Id, "")); DocumentDbContext.LinkTypes.Update(LinkType); DocumentDbContext.SaveChanges(); return (true, result, $"LinkTypeId {id} deleted Successfuly"); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } private bool LinkTypeExists(int id) { return DocumentDbContext.LinkTypes.AsNoTracking().Count(e => e.Id == id) > 0; } } }