435 lines
20 KiB
C#
435 lines
20 KiB
C#
using AutoMapper;
|
|
using DamageAssesment.Api.Documents.Db;
|
|
using DamageAssesment.Api.Documents.Interfaces;
|
|
using DamageAssesment.Api.Documents.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using System;
|
|
using System.Diagnostics.Eventing.Reader;
|
|
using System.Reflection.Metadata;
|
|
|
|
namespace DamageAssesment.Api.Documents.Providers
|
|
{
|
|
|
|
|
|
public class documentsProvider : IDocumentsProvider
|
|
{
|
|
private DocumentDbContext DocumentDbContext;
|
|
private ILogger<documentsProvider> logger;
|
|
private IUploadService uploadservice;
|
|
private IMapper mapper;
|
|
|
|
public documentsProvider(DocumentDbContext DocumentDbContext, ILogger<documentsProvider> 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() {TypeText = "Forms",IsActive=true, IsAttachment=true });
|
|
DocumentDbContext.LinkTypes.Add(new Db.LinkType() {TypeText = "Communiques",IsActive = true,IsAttachment=false });
|
|
DocumentDbContext.LinkTypes.Add(new Db.LinkType() {TypeText = "Memos",IsActive = true,IsAttachment=true });
|
|
DocumentDbContext.SaveChanges();
|
|
}
|
|
if (!DocumentDbContext.Documents.Any())
|
|
{
|
|
FileModel fileModel = new FileModel() { FileName = "Sample", FileContent = "c2FtcGxl", FileExtension = ".txt" };
|
|
DocumentInfo documentInfo = new DocumentInfo() { linkTypeId = 1, url = "Sample", File = fileModel };
|
|
Models.Document document = uploadservice.UploadDocument(0, documentInfo);
|
|
DocumentDbContext.Documents.Add(mapper.Map<Models.Document, Db.Document>(document));
|
|
DocumentDbContext.SaveChanges();
|
|
}
|
|
if (!DocumentDbContext.DocumentsTranslations.Any())
|
|
{
|
|
Db.DocumentsTranslation documents = new Db.DocumentsTranslation { DocumentId = 1, title = "Test", description = "ss", Language = "en" };
|
|
DocumentDbContext.DocumentsTranslations.Add(documents);
|
|
DocumentDbContext.SaveChanges();
|
|
}
|
|
}
|
|
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)> GetdocumentsByLinkAsync(string? linkType, string? language)
|
|
{
|
|
|
|
try
|
|
{
|
|
logger?.LogInformation("Query Question");
|
|
var documents=new List<Db.Document>();
|
|
if(String.IsNullOrEmpty(linkType))
|
|
documents = await DocumentDbContext.Documents.Include(a=>a.LinkType).AsNoTracking().Where(q => q.IsActive).ToListAsync();
|
|
else
|
|
documents = await DocumentDbContext.Documents.Include(a => a.LinkType).AsNoTracking().Where(q => q.IsActive &&
|
|
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<List<Db.Document>, List<Models.MultiLanDocument>>(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, IEnumerable<Models.MultiLanDocument> 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<Db.Document>, List<Models.MultiLanDocument>>(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.Document Document, string ErrorMessage)> GetDocumentByidAsync(int id)
|
|
{
|
|
try
|
|
{
|
|
logger?.LogInformation("Query LinkType");
|
|
var Document = await DocumentDbContext.Documents.Include(a => a.LinkType).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);
|
|
result.documentsTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>(
|
|
DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == result.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.MultiLanDocument 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.Document();
|
|
if (String.IsNullOrEmpty(linkType))
|
|
Document = await DocumentDbContext.Documents.Include(a => a.LinkType).AsNoTracking().Where(q => q.IsActive&&q.Id==id).FirstOrDefaultAsync();
|
|
else
|
|
Document = await DocumentDbContext.Documents.Include(a => a.LinkType).AsNoTracking().Where(q => q.IsActive && q.Id == id &&
|
|
q.linkTypeId == (DocumentDbContext.LinkTypes.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<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
|
|
{
|
|
var document = mapper.Map<Models.Document, Db.Document>(Document);
|
|
DocumentDbContext.Documents.Add(document);
|
|
DocumentDbContext.SaveChanges();
|
|
var dbtranslation = mapper.Map<List<Models.DocumentsTranslation>, List<Db.DocumentsTranslation>>(Document.documentsTranslations);
|
|
dbtranslation.ForEach(i => i.DocumentId = document.Id);
|
|
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, null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
|
|
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 == id);
|
|
if (existing != null)
|
|
{
|
|
Document.Id = existing.Id;
|
|
var document = mapper.Map<Models.Document, Db.Document>(Document);
|
|
DocumentDbContext.Documents.Update(document);
|
|
DocumentDbContext.SaveChanges();
|
|
var oldtranslations = DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == Document.Id).ToList();
|
|
if (oldtranslations != null)
|
|
DocumentDbContext.DocumentsTranslations.RemoveRange(oldtranslations);
|
|
var dbtranslation = mapper.Map<List<Models.DocumentsTranslation>, List<Db.DocumentsTranslation>>(Document.documentsTranslations);
|
|
dbtranslation.ForEach(i => i.DocumentId = Document.Id);
|
|
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
|
|
{
|
|
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.MultiLanDocument Document, string ErrorMessage)> DeleteDocumentAsync(int id)
|
|
{
|
|
|
|
try
|
|
{
|
|
Db.Document Document = DocumentDbContext.Documents.Include(a => a.LinkType).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, 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.Documents.Max(a => a.Id);
|
|
return (true, AttachmentId, "");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (false, 0, ex.Message);
|
|
}
|
|
}
|
|
|
|
//Link Type methods
|
|
public async Task<(bool IsSuccess, IEnumerable<Models.LinkType> LinkTypes, string ErrorMessage)> GetLinkTypesAsync()
|
|
{
|
|
|
|
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<Db.LinkType>, IEnumerable<Models.LinkType>>(LinkType);
|
|
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.LinkType LinkType, string ErrorMessage)> GetLinkTypeAsync(int Id)
|
|
{
|
|
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<Db.LinkType, Models.LinkType>(LinkType);
|
|
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.LinkType LinkType, string ErrorMessage)> PostLinkTypeAsync(Models.LinkType LinkType)
|
|
{
|
|
try
|
|
{
|
|
logger?.LogInformation("Query LinkType");
|
|
if (!LinkTypeExists(LinkType.Id))
|
|
{
|
|
var dbLink = mapper.Map<Models.LinkType, Db.LinkType>(LinkType);
|
|
DocumentDbContext.LinkTypes.Add(dbLink);
|
|
DocumentDbContext.SaveChanges();
|
|
var result = mapper.Map<Db.LinkType, Models.LinkType>(dbLink);
|
|
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.LinkType LinkType, string ErrorMessage)> UpdateLinkTypeAsync(Models.LinkType LinkType)
|
|
{
|
|
try
|
|
{
|
|
if (LinkType != null)
|
|
{
|
|
var existing = DocumentDbContext.LinkTypes.AsNoTracking().FirstOrDefault(x => x.Id == LinkType.Id);
|
|
if (existing != null)
|
|
{
|
|
var dbLink = mapper.Map<Models.LinkType, Db.LinkType>(LinkType);
|
|
DocumentDbContext.LinkTypes.Update(dbLink);
|
|
DocumentDbContext.SaveChanges();
|
|
return (true, mapper.Map<Db.LinkType, Models.LinkType>(dbLink), "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.LinkType 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;
|
|
DocumentDbContext.LinkTypes.Update(LinkType);
|
|
DocumentDbContext.SaveChanges();
|
|
return (true, mapper.Map<Db.LinkType, Models.LinkType>(LinkType), $"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;
|
|
}
|
|
}
|
|
}
|