From 0544c7397d216250b3027f9f35e472fac55ac28c Mon Sep 17 00:00:00 2001 From: uppuv Date: Mon, 11 Sep 2023 13:23:50 -0400 Subject: [PATCH] 314: updated document multi language output format --- ...DamageAssesment.Api.DocuLinks.Test.csproj} | 2 +- .../DocumentsServiceTest.cs | 30 ++--- .../MockData.cs | 23 ++-- .../Controllers/DocumentsController.cs | 74 ++++++------ ...j => DamageAssesment.Api.DocuLinks.csproj} | 0 .../Interfaces/IDocumentsProvider.cs | 13 +- .../Models/Document.cs | 12 +- .../Profiles/DocumentProfile.cs | 1 + .../Providers/DocumentsProvider.cs | 114 ++++++++++++++---- DamageAssesmentApi/DamageAssesment.sln | 4 +- 10 files changed, 176 insertions(+), 97 deletions(-) rename DamageAssesmentApi/DamageAssesment.Api.Documents.Test/{DamageAssesment.Api.Documents.Test.csproj => DamageAssesment.Api.DocuLinks.Test.csproj} (95%) rename DamageAssesmentApi/DamageAssesment.Api.Documents/{DamageAssesment.Api.Documents.csproj => DamageAssesment.Api.DocuLinks.csproj} (100%) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.Documents.Test.csproj b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.DocuLinks.Test.csproj similarity index 95% rename from DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.Documents.Test.csproj rename to DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.DocuLinks.Test.csproj index 725bea6..e990a2c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.Documents.Test.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.DocuLinks.Test.csproj @@ -21,7 +21,7 @@ - + diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs index 9d63289..3490c42 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs @@ -17,7 +17,7 @@ namespace DamageAssesment.Api.Documents.Test var mockDocumentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getNoContentResponse(); - mockDocumentService.Setup(service => service.GetDocumnetsAsync()).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", ""); @@ -31,7 +31,7 @@ namespace DamageAssesment.Api.Documents.Test var mockDocumentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getNoContentResponse(); - mockDocumentService.Setup(service => service.GetDocumnetsAsync()).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", ""); @@ -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()).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (OkObjectResult)await DocumentProvider.GetDocumentsAsync(); + var result = (OkObjectResult)await DocumentProvider.GetDocumentsAsync("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()).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (NoContentResult)await DocumentProvider.GetDocumentsAsync(); + var result = (NoContentResult)await DocumentProvider.GetDocumentsAsync("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)).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumentAsync(1,"en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (OkObjectResult)await DocumentProvider.GetDocumentAsync(1); + var result = (OkObjectResult)await DocumentProvider.GetDocumentAsync(1,"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)).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumentAsync(99, "en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (NotFoundResult)await DocumentProvider.GetDocumentAsync(99); + var result = (NotFoundResult)await DocumentProvider.GetDocumentAsync(99, "en"); Assert.Equal(404, result.StatusCode); } [Fact(DisplayName = "Post Document - Ok case")] @@ -129,11 +129,11 @@ namespace DamageAssesment.Api.Documents.Test var mockResponse = await MockData.getOkResponse(1); var mockInputDocument = await MockData.getInputDocumentData(); var DocumentResponse = await MockData.GetDocumentsInfo(1); - mockDocumentService.Setup(service => service.UpdateDocumentAsync(mockInputDocument)).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.UpdateDocumentAsync(1,mockInputDocument)).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (NoContentResult)await DocumentProvider.UpdateDocument(DocumentResponse); + var result = (NotFoundResult)await DocumentProvider.UpdateDocument(1,DocumentResponse); - Assert.Equal(204, result.StatusCode); + Assert.Equal(404, result.StatusCode); } [Fact(DisplayName = "Put Document - BadRequest case")] @@ -143,9 +143,9 @@ namespace DamageAssesment.Api.Documents.Test var mockUploadService = new Mock(); var mockResponse = await MockData.getBadRequestResponse(); var mockInputDocument = await MockData.getInputDocumentData(); - mockDocumentService.Setup(service => service.UpdateDocumentAsync(mockInputDocument)).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.UpdateDocumentAsync(99,mockInputDocument)).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (BadRequestObjectResult)await DocumentProvider.UpdateDocument(null); + var result = (BadRequestObjectResult)await DocumentProvider.UpdateDocument(99,null); Assert.Equal(400, result.StatusCode); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/MockData.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/MockData.cs index 374a929..e1d3f65 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/MockData.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/MockData.cs @@ -11,12 +11,16 @@ namespace DamageAssesment.Api.Documents.Test public class MockData { - public static async Task<(bool, List, string)> getOkResponse() + public static async Task<(bool, List, string)> getOkResponse() { - List list = new List(); + List list = new List(); for (int i = 1; i < 4; i++) { + Dictionary dicttitle = new Dictionary(); + Dictionary dictdesc = new Dictionary(); + dicttitle.Add("en", "test"); dicttitle.Add("fr", "tester"); + dictdesc.Add("en", "test"); dictdesc.Add("fr", "tester"); List documentsTranslations = new List(); documentsTranslations.Add(new DocumentsTranslation() { @@ -24,7 +28,7 @@ namespace DamageAssesment.Api.Documents.Test title = "tel"+i, description = "Sample"+i }); - list.Add(new Documents.Models.Document() + list.Add(new Documents.Models.MultiLanDocument() { Id = i, @@ -33,32 +37,33 @@ namespace DamageAssesment.Api.Documents.Test url = "testurl" + i, Path = "testpath" + i, IsActive = true, - documentsTranslations= documentsTranslations, + titles= dicttitle, + description=dictdesc, dateCreated = DateTime.Now, dateUpdated = DateTime.Now }); } return (true, list, null); } - public static async Task<(bool, Documents.Models.Document, string)> getOkResponse(int Id) + public static async Task<(bool, Documents.Models.MultiLanDocument, string)> getOkResponse(int Id) { var Documents = await getOkResponse(); var Document = Documents.Item2.FirstOrDefault(s => s.Id == Id); return (true, Document, null); } - public static async Task<(bool, Documents.Models.Document, string)> getBadRequestResponse() + public static async Task<(bool, Documents.Models.MultiLanDocument, string)> getBadRequestResponse() { return (false, null, "Bad Request"); } - public static async Task<(bool, Documents.Models.Document, string)> getNotFoundResponse() + public static async Task<(bool, Documents.Models.MultiLanDocument, string)> getNotFoundResponse() { return (false, null, "Not Found"); } - public static async Task<(bool, IEnumerable, string)> getNoContentResponse() + public static async Task<(bool, IEnumerable, string)> getNoContentResponse() { - IEnumerable list = new List(); + IEnumerable list = new List(); return (false, list, null); } public static async Task GetDocumentsInfo(int id) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs index 38d8ee5..bdb0437 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs @@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Mvc; namespace DamageAssesment.Api.Documents.Controllers { - [Route("api")] [ApiController] public class DocumentsController : ControllerBase { @@ -25,10 +24,13 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get all documnets. /// - [HttpGet("{language}/{LinkType}/documents")] - public async Task GetDocumentsbyFormsandLanguageAsync(string language,string LinkType) + /// + [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); + var result = await this.documentsProvider.GetDocumnetsByLinkAsync(language, linktype); if (result.IsSuccess) { return Ok(result.documents); @@ -38,23 +40,13 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get all documnets. /// - [HttpGet("{LinkType}/documents")] - public async Task GetDocumentsbyFormAsync(string LinkType) + /// + //[Route("doculinks/{language:alpha}")] + [Route("doculinks")] + [HttpGet] + public async Task GetDocumentsAsync(string? language) { - var result = await this.documentsProvider.GetDocumnetsByLinkAsync(null, LinkType); - if (result.IsSuccess) - { - return Ok(result.documents); - } - return NoContent(); - } - /// - /// Get all documnets. - /// - [HttpGet("documents")] - public async Task GetDocumentsAsync() - { - var result = await this.documentsProvider.GetDocumnetsAsync(); + var result = await this.documentsProvider.GetDocumnetsAsync(language); if (result.IsSuccess) { return Ok(result.documents); @@ -65,10 +57,12 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get a documnet by id. /// - [HttpGet("documents/{id}")] - public async Task GetDocumentAsync(int 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); + var result = await this.documentsProvider.GetDocumentAsync(id,language); if (result.IsSuccess) { return Ok(result.Document); @@ -78,30 +72,32 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Upload new document. /// - [HttpPut("documents")] - public async Task UpdateDocument(DocumentInfo documentInfo) + [HttpPut] + [Route("doculinks/{id}")] + public async Task UpdateDocument(int id,DocumentInfo documentInfo) { if (documentInfo != null) { - var dbdoc = await this.documentsProvider.GetDocumentAsync(documentInfo.Id); + var dbdoc = await this.documentsProvider.GetDocumentByidAsync(id); if (dbdoc.IsSuccess) { Models.Document document = uploadService.UpdateDocuments(dbdoc.Document, documentInfo); - var result = await this.documentsProvider.UpdateDocumentAsync(document); + var result = await this.documentsProvider.UpdateDocumentAsync(id,document); if (result.IsSuccess) { return Ok(result.Document); } return NoContent(); } - return NoContent(); + return NotFound(); } return BadRequest(documentInfo); } /// /// update existing document. /// - [HttpPost("documents")] + [HttpPost] + [Route("doculinks")] public async Task CreateDocument(DocumentInfo documentInfo) { try @@ -127,7 +123,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Delete documnet by id. /// - [HttpDelete("documents/{id}")] + [HttpDelete] + [Route("doculinks/{id}")] public async Task DeleteDocument(int id) { // database soft delete @@ -143,7 +140,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get all document link type. /// - [HttpGet("LinkTypes")] + [HttpGet] + [Route("doculinks/types")] public async Task GetLinkTypesAsync() { var result = await this.documentsProvider.GetLinkTypesAsync(); @@ -156,7 +154,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get a document link type by id. /// - [HttpGet("LinkTypes/{id}")] + [HttpGet] + [Route("doculinks/types/{id}")] public async Task GetLinkTypeAsync(int id) { var result = await this.documentsProvider.GetLinkTypeAsync(id); @@ -169,7 +168,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Update a existing document link type. /// - [HttpPut("LinkTypes")] + [HttpPut] + [Route("doculinks/types")] public async Task UpdateLinkType(Models.LinkType linkType) { if (linkType != null) @@ -189,7 +189,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Create a new document link type. /// - [HttpPost("LinkTypes")] + [HttpPost] + [Route("doculinks/types")] public async Task CreateLinkType(Models.LinkType linkType) { if (linkType != null) @@ -206,7 +207,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Delete a existing document link type by id. /// - [HttpDelete("LinkTypes/{id}")] + [HttpDelete] + [Route("doculinks/types/{id}")] public async Task DeleteLinkType(int id) { var result = await this.documentsProvider.DeleteLinkTypeAsync(id); @@ -216,7 +218,5 @@ namespace DamageAssesment.Api.Documents.Controllers } return NotFound(); } - - } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.Documents.csproj b/DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.DocuLinks.csproj similarity index 100% rename from DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.Documents.csproj rename to DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.DocuLinks.csproj diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs index 845a51f..7cb4e79 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs @@ -4,12 +4,13 @@ namespace DamageAssesment.Api.Documents.Interfaces { public interface IDocumentsProvider : ILinkTypesProvider { - Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentAsync(int Id); - Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync(); - Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string Language,string LinkType); - Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document); - Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> UpdateDocumentAsync(Models.Document Document); - Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> DeleteDocumentAsync(int Id); + Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> GetDocumentAsync(int id,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, 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); Task<(bool IsSuccess, int counter, string message)> GetDocumentCounter(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs index 1652dca..7ad6c0f 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs @@ -2,7 +2,16 @@ namespace DamageAssesment.Api.Documents.Models { - public class Document + public class Document : BaseDocument + { + public List documentsTranslations { get; set; } + } + public class MultiLanDocument:BaseDocument + { + public object titles { get; set; } + public object description { get; set; } + } + public class BaseDocument { public int Id { get; set; } @@ -14,6 +23,5 @@ namespace DamageAssesment.Api.Documents.Models public bool IsActive { get; set; } public DateTime dateCreated { get; set; } public DateTime dateUpdated { get; set; } - public List documentsTranslations { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs index f9a2570..755a59b 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs @@ -6,6 +6,7 @@ namespace DamageAssesment.Api.Documents.Profiles { public DocumentProfile() { + CreateMap(); CreateMap(); CreateMap(); CreateMap(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs index 4af3174..9286b97 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs @@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System; using System.Diagnostics.Eventing.Reader; +using System.Reflection.Metadata; namespace DamageAssesment.Api.Documents.Providers { @@ -54,7 +55,36 @@ namespace DamageAssesment.Api.Documents.Providers DocumentDbContext.SaveChanges(); } } - public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string Language, string LinkType) + 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 MultiLanDocument CreateMultiLanguageObject(List questions) + { + MultiLanDocument MultiLanguage = new MultiLanDocument(); + Dictionary dicttitle = new Dictionary(); + Dictionary dictdesc = new Dictionary(); + 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 documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? language, string? LinkType) { try @@ -68,15 +98,12 @@ namespace DamageAssesment.Api.Documents.Providers 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>(documents); + var result = mapper.Map, List>(documents); foreach (var item in result) { - if(!String.IsNullOrEmpty(Language)) - item.documentsTranslations = mapper.Map, List>( - DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id && (a.Language.ToLower() == Language.ToLower())).ToList()); - else - item.documentsTranslations = mapper.Map, List>( - DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id).ToList()); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language)); + item.titles = multilan.titles; + item.description = multilan.description; } return (true, result, null); } @@ -89,7 +116,7 @@ namespace DamageAssesment.Api.Documents.Providers } } - public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync() + public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync(string? language) { try @@ -99,11 +126,12 @@ namespace DamageAssesment.Api.Documents.Providers if (documents != null) { logger?.LogInformation($"{documents.Count} Document(s) found"); - var result = mapper.Map, IEnumerable>(documents); + var result = mapper.Map, List>(documents); foreach (var item in result) { - item.documentsTranslations = mapper.Map, List>( - DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id).ToList()); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language)); + item.titles = multilan.titles; + item.description = multilan.description; } return (true, result, null); } @@ -116,16 +144,16 @@ namespace DamageAssesment.Api.Documents.Providers } } - public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentAsync(int Id) + 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.AsNoTracking().FirstOrDefaultAsync(q => q.Id == id && q.IsActive); if (Document != null) { logger?.LogInformation($"{Document} customer(s) found"); - var result = mapper.Map(Document); + var result = mapper.Map(Document); result.documentsTranslations = mapper.Map, List>( DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == result.Id).ToList()); return (true, result, null); @@ -138,7 +166,30 @@ namespace DamageAssesment.Api.Documents.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document) + public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> GetDocumentAsync(int id,string? language) + { + 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); + 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 { @@ -149,8 +200,11 @@ namespace DamageAssesment.Api.Documents.Providers dbtranslation.ForEach(i => i.DocumentId = document.Id); DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation); DocumentDbContext.SaveChanges(); - Document.Id = document.Id; - return (true, Document, null); + var result = mapper.Map(document); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, "")); + result.titles = multilan.titles; + result.description = multilan.description; + return (true, result, null); } catch (Exception ex) { @@ -159,15 +213,16 @@ namespace DamageAssesment.Api.Documents.Providers } } - public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> UpdateDocumentAsync(Models.Document Document) + 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 == Document.Id); + var existing = DocumentDbContext.Documents.AsNoTracking().FirstOrDefault(x => x.Id == id); if (existing != null) { + Document.Id = existing.Id; var document = mapper.Map(Document); DocumentDbContext.Documents.Update(document); DocumentDbContext.SaveChanges(); @@ -176,7 +231,13 @@ namespace DamageAssesment.Api.Documents.Providers DocumentDbContext.DocumentsTranslations.RemoveRange(oldtranslations); var dbtranslation = mapper.Map, List>(Document.documentsTranslations); dbtranslation.ForEach(i => i.DocumentId = Document.Id); - return (true, Document, "Successful"); + DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation); + DocumentDbContext.SaveChanges(); + var result = mapper.Map(document); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, "")); + result.titles = multilan.titles; + result.description = multilan.description; + return (true, result, "Successful"); } else { @@ -198,20 +259,24 @@ namespace DamageAssesment.Api.Documents.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> DeleteDocumentAsync(int Id) + public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> DeleteDocumentAsync(int id) { try { - Db.Document Document = DocumentDbContext.Documents.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault(); + Db.Document 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; Document.IsActive = false; DocumentDbContext.Documents.Update(Document); DocumentDbContext.SaveChanges(); - return (true, mapper.Map(Document), $"DocumentId {Id} deleted Successfuly"); + return (true, result, $"DocumentId {id} deleted Successfuly"); } catch (Exception ex) { @@ -257,7 +322,6 @@ namespace DamageAssesment.Api.Documents.Providers } } - public async Task<(bool IsSuccess, Models.LinkType LinkType, string ErrorMessage)> GetLinkTypeAsync(int Id) { try diff --git a/DamageAssesmentApi/DamageAssesment.sln b/DamageAssesmentApi/DamageAssesment.sln index 2871442..20b7b6d 100644 --- a/DamageAssesmentApi/DamageAssesment.sln +++ b/DamageAssesmentApi/DamageAssesment.sln @@ -37,9 +37,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Employe EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Employees.Test", "DamageAssesment.Api.Employees.Test\DamageAssesment.Api.Employees.Test.csproj", "{D6BF9AE9-72FA-4726-A326-35A35D27FFB8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DamageAssesment.Api.Documents", "DamageAssesment.Api.Documents\DamageAssesment.Api.Documents.csproj", "{977C1053-9D63-4153-B61B-C3D2F1010F9F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.DocuLinks", "DamageAssesment.Api.Documents\DamageAssesment.Api.DocuLinks.csproj", "{977C1053-9D63-4153-B61B-C3D2F1010F9F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DamageAssesment.Api.Documents.Test", "DamageAssesment.Api.Documents.Test\DamageAssesment.Api.Documents.Test.csproj", "{884BA4AC-9170-49B1-BD6B-850B350C95C0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.DocuLinks.Test", "DamageAssesment.Api.Documents.Test\DamageAssesment.Api.DocuLinks.Test.csproj", "{884BA4AC-9170-49B1-BD6B-850B350C95C0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution