doculinks changes for grouping

This commit is contained in:
uppuv 2023-10-09 13:29:01 -04:00
parent c7a2dc5910
commit 71d4b524e7
6 changed files with 150 additions and 12 deletions

View File

@ -16,7 +16,7 @@ namespace DamageAssesment.Api.DocuLinks.Test
{ {
var mockDocumentService = new Mock<IDoculinkProvider>(); var mockDocumentService = new Mock<IDoculinkProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNoContentResponse(); var mockResponse = await MockData.getNoContentResponses();
mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms","en",null)).ReturnsAsync(mockResponse); mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms","en",null)).ReturnsAsync(mockResponse);
var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object);
@ -30,7 +30,7 @@ namespace DamageAssesment.Api.DocuLinks.Test
{ {
var mockDocumentService = new Mock<IDoculinkProvider>(); var mockDocumentService = new Mock<IDoculinkProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNoContentResponse(); var mockResponse = await MockData.getNoContentResponses();
mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms", "en", true)).ReturnsAsync(mockResponse); mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms", "en", true)).ReturnsAsync(mockResponse);
var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object);
@ -43,7 +43,7 @@ namespace DamageAssesment.Api.DocuLinks.Test
{ {
var mockDocumentService = new Mock<IDoculinkProvider>(); var mockDocumentService = new Mock<IDoculinkProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getOkResponse(); var mockResponse = await MockData.getOkResponses();
mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms","en", null)).ReturnsAsync(mockResponse); mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms","en", null)).ReturnsAsync(mockResponse);
var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object);
@ -56,7 +56,7 @@ namespace DamageAssesment.Api.DocuLinks.Test
{ {
var mockDocumentService = new Mock<IDoculinkProvider>(); var mockDocumentService = new Mock<IDoculinkProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getOkResponse(); var mockResponse = await MockData.getOkResponses();
mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms", "en", true)).ReturnsAsync(mockResponse); mockDocumentService.Setup(service => service.GetdocumentsByLinkAsync("forms", "en", true)).ReturnsAsync(mockResponse);
var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object); var DocumentProvider = new DoculinkController(mockDocumentService.Object, mockUploadService.Object);
@ -65,6 +65,32 @@ namespace DamageAssesment.Api.DocuLinks.Test
Assert.Equal(200, result.StatusCode); Assert.Equal(200, result.StatusCode);
} }
[Fact(DisplayName = "Get active Documents by linktypeid - Ok case")]
public async Task GetActiveDocumentsLinkTypeIdAsync_ShouldReturnStatusCode200()
{
var mockDocumentService = new Mock<IDoculinkProvider>();
var mockUploadService = new Mock<IUploadService>();
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<IDoculinkProvider>();
var mockUploadService = new Mock<IUploadService>();
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")] [Fact(DisplayName = "Get Document by Id - Ok case")]
public async Task GetDocumentAsync_ShouldReturnStatusCode200() public async Task GetDocumentAsync_ShouldReturnStatusCode200()
{ {

View File

@ -11,7 +11,7 @@ namespace DamageAssesment.Api.DocuLinks.Test
public class MockData public class MockData
{ {
public static async Task<(bool, List<DocuLinks.Models.ResDoculink>, string)> getOkResponse() public static async Task<(bool, List<DocuLinks.Models.ResDoculinks>, string)> getOkResponses()
{ {
List<DocuLinks.Models.ResDoculink> list = new List<DocuLinks.Models.ResDoculink>(); List<DocuLinks.Models.ResDoculink> list = new List<DocuLinks.Models.ResDoculink>();
@ -46,6 +46,48 @@ namespace DamageAssesment.Api.DocuLinks.Test
doclinksAttachments= doclinksAttachments doclinksAttachments= doclinksAttachments
}); });
} }
List<ResDoculinks> 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<DocuLinks.Models.ResDoculink>, string)> getOkResponse()
{
List<DocuLinks.Models.ResDoculink> list = new List<DocuLinks.Models.ResDoculink>();
for (int i = 1; i < 4; i++)
{
Dictionary<string, string> dicttitle = new Dictionary<string, string>();
Dictionary<string, string> dictdesc = new Dictionary<string, string>();
dicttitle.Add("en", "test"); dicttitle.Add("fr", "tester");
dictdesc.Add("en", "test"); dictdesc.Add("fr", "tester");
List<DoculinkTranslation> DocuLinksTranslations = new List<DoculinkTranslation>();
DocuLinksTranslations.Add(new DoculinkTranslation()
{
Language = "en",
title = "tel" + i,
description = "Sample" + i
});
List<DoculinkAttachments> doclinksAttachments = new List<DoculinkAttachments>();
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); return (true, list, null);
} }
public static async Task<(bool, DocuLinks.Models.ResDoculink, string)> getOkResponse(int Id) 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"); return (false, null, "Not Found");
} }
public static async Task<(bool, IEnumerable<DocuLinks.Models.ResDoculinks>, string)> getNoContentResponses()
{
IEnumerable<DocuLinks.Models.ResDoculinks> list = new List<DocuLinks.Models.ResDoculinks>();
return (false, list, null);
}
public static async Task<(bool, IEnumerable<DocuLinks.Models.ResDoculink>, string)> getNoContentResponse() public static async Task<(bool, IEnumerable<DocuLinks.Models.ResDoculink>, string)> getNoContentResponse()
{ {
IEnumerable<DocuLinks.Models.ResDoculink> list = new List<DocuLinks.Models.ResDoculink>(); IEnumerable<DocuLinks.Models.ResDoculink> list = new List<DocuLinks.Models.ResDoculink>();

View File

@ -136,7 +136,21 @@ namespace DamageAssesment.Api.DocuLinks.Controllers
} }
return NoContent(); return NoContent();
} }
/// <summary>
/// Get all active Doculink.
/// </summary>
[Route("doculinks/active/{linktypeid:int}")]
[Route("doculinks/active/{linktypeid:int}/{language:alpha}")]
[HttpGet]
public async Task<IActionResult> GetDocumentsByActiveLinkTypeIdAsync(int? linktypeid, string? language)
{
var result = await this.documentsProvider.GetdocumentsByLinkTypeIdAsync(linktypeid, language, true);
if (result.IsSuccess)
{
return Ok(result.documents);
}
return NoContent();
}
/// <summary> /// <summary>
/// Get a Doculink by id. /// Get a Doculink by id.
/// </summary> /// </summary>

