Merge branch 'dev' into Database-Integration

This commit is contained in:
uppuv 2023-09-13 16:52:43 -04:00
commit 465bf4b081
9 changed files with 189 additions and 168 deletions

View File

@ -17,10 +17,10 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNoContentResponse();
mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse);
mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms","en")).ReturnsAsync(mockResponse);
var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object);
var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", "");
var result = (NoContentResult)await DocumentProvider.GetDocumentsAsync("", "");
Assert.Equal(204, result.StatusCode);
}
@ -31,10 +31,10 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNoContentResponse();
mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse);
mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms","en")).ReturnsAsync(mockResponse);
var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object);
var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", "");
var result = (NoContentResult)await DocumentProvider.GetDocumentsAsync("", "");
Assert.Equal(204, result.StatusCode);
}
@ -44,10 +44,10 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getOkResponse();
mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse);
mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms","en")).ReturnsAsync(mockResponse);
var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object);
var result = (OkObjectResult)await DocumentProvider.GetDocumentsAsync("en");
var result = (OkObjectResult)await DocumentProvider.GetDocumentsAsync("forms","en");
Assert.Equal(200, result.StatusCode);
}
@ -58,10 +58,10 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNoContentResponse();
mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse);
mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms", "en")).ReturnsAsync(mockResponse);
var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object);
var result = (NoContentResult)await DocumentProvider.GetDocumentsAsync("en");
var result = (NoContentResult)await DocumentProvider.GetDocumentsAsync("forms","en");
Assert.Equal(204, result.StatusCode);
}
@ -72,10 +72,10 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getOkResponse(1);
mockDocumentService.Setup(service => service.GetDocumentAsync(1,"en")).ReturnsAsync(mockResponse);
mockDocumentService.Setup(service => service.GetDocumentAsync(1,"forms","en")).ReturnsAsync(mockResponse);
var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object);
var result = (OkObjectResult)await DocumentProvider.GetDocumentAsync(1,"en");
var result = (OkObjectResult)await DocumentProvider.GetDocumentAsync(1, "forms", "en");
Assert.Equal(200, result.StatusCode);
}
@ -86,9 +86,9 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNotFoundResponse();
mockDocumentService.Setup(service => service.GetDocumentAsync(99, "en")).ReturnsAsync(mockResponse);
mockDocumentService.Setup(service => service.GetDocumentAsync(99, "forms", "en")).ReturnsAsync(mockResponse);
var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object);
var result = (NotFoundResult)await DocumentProvider.GetDocumentAsync(99, "en");
var result = (NotFoundResult)await DocumentProvider.GetDocumentAsync(99, "forms", "en");
Assert.Equal(404, result.StatusCode);
}
[Fact(DisplayName = "Post Document - Ok case")]

View File

