diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/DoculinkServiceTest.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/DoculinkServiceTest.cs index b953eeb..14ddaaa 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/DoculinkServiceTest.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/DoculinkServiceTest.cs @@ -16,7 +16,7 @@ namespace DamageAssesment.Api.DocuLinks.Test { var mockDocumentService = new Mock(); var mockUploadService = new Mock(); - var mockResponse = await MockData.getNoContentResponse(); + var mockResponse = await MockData.getNoContentResponses(); mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms","en",null)).ReturnsAsync(mockResponse); var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); @@ -30,7 +30,7 @@ namespace DamageAssesment.Api.DocuLinks.Test { var mockDocumentService = new Mock(); var mockUploadService = new Mock(); - var mockResponse = await MockData.getNoContentResponse(); + var mockResponse = await MockData.getNoContentResponses(); mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms", "en", true)).ReturnsAsync(mockResponse); var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); @@ -43,7 +43,7 @@ namespace DamageAssesment.Api.DocuLinks.Test { var mockDocumentService = new Mock(); var mockUploadService = new Mock(); - var mockResponse = await MockData.getOkResponse(); + var mockResponse = await MockData.getOkResponses(); mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms","en", null)).ReturnsAsync(mockResponse); var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); @@ -56,7 +56,7 @@ namespace DamageAssesment.Api.DocuLinks.Test { var mockDocumentService = new Mock(); var mockUploadService = new Mock(); - var mockResponse = await MockData.getOkResponse(); + var mockResponse = await MockData.getOkResponses(); mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms", "en", true)).ReturnsAsync(mockResponse); var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); @@ -65,6 +65,32 @@ namespace DamageAssesment.Api.DocuLinks.Test Assert.Equal(200, result.StatusCode); } + [Fact(DisplayName = "Get active Documents by linktypeid - Ok case")] + public async Task GetActiveDocumentsLinkTypeIdAsync_ShouldReturnStatusCode200() + { + var mockDocumentService = new Mock(); + var mockUploadService = new Mock(); + var mockResponse = await MockData.getOkResponses(); + mockDocumentService.Setup(service => service.GetdocumentsByLinkTypeIdAsync(null, "en", true)).ReturnsAsync(mockResponse); + + var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); + var result = (OkObjectResult)await DocumentProvider.GetDocumentsByActiveLinkTypeIdAsync(null, "en"); + + Assert.Equal(200, result.StatusCode); + } + [Fact(DisplayName = "Get active Documents by linktypeid - NoContent Case")] + public async Task GetDocumentsLinkTypeIdAsync_ShouldReturnStatusCode204() + { + var mockDocumentService = new Mock(); + var mockUploadService = new Mock(); + var mockResponse = await MockData.getNoContentResponses(); + mockDocumentService.Setup(service => service.GetdocumentsByLinkTypeIdAsync(null, "en", true)).ReturnsAsync(mockResponse); + + var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); + var result = (NoContentResult)await DocumentProvider.GetDocumentsByActiveLinkTypeIdAsync(null, ""); + + Assert.Equal(204, result.StatusCode); + } [Fact(DisplayName = "Get Document by Id - Ok case")] public async Task GetDocumentAsync_ShouldReturnStatusCode200() { diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/MockData.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/MockData.cs index bd10e4b..23ffae4 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/MockData.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks.Test/MockData.cs @@ -11,7 +11,7 @@ namespace DamageAssesment.Api.DocuLinks.Test public class MockData { - public static async Task<(bool, List, string)> getOkResponse() + public static async Task<(bool, List, string)> getOkResponses() { List list = new List(); @@ -46,6 +46,48 @@ namespace DamageAssesment.Api.DocuLinks.Test doclinksAttachments= doclinksAttachments }); } + List doculinks = list.GroupBy(a => a.linkTypeId).Select(a => new ResDoculinks() { linkTypeId = a.Key, doculinks = a.ToList() }).ToList(); + return (true, doculinks, null); + } + + + public static async Task<(bool, List, string)> getOkResponse() + { + 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 DocuLinksTranslations = new List(); + DocuLinksTranslations.Add(new DoculinkTranslation() + { + Language = "en", + title = "tel" + i, + description = "Sample" + i + }); + List doclinksAttachments = new List(); + doclinksAttachments.Add(new DoculinkAttachments() + { + docName = "", + Path = "www.google.com", + IsAttachments = false, + CustomOrder = 1 + }); + list.Add(new DocuLinks.Models.ResDoculink() + { + + Id = i, + linkTypeId = i, + IsActive = true, + titles = dicttitle, + description = dictdesc, + CustomOrder = i, + doclinksAttachments = doclinksAttachments + }); + } return (true, list, null); } public static async Task<(bool, DocuLinks.Models.ResDoculink, string)> getOkResponse(int Id) @@ -64,6 +106,11 @@ namespace DamageAssesment.Api.DocuLinks.Test { return (false, null, "Not Found"); } + public static async Task<(bool, IEnumerable, string)> getNoContentResponses() + { + IEnumerable list = new List(); + return (false, list, null); + } public static async Task<(bool, IEnumerable, string)> getNoContentResponse() { IEnumerable list = new List(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Controllers/DoculinkController.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Controllers/DoculinkController.cs index cfbd508..99d00a8 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Controllers/DoculinkController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Controllers/DoculinkController.cs @@ -136,7 +136,21 @@ namespace DamageAssesment.Api.DocuLinks.Controllers } return NoContent(); } - + /// + /// Get all active Doculink. + /// + [Route("doculinks/active/{linktypeid:int}")] + [Route("doculinks/active/{linktypeid:int}/{language:alpha}")] + [HttpGet] + public async Task GetDocumentsByActiveLinkTypeIdAsync(int? linktypeid, string? language) + { + var result = await this.documentsProvider.GetdocumentsByLinkTypeIdAsync(linktypeid, language, true); + if (result.IsSuccess) + { + return Ok(result.documents); + } + return NoContent(); + } /// /// Get a Doculink by id. /// diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Interfaces/IDoculinkProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Interfaces/IDoculinkProvider.cs index f4f9c84..6d271fd 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Interfaces/IDoculinkProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Interfaces/IDoculinkProvider.cs @@ -7,7 +7,8 @@ namespace DamageAssesment.Api.DocuLinks.Interfaces Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> GetDocumentAsync(int id, string? linktype, string? language); Task<(bool IsSuccess, Models.Doculink Document, string ErrorMessage)> GetDocumentByidAsync(int id); // Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync(string? language); - Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language, bool? isactive); + Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language, bool? isactive); + Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetdocumentsByLinkTypeIdAsync(int? linkTypeId, string? language, bool? isactive); Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> PostDocumentAsync(Models.Doculink Document); Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> UpdateDocumentAsync(int id,Models.Doculink Document); Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> DeleteDocumentAsync(int id); diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/Doculink.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/Doculink.cs index 6fb538b..33259e1 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/Doculink.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Models/Doculink.cs @@ -7,6 +7,11 @@ namespace DamageAssesment.Api.DocuLinks.Models { public List documentsTranslations { get; set; } } + public class ResDoculinks + { + public int linkTypeId { get; set; } + public List doculinks { get; set; } + } public class ResDoculink:BaseDoculink { public object titles { get; set; } diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/DoculinkProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/DoculinkProvider.cs index 5ae15b1..81535ab 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/DoculinkProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Providers/DoculinkProvider.cs @@ -6,8 +6,10 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System; +using System.Collections.Immutable; using System.Diagnostics.Eventing.Reader; using System.Reflection.Metadata; +using System.Xml; using System.Xml.Linq; namespace DamageAssesment.Api.DocuLinks.Providers @@ -63,12 +65,17 @@ namespace DamageAssesment.Api.DocuLinks.Providers int counter = 0; for (int i = 1; i <= 4; i++) { + int linkTypeId = 2; FileModel fileModel = new FileModel(); if (i < 3) + { + linkTypeId = 1; + fileModel = new FileModel() { FileName = "Sample" + i, FileExtension = ".txt", FileContent = "c2FtcGxl", IsAttachments = true, CustomOrder = 1 }; + } else fileModel = new FileModel() { url = "www.google" + i + ".com", IsAttachments = false, CustomOrder = 1 }; - ReqDoculink documentInfo = new ReqDoculink() { linkTypeId = i, CustomOrder = i, Files = new List() { fileModel } }; + ReqDoculink documentInfo = new ReqDoculink() { linkTypeId = linkTypeId, CustomOrder = i, Files = new List() { fileModel } }; Models.Doculink document = uploadservice.UploadDocument(counter, documentInfo); DocumentDbContext.Documents.Add(mapper.Map(document)); DocumentDbContext.SaveChanges(); @@ -163,7 +170,44 @@ namespace DamageAssesment.Api.DocuLinks.Providers MultiLanguage = dicttitle; return MultiLanguage; } - public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language, bool? isactive) + + public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetdocumentsByLinkTypeIdAsync(int? linkTypeId, string? language, bool? isactive) + { + + try + { + logger?.LogInformation("Query Question"); + var documents = new List(); + if (linkTypeId==null) + documents = await DocumentDbContext.Documents.AsNoTracking().Where(q => (isactive == null || q.IsActive == isactive.Value)).ToListAsync(); + else + documents = await DocumentDbContext.Documents.AsNoTracking().Where(q => (isactive == null || q.IsActive == isactive.Value) && + q.linkTypeId == linkTypeId.Value).ToListAsync(); + if (documents != null) + { + 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; + item.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(item.linkTypeId, language)); + item.doclinksAttachments = mapper.Map, List>( + DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == item.Id).ToList()); + } + List doculinks = result.GroupBy(a => a.linkTypeId).Select(a => new ResDoculinks() { linkTypeId = a.Key, doculinks = a.ToList() }).ToList(); + return (true, doculinks, null); + } + return (false, null, "Not found"); + } + catch (Exception ex) + { + logger?.LogError(ex.ToString()); + return (false, null, ex.Message); + } + + } + public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language, bool? isactive) { try @@ -187,7 +231,8 @@ namespace DamageAssesment.Api.DocuLinks.Providers item.doclinksAttachments = mapper.Map, List>( DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == item.Id).ToList()); } - return (true, result, null); + List doculinks = result.GroupBy(a => a.linkTypeId).Select(a => new ResDoculinks() { linkTypeId = a.Key, doculinks = a.ToList() }).ToList(); + return (true, doculinks, null); } return (false, null, "Not found"); }