Reginald Cherenfant Jasmin 2023-09-13 01:29:14 -04:00
commit fedbb8a93e
10 changed files with 176 additions and 97 deletions

View File

@ -21,7 +21,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\DamageAssesment.Api.Documents\DamageAssesment.Api.Documents.csproj" /> <ProjectReference Include="..\DamageAssesment.Api.Documents\DamageAssesment.Api.DocuLinks.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -17,7 +17,7 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>(); var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNoContentResponse(); 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 DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object);
var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", ""); var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", "");
@ -31,7 +31,7 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>(); var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNoContentResponse(); 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 DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object);
var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", ""); var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", "");
@ -44,10 +44,10 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>(); var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getOkResponse(); 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 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); Assert.Equal(200, result.StatusCode);
} }
@ -58,10 +58,10 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>(); var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNoContentResponse(); 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 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); Assert.Equal(204, result.StatusCode);
} }
@ -72,10 +72,10 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>(); var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getOkResponse(1); 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 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); Assert.Equal(200, result.StatusCode);
} }
@ -86,9 +86,9 @@ namespace DamageAssesment.Api.Documents.Test
var mockDocumentService = new Mock<IDocumentsProvider>(); var mockDocumentService = new Mock<IDocumentsProvider>();
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getNotFoundResponse(); 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 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); Assert.Equal(404, result.StatusCode);
} }
[Fact(DisplayName = "Post Document - Ok case")] [Fact(DisplayName = "Post Document - Ok case")]
@ -129,11 +129,11 @@ namespace DamageAssesment.Api.Documents.Test
var mockResponse = await MockData.getOkResponse(1); var mockResponse = await MockData.getOkResponse(1);
var mockInputDocument = await MockData.getInputDocumentData(); var mockInputDocument = await MockData.getInputDocumentData();
var DocumentResponse = await MockData.GetDocumentsInfo(1); 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 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")] [Fact(DisplayName = "Put Document - BadRequest case")]
@ -143,9 +143,9 @@ namespace DamageAssesment.Api.Documents.Test
var mockUploadService = new Mock<IUploadService>(); var mockUploadService = new Mock<IUploadService>();
var mockResponse = await MockData.getBadRequestResponse(); var mockResponse = await MockData.getBadRequestResponse();
var mockInputDocument = await MockData.getInputDocumentData(); 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 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); Assert.Equal(400, result.StatusCode);
} }

View File

@ -11,12 +11,16 @@ namespace DamageAssesment.Api.Documents.Test
public class MockData public class MockData
{ {
public static async Task<(bool, List<Documents.Models.Document>, string)> getOkResponse() public static async Task<(bool, List<Documents.Models.MultiLanDocument>, string)> getOkResponse()
{ {
List<Documents.Models.Document> list = new List<Documents.Models.Document>(); List<Documents.Models.MultiLanDocument> list = new List<Documents.Models.MultiLanDocument>();
for (int i = 1; i < 4; i++) 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<Documents.Models.DocumentsTranslation> documentsTranslations = new List<DocumentsTranslation>(); List<Documents.Models.DocumentsTranslation> documentsTranslations = new List<DocumentsTranslation>();
documentsTranslations.Add(new DocumentsTranslation() documentsTranslations.Add(new DocumentsTranslation()
{ {
@ -24,7 +28,7 @@ namespace DamageAssesment.Api.Documents.Test
title = "tel"+i, title = "tel"+i,
description = "Sample"+i description = "Sample"+i
}); });
list.Add(new Documents.Models.Document() list.Add(new Documents.Models.MultiLanDocument()
{ {
Id = i, Id = i,
@ -33,32 +37,33 @@ namespace DamageAssesment.Api.Documents.Test
url = "testurl" + i, url = "testurl" + i,
Path = "testpath" + i, Path = "testpath" + i,
IsActive = true, IsActive = true,
documentsTranslations= documentsTranslations, titles= dicttitle,
description=dictdesc,
dateCreated = DateTime.Now, dateCreated = DateTime.Now,
dateUpdated = DateTime.Now dateUpdated = DateTime.Now
}); });
} }
return (true, list, null); 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 Documents = await getOkResponse();
var Document = Documents.Item2.FirstOrDefault(s => s.Id == Id); var Document = Documents.Item2.FirstOrDefault(s => s.Id == Id);
return (true, Document, null); 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"); 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"); return (false, null, "Not Found");
} }
public static async Task<(bool, IEnumerable<Documents.Models.Document>, string)> getNoContentResponse() public static async Task<(bool, IEnumerable<Documents.Models.MultiLanDocument>, string)> getNoContentResponse()
{ {
IEnumerable<Documents.Models.Document> list = new List<Documents.Models.Document>(); IEnumerable<Documents.Models.MultiLanDocument> list = new List<Documents.Models.MultiLanDocument>();
return (false, list, null); return (false, list, null);
} }
public static async Task<Documents.Models.DocumentInfo> GetDocumentsInfo(int id) public static async Task<Documents.Models.DocumentInfo> GetDocumentsInfo(int id)