@ -20,123 +20,6 @@ namespace DamageAssesment.Api.Documents.Controllers
this.uploadService = uploadService;
}
/// <summary>
/// Get all documnets.
/// </summary>
///
[Route("doculinks/{linktype:alpha}")]
[Route("doculinks/{linktype:alpha}/{language:alpha}")]
[HttpGet]
public async Task<IActionResult> GetDocumentsbyFormsandLanguageAsync(string linktype, string? language)
{
var result = await this.documentsProvider.GetDocumnetsByLinkAsync(language, linktype);
if (result.IsSuccess)
{
return Ok(result.documents);
}
return NoContent();
}
/// <summary>
/// Get all documnets.
/// </summary>
///
//[Route("doculinks/{language:alpha}")]
[Route("doculinks")]
[HttpGet]
public async Task<IActionResult> GetDocumentsAsync(string? language)
{
var result = await this.documentsProvider.GetDocumnetsAsync(language);
if (result.IsSuccess)
{
return Ok(result.documents);
}
return NoContent();
}
/// <summary>
/// Get a documnet by id.
/// </summary>
[HttpGet]
[Route("doculinks/{id}")]
[Route("doculinks/{id}/{language:alpha}")]
public async Task<IActionResult> GetDocumentAsync(int id,string? language)
{
var result = await this.documentsProvider.GetDocumentAsync(id,language);
if (result.IsSuccess)
{
return Ok(result.Document);
}
return NotFound();
}
/// <summary>
/// Upload new document.
/// </summary>
[HttpPut]
[Route("doculinks/{id}")]
public async Task<IActionResult> UpdateDocument(int id,DocumentInfo documentInfo)
{
if (documentInfo != null)
{
var dbdoc = await this.documentsProvider.GetDocumentByidAsync(id);
if (dbdoc.IsSuccess)
{
Models.Document document = uploadService.UpdateDocuments(dbdoc.Document, documentInfo);
var result = await this.documentsProvider.UpdateDocumentAsync(id,document);
if (result.IsSuccess)
{
return Ok(result.Document);
}
return NoContent();
}
return NotFound();
}
return BadRequest(documentInfo);
}
/// <summary>
/// update existing document.
/// </summary>
[HttpPost]
[Route("doculinks")]
public async Task<IActionResult> CreateDocument(DocumentInfo documentInfo)
{
try
{
if (documentInfo != null)
{
var documents = await this.documentsProvider.GetDocumentCounter();
Models.Document document = uploadService.UploadDocument(documents.counter, documentInfo);
var result = await this.documentsProvider.PostDocumentAsync(document);
if (result.IsSuccess)
{
return Ok(result.Document);
}
return NoContent();
}
return BadRequest(documentInfo);
}
catch (Exception ex)
{
return BadRequest($"Internal server error: {ex}");
}
}
/// <summary>
/// Delete documnet by id.
/// </summary>
[HttpDelete]
[Route("doculinks/{id}")]
public async Task<IActionResult> DeleteDocument(int id)
{
// database soft delete
var result = await this.documentsProvider.DeleteDocumentAsync(id);
if (result.IsSuccess)
{
// deleting file from folder
uploadService.Movefile(result.Document.Path);
return Ok(result.Document);
}
return NotFound();
}
/// <summary>
/// Get all document link type.
/// </summary>
@ -218,5 +101,124 @@ namespace DamageAssesment.Api.Documents.Controllers
}
return NotFound();
}
/// <summary>
/// Get all documents.
/// </summary>
///
[Route("doculinks")]
[Route("doculinks/{linktype:alpha}")]
[Route("doculinks/{linktype:alpha}/{language:alpha}")]
[HttpGet]
public async Task<IActionResult> GetDocumentsAsync(string? linktype, string? language)
{
var result = await this.documentsProvider.GetdocumentsByLinkAsync(linktype, language);
if (result.IsSuccess)
{
return Ok(result.documents);
}
return NoContent();
}
/// <summary>
/// Get all documents.
/// </summary>
///
//[Route("doculinks/{language:alpha}")]
//[Route("doculinks")]
//[HttpGet]
//public async Task<IActionResult> GetDocumentsAsync(string? language)
//{
// var result = await this.documentsProvider.GetdocumentsAsync(language);
// if (result.IsSuccess)
// {
// return Ok(result.documents);
// }
// return NoContent();
//}
/// <summary>
/// Get a document by id.
/// </summary>
[HttpGet]
[Route("doculinks/{id}")]
[Route("doculinks/{id}/{linktype:alpha}")]
[Route("doculinks/{id}/{linktype:alpha}/{language:alpha}")]
public async Task<IActionResult> GetDocumentAsync(int id,string? linktype, string? language)
{
var result = await this.documentsProvider.GetDocumentAsync(id, linktype, language);
if (result.IsSuccess)
{
return Ok(result.Document);
}
return NotFound();
}
/// <summary>
/// Upload new document.
/// </summary>
[HttpPut]
[Route("doculinks/{id}")]
public async Task<IActionResult> UpdateDocument(int id,DocumentInfo documentInfo)
{
if (documentInfo != null)
{
var dbdoc = await this.documentsProvider.GetDocumentByidAsync(id);
if (dbdoc.IsSuccess)
{
Models.Document document = uploadService.UpdateDocuments(dbdoc.Document, documentInfo);
var result = await this.documentsProvider.UpdateDocumentAsync(id,document);
if (result.IsSuccess)
{
return Ok(result.Document);
}
return NoContent();
}
return NotFound();
}
return BadRequest(documentInfo);
}
/// <summary>
/// update existing document.
/// </summary>
[HttpPost]
[Route("doculinks")]
public async Task<IActionResult> CreateDocument(DocumentInfo documentInfo)
{
try
{
if (documentInfo != null)
{
var documents = await this.documentsProvider.GetDocumentCounter();
Models.Document document = uploadService.UploadDocument(documents.counter, documentInfo);
var result = await this.documentsProvider.PostDocumentAsync(document);
if (result.IsSuccess)
{
return Ok(result.Document);
}
return NoContent();
}
return BadRequest(documentInfo);
}
catch (Exception ex)
{
return BadRequest($"Internal server error: {ex}");
}
}
/// <summary>
/// Delete document by id.
/// </summary>
[HttpDelete]
[Route("doculinks/{id}")]
public async Task<IActionResult> DeleteDocument(int id)
{
// database soft delete
var result = await this.documentsProvider.DeleteDocumentAsync(id);
if (result.IsSuccess)
{
// deleting file from folder
uploadService.Movefile(result.Document.Path);
return Ok(result.Document);
}
return NotFound();
}
}
}

