2023-09-22 10:52:17 -05:00
|
|
|
|
using DamageAssesment.Api.DocuLinks.Db;
|
|
|
|
|
using DamageAssesment.Api.DocuLinks.Interfaces;
|
|
|
|
|
using DamageAssesment.Api.DocuLinks.Models;
|
|
|
|
|
using DamageAssesment.Api.DocuLinks.Providers;
|
2023-08-31 18:00:51 -05:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
2023-09-22 10:52:17 -05:00
|
|
|
|
namespace DamageAssesment.Api.DocuLinks.Controllers
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
2023-09-22 10:52:17 -05:00
|
|
|
|
public class DoculinkController : ControllerBase
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
2023-09-22 10:52:17 -05:00
|
|
|
|
private readonly IDoculinkProvider documentsProvider;
|
2023-08-31 18:00:51 -05:00
|
|
|
|
private readonly IUploadService uploadService;
|
|
|
|
|
|
2023-12-04 14:56:13 -05:00
|
|
|
|
public DoculinkController(IDoculinkProvider documentsProvider, IUploadService uploadService)
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
this.documentsProvider = documentsProvider;
|
2023-12-04 14:56:13 -05:00
|
|
|
|
this.uploadService = uploadService;
|
2023-08-31 18:00:51 -05:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Get all Doculink type.
|
2023-08-31 18:00:51 -05:00
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpGet]
|
2023-09-13 15:49:59 -05:00
|
|
|
|
[Route("doculinks/types")]
|
2023-09-22 10:52:17 -05:00
|
|
|
|
[Route("doculinks/types/{language:alpha}")]
|
|
|
|
|
public async Task<IActionResult> GetLinkTypesAsync(string? language)
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
2023-09-22 10:52:17 -05:00
|
|
|
|
var result = await this.documentsProvider.GetLinkTypesAsync(language);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
2023-09-13 15:49:59 -05:00
|
|
|
|
return Ok(result.LinkTypes);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Get a Doculink type by id.
|
2023-08-31 18:00:51 -05:00
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpGet]
|
2023-09-13 15:49:59 -05:00
|
|
|
|
[Route("doculinks/types/{id}")]
|
2023-09-22 10:52:17 -05:00
|
|
|
|
[Route("doculinks/types/{id}/{language:alpha}")]
|
2023-12-04 14:56:13 -05:00
|
|
|
|
public async Task<IActionResult> GetLinkTypeAsync(int id, string? language)
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
2023-09-22 10:52:17 -05:00
|
|
|
|
var result = await this.documentsProvider.GetLinkTypeAsync(id, language);
|
2023-09-13 15:49:59 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.LinkType);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Update a existing Doculink type.
|
2023-09-13 15:49:59 -05:00
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpPut]
|
2023-09-22 10:52:17 -05:00
|
|
|
|
[Route("doculinks/types/{id}")]
|
2023-12-04 14:56:13 -05:00
|
|
|
|
public async Task<IActionResult> UpdateLinkType(int id, Models.LinkType linkType)
|
2023-09-13 15:49:59 -05:00
|
|
|
|
{
|
|
|
|
|
if (linkType != null)
|
|
|
|
|
{
|
2023-12-04 14:56:13 -05:00
|
|
|
|
var result = await this.documentsProvider.UpdateLinkTypeAsync(id, linkType);
|
2023-09-13 15:49:59 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.LinkType);
|
|
|
|
|
}
|
|
|
|
|
if (result.ErrorMessage == "Not Found")
|
|
|
|
|
return NotFound(result.ErrorMessage);
|
|
|
|
|
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
return CreatedAtRoute("DefaultApi", new { id = linkType.Id }, linkType);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Create a new Doculink type.
|
2023-09-13 15:49:59 -05:00
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("doculinks/types")]
|
|
|
|
|
public async Task<IActionResult> CreateLinkType(Models.LinkType linkType)
|
|
|
|
|
{
|
|
|
|
|
if (linkType != null)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.documentsProvider.PostLinkTypeAsync(linkType);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.LinkType);
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
return CreatedAtRoute("DefaultApi", new { id = linkType.Id }, linkType);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Delete a existing Doculink type by id.
|
2023-09-13 15:49:59 -05:00
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
[Route("doculinks/types/{id}")]
|
|
|
|
|
public async Task<IActionResult> DeleteLinkType(int id)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.documentsProvider.DeleteLinkTypeAsync(id);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.LinkType);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// <summary>
|
2023-12-01 17:22:49 -05:00
|
|
|
|
/// download an existing attachment.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpGet("doculinks/download/{id}")]
|
2023-12-04 14:56:13 -05:00
|
|
|
|
public async Task<IActionResult> downloadfile(int id)
|
2023-12-01 17:22:49 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = await this.documentsProvider.GetDownloadAttachmentAsync(id);
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
return NotFound();
|
2023-12-04 14:56:13 -05:00
|
|
|
|
string path = await uploadService.GetFile(result.DoculinkAttachments.Path);
|
|
|
|
|
if (path == null)
|
2023-12-01 17:22:49 -05:00
|
|
|
|
return NotFound();
|
2023-12-04 14:56:13 -05:00
|
|
|
|
var contentType = GetContentType(result.DoculinkAttachments.docName);
|
|
|
|
|
if (contentType == "application/octet-stream")
|
|
|
|
|
return PhysicalFile(path, contentType, result.DoculinkAttachments.docName);
|
|
|
|
|
return PhysicalFile(path, contentType, enableRangeProcessing: true);
|
2023-12-01 17:22:49 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Handle the exception here or log it
|
|
|
|
|
return StatusCode(500, "An error occurred: " + ex.Message);
|
|
|
|
|
}
|
2023-12-04 14:56:13 -05:00
|
|
|
|
//try
|
|
|
|
|
//{
|
|
|
|
|
// var result = await this.documentsProvider.GetDownloadAttachmentAsync(id);
|
|
|
|
|
// if (!result.IsSuccess)
|
|
|
|
|
// return NotFound();
|
|
|
|
|
// byte[] fileContent = await uploadService.DownloadFile(result.DoculinkAttachments.Path);
|
|
|
|
|
// if (fileContent == null || fileContent.Length == 0)
|
|
|
|
|
// return NotFound();
|
|
|
|
|
// var contentType = "application/octet-stream";
|
|
|
|
|
// return File(fileContent, contentType, result.DoculinkAttachments.docName);
|
|
|
|
|
//}
|
|
|
|
|
//catch (Exception ex)
|
|
|
|
|
//{
|
|
|
|
|
// // Handle the exception here or log it
|
|
|
|
|
// return StatusCode(500, "An error occurred: " + ex.Message);
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
private string GetContentType(string fileName)
|
|
|
|
|
{
|
|
|
|
|
// You can add more content types based on the file extensions
|
|
|
|
|
switch (Path.GetExtension(fileName).ToLower())
|
|
|
|
|
{
|
|
|
|
|
//case ".txt":
|
|
|
|
|
// return "text/plain";
|
|
|
|
|
case ".jpg":
|
|
|
|
|
case ".jpeg":
|
|
|
|
|
return "image/jpeg";
|
|
|
|
|
case ".png":
|
|
|
|
|
return "image/png";
|
|
|
|
|
case ".gif":
|
|
|
|
|
return "image/gif";
|
|
|
|
|
case ".bmp":
|
|
|
|
|
return "image/bmp";
|
|
|
|
|
case ".webp":
|
|
|
|
|
return "image/webp";
|
|
|
|
|
case ".csv":
|
|
|
|
|
return "text/csv";
|
|
|
|
|
case ".pdf":
|
|
|
|
|
return "application/pdf";
|
|
|
|
|
case ".docx":
|
|
|
|
|
case ".doc":
|
|
|
|
|
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
|
|
|
case ".xlsx":
|
|
|
|
|
case ".xls":
|
|
|
|
|
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
|
|
|
// Add more cases as needed
|
|
|
|
|
default:
|
|
|
|
|
return "application/octet-stream";
|
|
|
|
|
}
|
2023-12-01 17:22:49 -05:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Get all Doculink.
|
|
|
|
|
/// </summary>
|
|
|
|
|
///
|
|
|
|
|
[Route("doculinks")]
|
2023-09-13 15:49:59 -05:00
|
|
|
|
[Route("doculinks/{linktype:alpha}")]
|
|
|
|
|
[Route("doculinks/{linktype:alpha}/{language:alpha}")]
|
|
|
|
|
[HttpGet]
|
2023-12-04 14:56:13 -05:00
|
|
|
|
public async Task<IActionResult> GetDocumentsAsync(string? linktype, string? language, bool? isactive)
|
2023-09-13 15:49:59 -05:00
|
|
|
|
{
|
2023-09-22 10:52:17 -05:00
|
|
|
|
var result = await this.documentsProvider.GetdocumentsByLinkAsync(linktype, language, isactive);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.documents);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2023-09-13 15:49:59 -05:00
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Get all active Doculink.
|
2023-09-13 15:49:59 -05:00
|
|
|
|
/// </summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
[Route("doculinks/active")]
|
|
|
|
|
[Route("doculinks/active/{linktype:alpha}")]
|
|
|
|
|
[Route("doculinks/active/{linktype:alpha}/{language:alpha}")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> GetDocumentsByActiveAsync(string? linktype, string? language)
|
|
|
|
|
{
|
2023-12-04 14:56:13 -05:00
|
|
|
|
var result = await this.documentsProvider.GetdocumentsByLinkAsync(linktype, language, true);
|
2023-09-22 10:52:17 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.documents);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2023-10-09 19:38:35 -05:00
|
|
|
|
/// <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();
|
|
|
|
|
}
|
2023-08-31 18:00:51 -05:00
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Get a Doculink by id.
|
2023-08-31 18:00:51 -05:00
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("doculinks/{id}")]
|
2023-09-13 15:49:59 -05:00
|
|
|
|
[Route("doculinks/{id}/{linktype:alpha}")]
|
|
|
|
|
[Route("doculinks/{id}/{linktype:alpha}/{language:alpha}")]
|
2023-12-04 14:56:13 -05:00
|
|
|
|
public async Task<IActionResult> GetDocumentAsync(int id, string? linktype, string? language)
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
2023-09-13 15:49:59 -05:00
|
|
|
|
var result = await this.documentsProvider.GetDocumentAsync(id, linktype, language);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Document);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// update existing doclink.
|
2023-08-31 18:00:51 -05:00
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpPut]
|
|
|
|
|
[Route("doculinks/{id}")]
|
2023-12-04 14:56:13 -05:00
|
|
|
|
public async Task<IActionResult> UpdateDocument(int id, ReqDoculink documentInfo)
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
|
|
|
|
if (documentInfo != null)
|
|
|
|
|
{
|
2023-09-11 12:23:50 -05:00
|
|
|
|
var dbdoc = await this.documentsProvider.GetDocumentByidAsync(id);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
if (dbdoc.IsSuccess)
|
|
|
|
|
{
|
2023-09-22 10:52:17 -05:00
|
|
|
|
var documents = await this.documentsProvider.GetDocumentCounter();
|
2023-12-04 14:56:13 -05:00
|
|
|
|
Models.Doculink DocuLink = uploadService.UpdateDocuments(documents.counter, dbdoc.Document, documentInfo);
|
2023-09-22 10:52:17 -05:00
|
|
|
|
var result = await this.documentsProvider.UpdateDocumentAsync(id, DocuLink);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Document);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2023-09-11 12:23:50 -05:00
|
|
|
|
return NotFound();
|
2023-08-31 18:00:51 -05:00
|
|
|
|
}
|
|
|
|
|
return BadRequest(documentInfo);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-12-04 14:56:13 -05:00
|
|
|
|
/// update existing doclink isactive field.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpPut]
|
|
|
|
|
[Route("doculinks/{id}/{isactive}")]
|
|
|
|
|
public async Task<IActionResult> UpdateIsActiveDocument(int id, bool isactive)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.documentsProvider.UpdateDocumentAsync(id, isactive);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Document);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Create new doclink.
|
2023-08-31 18:00:51 -05:00
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("doculinks")]
|
2023-09-22 10:52:17 -05:00
|
|
|
|
public async Task<IActionResult> CreateDocument(ReqDoculink documentInfo)
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (documentInfo != null)
|
|
|
|
|
{
|
|
|
|
|
var documents = await this.documentsProvider.GetDocumentCounter();
|
2023-12-04 14:56:13 -05:00
|
|
|
|
Models.Doculink DocuLink = uploadService.UploadDocument(documents.counter, documentInfo);
|
2023-09-22 10:52:17 -05:00
|
|
|
|
var result = await this.documentsProvider.PostDocumentAsync(DocuLink);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Document);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(documentInfo);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest($"Internal server error: {ex}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2023-09-22 10:52:17 -05:00
|
|
|
|
/// Delete Doculink by id.
|
2023-08-31 18:00:51 -05:00
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpDelete]
|
|
|
|
|
[Route("doculinks/{id}")]
|
2023-08-31 18:00:51 -05:00
|
|
|
|
public async Task<IActionResult> DeleteDocument(int id)
|
|
|
|
|
{
|
|
|
|
|
// database soft delete
|
|
|
|
|
var result = await this.documentsProvider.DeleteDocumentAsync(id);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
// deleting file from folder
|
2023-09-22 10:52:17 -05:00
|
|
|
|
foreach (var item in result.Document.doclinksAttachments)
|
|
|
|
|
{
|
|
|
|
|
uploadService.Movefile(item.Path);
|
|
|
|
|
}
|
2023-08-31 18:00:51 -05:00
|
|
|
|
return Ok(result.Document);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2023-12-04 14:56:13 -05:00
|
|
|
|
|
2023-08-31 18:00:51 -05:00
|
|
|
|
}
|
|
|
|
|
}
|