diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs index 3490c42..9ca0bbd 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs @@ -17,10 +17,10 @@ namespace DamageAssesment.Api.Documents.Test var mockDocumentService = new Mock(); var mockUploadService = new Mock(); 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(); var mockUploadService = new Mock(); 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(); var mockUploadService = new Mock(); 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(); var mockUploadService = new Mock(); 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(); var mockUploadService = new Mock(); 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(); var mockUploadService = new Mock(); 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")] diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs index bdb0437..dcf43b9 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs @@ -20,123 +20,6 @@ namespace DamageAssesment.Api.Documents.Controllers this.uploadService = uploadService; } - - /// - /// Get all documnets. - /// - /// - [Route("doculinks/{linktype:alpha}")] - [Route("doculinks/{linktype:alpha}/{language:alpha}")] - [HttpGet] - public async Task GetDocumentsbyFormsandLanguageAsync(string linktype, string? language) - { - var result = await this.documentsProvider.GetDocumnetsByLinkAsync(language, linktype); - if (result.IsSuccess) - { - return Ok(result.documents); - } - return NoContent(); - } - /// - /// Get all documnets. - /// - /// - //[Route("doculinks/{language:alpha}")] - [Route("doculinks")] - [HttpGet] - public async Task GetDocumentsAsync(string? language) - { - var result = await this.documentsProvider.GetDocumnetsAsync(language); - if (result.IsSuccess) - { - return Ok(result.documents); - } - return NoContent(); - } - - /// - /// Get a documnet by id. - /// - [HttpGet] - [Route("doculinks/{id}")] - [Route("doculinks/{id}/{language:alpha}")] - public async Task GetDocumentAsync(int id,string? language) - { - var result = await this.documentsProvider.GetDocumentAsync(id,language); - if (result.IsSuccess) - { - return Ok(result.Document); - } - return NotFound(); - } - /// - /// Upload new document. - /// - [HttpPut] - [Route("doculinks/{id}")] - public async Task 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); - } - /// - /// update existing document. - /// - [HttpPost] - [Route("doculinks")] - public async Task 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}"); - } - } - /// - /// Delete documnet by id. - /// - [HttpDelete] - [Route("doculinks/{id}")] - public async Task 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(); - } /// /// Get all document link type. /// @@ -218,5 +101,124 @@ namespace DamageAssesment.Api.Documents.Controllers } return NotFound(); } + /// + /// Get all documents. + /// + /// + [Route("doculinks")] + [Route("doculinks/{linktype:alpha}")] + [Route("doculinks/{linktype:alpha}/{language:alpha}")] + [HttpGet] + public async Task GetDocumentsAsync(string? linktype, string? language) + { + var result = await this.documentsProvider.GetdocumentsByLinkAsync(linktype, language); + if (result.IsSuccess) + { + return Ok(result.documents); + } + return NoContent(); + } + /// + /// Get all documents. + /// + /// + //[Route("doculinks/{language:alpha}")] + //[Route("doculinks")] + //[HttpGet] + //public async Task GetDocumentsAsync(string? language) + //{ + // var result = await this.documentsProvider.GetdocumentsAsync(language); + // if (result.IsSuccess) + // { + // return Ok(result.documents); + // } + // return NoContent(); + //} + + /// + /// Get a document by id. + /// + [HttpGet] + [Route("doculinks/{id}")] + [Route("doculinks/{id}/{linktype:alpha}")] + [Route("doculinks/{id}/{linktype:alpha}/{language:alpha}")] + public async Task 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(); + } + /// + /// Upload new document. + /// + [HttpPut] + [Route("doculinks/{id}")] + public async Task 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); + } + /// + /// update existing document. + /// + [HttpPost] + [Route("doculinks")] + public async Task 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}"); + } + } + /// + /// Delete document by id. + /// + [HttpDelete] + [Route("doculinks/{id}")] + public async Task 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(); + } + } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.DocuLinks.csproj b/DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.DocuLinks.csproj index 03eeae8..dab0e6c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.DocuLinks.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.DocuLinks.csproj @@ -4,6 +4,7 @@ net6.0 enable enable + True diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Db/Document.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Db/Document.cs index d5c7280..5313d72 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Db/Document.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Db/Document.cs @@ -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; } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs index 7cb4e79..58519ec 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs @@ -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 documents, string ErrorMessage)> GetDocumnetsAsync(string? language); - Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? Language,string LinkType); + // Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync(string? language); + Task<(bool IsSuccess, IEnumerable 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); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs index 7ad6c0f..96e49f8 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs @@ -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 { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs index 755a59b..5e02b48 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs @@ -6,7 +6,8 @@ namespace DamageAssesment.Api.Documents.Profiles { public DocumentProfile() { - CreateMap(); + CreateMap().ForMember(dest => dest.LinkType, + opt => opt.MapFrom(src => src.LinkType.TypeText)); ; CreateMap(); CreateMap(); CreateMap(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Program.cs index a1e9361..04b93fe 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Program.cs @@ -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(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs index 9286b97..085d6c3 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs @@ -84,18 +84,18 @@ namespace DamageAssesment.Api.Documents.Providers MultiLanguage.description = dictdesc; return MultiLanguage; } - public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? language, string? LinkType) + public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language) { try { logger?.LogInformation("Query Question"); var documents=new List(); - 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>(documents); @@ -116,40 +116,40 @@ namespace DamageAssesment.Api.Documents.Providers } } - public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync(string? language) - { + //public async Task<(bool IsSuccess, IEnumerable 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>(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>(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");