View File

@ -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.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, Models.Doculink Document, string ErrorMessage)> GetDocumentByidAsync(int id);
// Task<(bool IsSuccess, IEnumerable<Models.ResDoculink> documents, string ErrorMessage)> GetDocumnetsAsync(string? language); // Task<(bool IsSuccess, IEnumerable<Models.ResDoculink> documents, string ErrorMessage)> GetDocumnetsAsync(string? language);
Task<(bool IsSuccess, IEnumerable<Models.ResDoculink> documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language, bool? isactive); Task<(bool IsSuccess, IEnumerable<Models.ResDoculinks> documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language, bool? isactive);
Task<(bool IsSuccess, IEnumerable<Models.ResDoculinks> 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)> 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)> UpdateDocumentAsync(int id,Models.Doculink Document);
Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> DeleteDocumentAsync(int id); Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> DeleteDocumentAsync(int id);

View File

@ -7,6 +7,11 @@ namespace DamageAssesment.Api.DocuLinks.Models
{ {
public List<DoculinkTranslation> documentsTranslations { get; set; } public List<DoculinkTranslation> documentsTranslations { get; set; }
} }
public class ResDoculinks
{
public int linkTypeId { get; set; }
public List<ResDoculink> doculinks { get; set; }
}
public class ResDoculink:BaseDoculink public class ResDoculink:BaseDoculink
{ {
public object titles { get; set; } public object titles { get; set; }

View File

@ -6,8 +6,10 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System; using System;
using System.Collections.Immutable;
using System.Diagnostics.Eventing.Reader; using System.Diagnostics.Eventing.Reader;
using System.Reflection.Metadata; using System.Reflection.Metadata;
using System.Xml;
using System.Xml.Linq; using System.Xml.Linq;
namespace DamageAssesment.Api.DocuLinks.Providers namespace DamageAssesment.Api.DocuLinks.Providers
@ -63,12 +65,17 @@ namespace DamageAssesment.Api.DocuLinks.Providers
int counter = 0; int counter = 0;
for (int i = 1; i <= 4; i++) for (int i = 1; i <= 4; i++)
{ {
int linkTypeId = 2;
FileModel fileModel = new FileModel(); FileModel fileModel = new FileModel();
if (i<3) if (i < 3)
fileModel = new FileModel() { FileName="Sample"+i,FileExtension=".txt",FileContent= "c2FtcGxl", IsAttachments = true, CustomOrder = 1 }; {
linkTypeId = 1;
fileModel = new FileModel() { FileName = "Sample" + i, FileExtension = ".txt", FileContent = "c2FtcGxl", IsAttachments = true, CustomOrder = 1 };
}
else else
fileModel = new FileModel() { url = "www.google" + i + ".com", IsAttachments = false, CustomOrder = 1 }; fileModel = new FileModel() { url = "www.google" + i + ".com", IsAttachments = false, CustomOrder = 1 };
ReqDoculink documentInfo = new ReqDoculink() { linkTypeId = i, CustomOrder = i, Files = new List<FileModel>() { fileModel } }; ReqDoculink documentInfo = new ReqDoculink() { linkTypeId = linkTypeId, CustomOrder = i, Files = new List<FileModel>() { fileModel } };
Models.Doculink document = uploadservice.UploadDocument(counter, documentInfo); Models.Doculink document = uploadservice.UploadDocument(counter, documentInfo);
DocumentDbContext.Documents.Add(mapper.Map<Models.Doculink, Db.Doculink>(document)); DocumentDbContext.Documents.Add(mapper.Map<Models.Doculink, Db.Doculink>(document));
DocumentDbContext.SaveChanges(); DocumentDbContext.SaveChanges();
@ -163,7 +170,44 @@ namespace DamageAssesment.Api.DocuLinks.Providers
MultiLanguage = dicttitle; MultiLanguage = dicttitle;
return MultiLanguage; return MultiLanguage;
} }
public async Task<(bool IsSuccess, IEnumerable<Models.ResDoculink> documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language, bool? isactive)
public async Task<(bool IsSuccess, IEnumerable<Models.ResDoculinks> documents, string ErrorMessage)> GetdocumentsByLinkTypeIdAsync(int? linkTypeId, string? language, bool? isactive)
{
try
{
logger?.LogInformation("Query Question");
var documents = new List<Db.Doculink>();
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<Db.Doculink>, List<Models.ResDoculink>>(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<Db.DoculinkAttachments>, List<Models.DoculinkAttachments>>(
DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == item.Id).ToList());
}
List<ResDoculinks> 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<Models.ResDoculinks> documents, string ErrorMessage)> GetdocumentsByLinkAsync(string? linkType, string? language, bool? isactive)
{ {
try try
@ -187,7 +231,8 @@ namespace DamageAssesment.Api.DocuLinks.Providers
item.doclinksAttachments = mapper.Map<List<Db.DoculinkAttachments>, List<Models.DoculinkAttachments>>( item.doclinksAttachments = mapper.Map<List<Db.DoculinkAttachments>, List<Models.DoculinkAttachments>>(
DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == item.Id).ToList()); DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == item.Id).ToList());
} }
return (true, result, null); List<ResDoculinks> 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"); return (false, null, "Not found");
} }