View File

@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>

View File

@ -9,6 +9,7 @@ namespace DamageAssesment.Api.Documents.Db
public int Id { get; set; }
[ForeignKey("LinkType")]
public int linkTypeId { get; set; }
public LinkType? LinkType { get; set; }
public string docName { get; set; }
public string url { get; set; }

View File

@ -4,10 +4,10 @@ namespace DamageAssesment.Api.Documents.Interfaces
{
public interface IDocumentsProvider : ILinkTypesProvider
{
Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> GetDocumentAsync(int id,string? language);
Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> GetDocumentAsync(int id, string? linktype, string? language);
Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentByidAsync(int id);
Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsAsync(string? language);
Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? Language,string LinkType);
// Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsAsync(string? language);
Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language);
Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document);
Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> UpdateDocumentAsync(int id,Models.Document Document);
Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> DeleteDocumentAsync(int id);

View File

@ -10,6 +10,7 @@ namespace DamageAssesment.Api.Documents.Models
{
public object titles { get; set; }
public object description { get; set; }
public string LinkType { get; set; }
}
public class BaseDocument
{

View File

@ -6,7 +6,8 @@ namespace DamageAssesment.Api.Documents.Profiles
{
public DocumentProfile()
{
CreateMap<Db.Document, Models.MultiLanDocument>();
CreateMap<Db.Document, Models.MultiLanDocument>().ForMember(dest => dest.LinkType,
opt => opt.MapFrom(src => src.LinkType.TypeText)); ;
CreateMap<Db.Document, Models.Document>();
CreateMap<Models.Document, Db.Document>();
CreateMap<Db.LinkType, Models.LinkType>();

View File

@ -2,12 +2,20 @@ using DamageAssesment.Api.Documents.Db;
using DamageAssesment.Api.Documents.Interfaces;
using DamageAssesment.Api.Documents.Providers;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
// Include XML comments from your assembly
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

View File

@ -84,18 +84,18 @@ namespace DamageAssesment.Api.Documents.Providers
MultiLanguage.description = dictdesc;
return MultiLanguage;
}
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? language, string? LinkType)
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.AsNoTracking().Where(q => q.IsActive).ToListAsync();
if(String.IsNullOrEmpty(linkType))
documents = await DocumentDbContext.Documents.Include(a=>a.LinkType).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();
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);
@ -116,40 +116,40 @@ namespace DamageAssesment.Api.Documents.Providers
}
}
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsAsync(string? language)
{
//public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsAsync(string? language)
//{
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<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);
}
// 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.AsNoTracking().FirstOrDefaultAsync(q => q.Id == id && q.IsActive);
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");
@ -166,12 +166,19 @@ namespace DamageAssesment.Api.Documents.Providers
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> GetDocumentAsync(int id,string? language)
//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.AsNoTracking().FirstOrDefaultAsync(q => q.Id == id && q.IsActive);
//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");
@ -264,7 +271,7 @@ namespace DamageAssesment.Api.Documents.Providers
try
{
Db.Document Document = DocumentDbContext.Documents.AsNoTracking().Where(a => a.Id == id).FirstOrDefault();
Db.Document Document = DocumentDbContext.Documents.Include(a => a.LinkType).AsNoTracking().Where(a => a.Id == id).FirstOrDefault();
if (Document == null)
{
return (false, null, "Not Found");