View File

@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Documents.Controllers namespace DamageAssesment.Api.Documents.Controllers
{ {
[Route("api")]
[ApiController] [ApiController]
public class DocumentsController : ControllerBase public class DocumentsController : ControllerBase
{ {
@ -25,10 +24,13 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Get all documnets. /// Get all documnets.
/// </summary> /// </summary>
[HttpGet("{language}/{LinkType}/documents")] ///
public async Task<IActionResult> GetDocumentsbyFormsandLanguageAsync(string language,string LinkType) [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); var result = await this.documentsProvider.GetDocumnetsByLinkAsync(language, linktype);
if (result.IsSuccess) if (result.IsSuccess)
{ {
return Ok(result.documents); return Ok(result.documents);
@ -38,23 +40,13 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Get all documnets. /// Get all documnets.
/// </summary> /// </summary>
[HttpGet("{LinkType}/documents")] ///
public async Task<IActionResult> GetDocumentsbyFormAsync(string LinkType) //[Route("doculinks/{language:alpha}")]
[Route("doculinks")]
[HttpGet]
public async Task<IActionResult> GetDocumentsAsync(string? language)
{ {
var result = await this.documentsProvider.GetDocumnetsByLinkAsync(null, LinkType); var result = await this.documentsProvider.GetDocumnetsAsync(language);
if (result.IsSuccess)
{
return Ok(result.documents);
}
return NoContent();
}
/// <summary>
/// Get all documnets.
/// </summary>
[HttpGet("documents")]
public async Task<IActionResult> GetDocumentsAsync()
{
var result = await this.documentsProvider.GetDocumnetsAsync();
if (result.IsSuccess) if (result.IsSuccess)
{ {
return Ok(result.documents); return Ok(result.documents);
@ -65,10 +57,12 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Get a documnet by id. /// Get a documnet by id.
/// </summary> /// </summary>
[HttpGet("documents/{id}")] [HttpGet]
public async Task<IActionResult> GetDocumentAsync(int id) [Route("doculinks/{id}")]
[Route("doculinks/{id}/{language:alpha}")]
public async Task<IActionResult> GetDocumentAsync(int id,string? language)
{ {
var result = await this.documentsProvider.GetDocumentAsync(id); var result = await this.documentsProvider.GetDocumentAsync(id,language);
if (result.IsSuccess) if (result.IsSuccess)
{ {
return Ok(result.Document); return Ok(result.Document);
@ -78,30 +72,32 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Upload new document. /// Upload new document.
/// </summary> /// </summary>
[HttpPut("documents")] [HttpPut]
public async Task<IActionResult> UpdateDocument(DocumentInfo documentInfo) [Route("doculinks/{id}")]
public async Task<IActionResult> UpdateDocument(int id,DocumentInfo documentInfo)
{ {
if (documentInfo != null) if (documentInfo != null)
{ {
var dbdoc = await this.documentsProvider.GetDocumentAsync(documentInfo.Id); var dbdoc = await this.documentsProvider.GetDocumentByidAsync(id);
if (dbdoc.IsSuccess) if (dbdoc.IsSuccess)
{ {
Models.Document document = uploadService.UpdateDocuments(dbdoc.Document, documentInfo); 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) if (result.IsSuccess)
{ {
return Ok(result.Document); return Ok(result.Document);
} }
return NoContent(); return NoContent();
} }
return NoContent(); return NotFound();
} }
return BadRequest(documentInfo); return BadRequest(documentInfo);
} }
/// <summary> /// <summary>
/// update existing document. /// update existing document.
/// </summary> /// </summary>
[HttpPost("documents")] [HttpPost]
[Route("doculinks")]
public async Task<IActionResult> CreateDocument(DocumentInfo documentInfo) public async Task<IActionResult> CreateDocument(DocumentInfo documentInfo)
{ {
try try
@ -127,7 +123,8 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Delete documnet by id. /// Delete documnet by id.
/// </summary> /// </summary>
[HttpDelete("documents/{id}")] [HttpDelete]
[Route("doculinks/{id}")]
public async Task<IActionResult> DeleteDocument(int id) public async Task<IActionResult> DeleteDocument(int id)
{ {
// database soft delete // database soft delete
@ -143,7 +140,8 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Get all document link type. /// Get all document link type.
/// </summary> /// </summary>
[HttpGet("LinkTypes")] [HttpGet]
[Route("doculinks/types")]
public async Task<IActionResult> GetLinkTypesAsync() public async Task<IActionResult> GetLinkTypesAsync()
{ {
var result = await this.documentsProvider.GetLinkTypesAsync(); var result = await this.documentsProvider.GetLinkTypesAsync();
@ -156,7 +154,8 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Get a document link type by id. /// Get a document link type by id.
/// </summary> /// </summary>
[HttpGet("LinkTypes/{id}")] [HttpGet]
[Route("doculinks/types/{id}")]
public async Task<IActionResult> GetLinkTypeAsync(int id) public async Task<IActionResult> GetLinkTypeAsync(int id)
{ {
var result = await this.documentsProvider.GetLinkTypeAsync(id); var result = await this.documentsProvider.GetLinkTypeAsync(id);
@ -169,7 +168,8 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Update a existing document link type. /// Update a existing document link type.
/// </summary> /// </summary>
[HttpPut("LinkTypes")] [HttpPut]
[Route("doculinks/types")]
public async Task<IActionResult> UpdateLinkType(Models.LinkType linkType) public async Task<IActionResult> UpdateLinkType(Models.LinkType linkType)
{ {
if (linkType != null) if (linkType != null)
@ -189,7 +189,8 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Create a new document link type. /// Create a new document link type.
/// </summary> /// </summary>
[HttpPost("LinkTypes")] [HttpPost]
[Route("doculinks/types")]
public async Task<IActionResult> CreateLinkType(Models.LinkType linkType) public async Task<IActionResult> CreateLinkType(Models.LinkType linkType)
{ {
if (linkType != null) if (linkType != null)
@ -206,7 +207,8 @@ namespace DamageAssesment.Api.Documents.Controllers
/// <summary> /// <summary>
/// Delete a existing document link type by id. /// Delete a existing document link type by id.
/// </summary> /// </summary>
[HttpDelete("LinkTypes/{id}")] [HttpDelete]
[Route("doculinks/types/{id}")]
public async Task<IActionResult> DeleteLinkType(int id) public async Task<IActionResult> DeleteLinkType(int id)
{ {
var result = await this.documentsProvider.DeleteLinkTypeAsync(id); var result = await this.documentsProvider.DeleteLinkTypeAsync(id);
@ -216,7 +218,5 @@ namespace DamageAssesment.Api.Documents.Controllers
} }
return NotFound(); return NotFound();
} }
} }
} }

View File

@ -4,12 +4,13 @@ namespace DamageAssesment.Api.Documents.Interfaces
{ {
public interface IDocumentsProvider : ILinkTypesProvider public interface IDocumentsProvider : ILinkTypesProvider
{ {
Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentAsync(int Id); Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> GetDocumentAsync(int id,string? language);
Task<(bool IsSuccess, IEnumerable<Models.Document> documents, string ErrorMessage)> GetDocumnetsAsync(); Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentByidAsync(int id);
Task<(bool IsSuccess, IEnumerable<Models.Document> documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string Language,string LinkType); Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsAsync(string? language);
Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document); Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? Language,string LinkType);
Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> UpdateDocumentAsync(Models.Document Document); Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document);
Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> DeleteDocumentAsync(int Id); 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(); Task<(bool IsSuccess, int counter, string message)> GetDocumentCounter();

View File

@ -2,7 +2,16 @@
namespace DamageAssesment.Api.Documents.Models namespace DamageAssesment.Api.Documents.Models
{ {
public class Document public class Document : BaseDocument
{
public List<DocumentsTranslation> 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; } public int Id { get; set; }
@ -14,6 +23,5 @@ namespace DamageAssesment.Api.Documents.Models
public bool IsActive { get; set; } public bool IsActive { get; set; }
public DateTime dateCreated { get; set; } public DateTime dateCreated { get; set; }
public DateTime dateUpdated { get; set; } public DateTime dateUpdated { get; set; }
public List<DocumentsTranslation> documentsTranslations { get; set; }
} }
} }

View File

@ -6,6 +6,7 @@ namespace DamageAssesment.Api.Documents.Profiles
{ {
public DocumentProfile() public DocumentProfile()
{ {
CreateMap<Db.Document, Models.MultiLanDocument>();
CreateMap<Db.Document, Models.Document>(); CreateMap<Db.Document, Models.Document>();
CreateMap<Models.Document, Db.Document>(); CreateMap<Models.Document, Db.Document>();
CreateMap<Db.LinkType, Models.LinkType>(); CreateMap<Db.LinkType, Models.LinkType>();

View File

@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System; using System;
using System.Diagnostics.Eventing.Reader; using System.Diagnostics.Eventing.Reader;
using System.Reflection.Metadata;
namespace DamageAssesment.Api.Documents.Providers namespace DamageAssesment.Api.Documents.Providers
{ {
@ -54,7 +55,36 @@ namespace DamageAssesment.Api.Documents.Providers
DocumentDbContext.SaveChanges(); DocumentDbContext.SaveChanges();
} }
} }
public async Task<(bool IsSuccess, IEnumerable<Models.Document> documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string Language, string LinkType) public List<Models.DocumentsTranslation> GetDocumentTranslations(int id, string? language)
{
List<Models.DocumentsTranslation> QuestionTranslations;
if (string.IsNullOrEmpty(language))
{
QuestionTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>(
DocumentDbContext.DocumentsTranslations.AsNoTracking().Where(a => a.DocumentId == id).ToList());
}
else
{
QuestionTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>(
DocumentDbContext.DocumentsTranslations.AsNoTracking().Where(a => a.DocumentId == id && a.Language == language).ToList());
}
return QuestionTranslations;
}
public MultiLanDocument CreateMultiLanguageObject(List<Models.DocumentsTranslation> questions)
{
MultiLanDocument MultiLanguage = new MultiLanDocument();
Dictionary<string, string> dicttitle = new Dictionary<string, string>();
Dictionary<string, string> dictdesc = new Dictionary<string, string>();
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<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? language, string? LinkType)
{ {
try 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(); q.linkTypeId == (DocumentDbContext.LinkTypes.AsNoTracking().Where(a => a.TypeText.ToLower() == LinkType.ToLower()).Select(a => a.Id).FirstOrDefault())).ToListAsync();
if (documents != null) if (documents != null)
{ {
var result = mapper.Map<IEnumerable<Db.Document>, IEnumerable<Models.Document>>(documents); var result = mapper.Map<List<Db.Document>, List<Models.MultiLanDocument>>(documents);
foreach (var item in result) foreach (var item in result)
{ {
if(!String.IsNullOrEmpty(Language)) var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language));
item.documentsTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>( item.titles = multilan.titles;
DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id && (a.Language.ToLower() == Language.ToLower())).ToList()); item.description = multilan.description;
else
item.documentsTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>(
DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id).ToList());
} }
return (true, result, null); return (true, result, null);
} }
@ -89,7 +116,7 @@ namespace DamageAssesment.Api.Documents.Providers
} }
} }
public async Task<(bool IsSuccess, IEnumerable<Models.Document> documents, string ErrorMessage)> GetDocumnetsAsync() public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanDocument> documents, string ErrorMessage)> GetDocumnetsAsync(string? language)
{ {
try try
@ -99,11 +126,12 @@ namespace DamageAssesment.Api.Documents.Providers
if (documents != null) if (documents != null)
{ {
logger?.LogInformation($"{documents.Count} Document(s) found"); logger?.LogInformation($"{documents.Count} Document(s) found");
var result = mapper.Map<IEnumerable<Db.Document>, IEnumerable<Models.Document>>(documents); var result = mapper.Map<List<Db.Document>, List<Models.MultiLanDocument>>(documents);
foreach (var item in result) foreach (var item in result)
{ {
item.documentsTranslations = mapper.Map<List<Db.DocumentsTranslation>, List<Models.DocumentsTranslation>>( var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language));
DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id).ToList()); item.titles = multilan.titles;
item.description = multilan.description;
} }
return (true, result, null); return (true, result, null);
} }
@ -116,12 +144,12 @@ 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 try
{ {
logger?.LogInformation("Query LinkType"); 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) if (Document != null)
{ {
logger?.LogInformation($"{Document} customer(s) found"); logger?.LogInformation($"{Document} customer(s) found");
@ -138,7 +166,30 @@ namespace DamageAssesment.Api.Documents.Providers
return (false, null, ex.Message); 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<Db.Document, Models.MultiLanDocument>(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 try
{ {
@ -149,8 +200,11 @@ namespace DamageAssesment.Api.Documents.Providers
dbtranslation.ForEach(i => i.DocumentId = document.Id); dbtranslation.ForEach(i => i.DocumentId = document.Id);
DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation); DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation);
DocumentDbContext.SaveChanges(); DocumentDbContext.SaveChanges();
Document.Id = document.Id; var result = mapper.Map<Db.Document, Models.MultiLanDocument>(document);
return (true, Document, null); var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, ""));
result.titles = multilan.titles;
result.description = multilan.description;
return (true, result, null);
} }
catch (Exception ex) 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 try
{ {
if (Document != null) 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) if (existing != null)
{ {
Document.Id = existing.Id;
var document = mapper.Map<Models.Document, Db.Document>(Document); var document = mapper.Map<Models.Document, Db.Document>(Document);
DocumentDbContext.Documents.Update(document); DocumentDbContext.Documents.Update(document);
DocumentDbContext.SaveChanges(); DocumentDbContext.SaveChanges();
@ -176,7 +231,13 @@ namespace DamageAssesment.Api.Documents.Providers
DocumentDbContext.DocumentsTranslations.RemoveRange(oldtranslations); DocumentDbContext.DocumentsTranslations.RemoveRange(oldtranslations);
var dbtranslation = mapper.Map<List<Models.DocumentsTranslation>, List<Db.DocumentsTranslation>>(Document.documentsTranslations); var dbtranslation = mapper.Map<List<Models.DocumentsTranslation>, List<Db.DocumentsTranslation>>(Document.documentsTranslations);
dbtranslation.ForEach(i => i.DocumentId = Document.Id); dbtranslation.ForEach(i => i.DocumentId = Document.Id);
return (true, Document, "Successful"); DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation);
DocumentDbContext.SaveChanges();
var result = mapper.Map<Db.Document, Models.MultiLanDocument>(document);
var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, ""));
result.titles = multilan.titles;
result.description = multilan.description;
return (true, result, "Successful");
} }
else else
{ {
@ -198,20 +259,24 @@ namespace DamageAssesment.Api.Documents.Providers
return (false, null, ex.Message); 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 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) if (Document == null)
{ {
return (false, null, "Not Found"); return (false, null, "Not Found");
} }
var result = mapper.Map<Db.Document, Models.MultiLanDocument>(Document);
var multilan = CreateMultiLanguageObject(GetDocumentTranslations(Document.Id, ""));
result.titles = multilan.titles;
result.description = multilan.description;
Document.IsActive = false; Document.IsActive = false;
DocumentDbContext.Documents.Update(Document); DocumentDbContext.Documents.Update(Document);
DocumentDbContext.SaveChanges(); DocumentDbContext.SaveChanges();
return (true, mapper.Map<Db.Document, Models.Document>(Document), $"DocumentId {Id} deleted Successfuly"); return (true, result, $"DocumentId {id} deleted Successfuly");
} }
catch (Exception ex) 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) public async Task<(bool IsSuccess, Models.LinkType LinkType, string ErrorMessage)> GetLinkTypeAsync(int Id)
{ {
try try

View File

@ -37,9 +37,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Employe
EndProject 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}" 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 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 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution