DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs

364 lines
16 KiB
C#
Raw Normal View History

2023-08-31 18:00:51 -05:00
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;
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" };
2023-08-31 18:00:51 -05:00
DocumentDbContext.DocumentsTranslations.Add(documents);
DocumentDbContext.SaveChanges();
}
}
public async Task<(bool IsSuccess, IEnumerable<Models.Document> documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string Language, string LinkType)
{
try
{
logger?.LogInformation("Query Question");
var documents=new List<Db.Document>();
if(String.IsNullOrEmpty(LinkType))
documents = await DocumentDbContext.Documents.AsNoTracking().Where(q => q.IsActive).ToListAsync();
else
documents = await DocumentDbContext.Documents.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<IEnumerable<Db.Document>, IEnumerable<Models.Document>>(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());
}
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.Document> documents, string ErrorMessage)> GetDocumnetsAsync()
{
try
{
logger?.LogInformation("Query Question");
var documents = await DocumentDbContext.Documents.AsNoTracking().Where(q => q.IsActive).ToListAsync();
if (documents != null)
{
logger?.LogInformation($"{documents.Count} Document(s) found");
var result = mapper.Map<IEnumerable<Db.Document>, IEnumerable<Models.Document>>(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());
}
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)> GetDocumentAsync(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<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);
}
}
public async Task<(bool IsSuccess, Models.Document 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();
Document.Id = document.Id;
return (true, Document, null);
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> UpdateDocumentAsync(Models.Document Document)
{
try
{
if (Document != null)
{
var existing = DocumentDbContext.Documents.AsNoTracking().FirstOrDefault(x => x.Id == Document.Id);
if (existing != null)
{
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);
return (true, Document, "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.Document Document, string ErrorMessage)> DeleteDocumentAsync(int Id)
{
try
{
Db.Document Document = DocumentDbContext.Documents.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault();
if (Document == null)
{
return (false, null, "Not Found");
}
Document.IsActive = false;
DocumentDbContext.Documents.Update(Document);
DocumentDbContext.SaveChanges();
return (true, mapper.Map<Db.Document, Models.Document>(Document), $"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;
}
}
}