DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Controllers/DoculinkController.cs

268 lines
9.6 KiB
C#
Raw Normal View History

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-09-22 10:52:17 -05:00
public DoculinkController(IDoculinkProvider documentsProvider,IUploadService uploadService)
2023-08-31 18:00:51 -05:00
{
this.documentsProvider = documentsProvider;
this.uploadService = uploadService;
}
/// <summary>
2023-09-22 10:52:17 -05:00
/// Get all Doculink type.
2023-08-31 18:00:51 -05:00
/// </summary>
[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>
[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}")]
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}")]
public async Task<IActionResult> UpdateLinkType(int id,Models.LinkType linkType)
2023-09-13 15:49:59 -05:00
{
if (linkType != null)
{
2023-09-22 10:52:17 -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:20:11 -05:00
/// download an existing attachment.
/// </summary>
[HttpGet("doculinks/download/{id}")]
public async Task<IActionResult> downloadfile1(int id)
{
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);
}
}
/// <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-09-22 10:52:17 -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)
{
var result = await this.documentsProvider.GetdocumentsByLinkAsync(linktype, language,true);
if (result.IsSuccess)
{
return Ok(result.documents);
}
return NoContent();
}
2023-10-09 12:29:01 -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>
[HttpGet]
[Route("doculinks/{id}")]
2023-09-13 15:49:59 -05:00
[Route("doculinks/{id}/{linktype:alpha}")]
[Route("doculinks/{id}/{linktype:alpha}/{language:alpha}")]
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>
[HttpPut]
[Route("doculinks/{id}")]
2023-09-22 10:52:17 -05:00
public async Task<IActionResult> UpdateDocument(int id,ReqDoculink documentInfo)
2023-08-31 18:00:51 -05:00
{
if (documentInfo != null)
{
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();
Models.Doculink DocuLink= uploadService.UpdateDocuments(documents.counter,dbdoc.Document, documentInfo);
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();
}
return NotFound();
2023-08-31 18:00:51 -05:00
}
return BadRequest(documentInfo);
}
/// <summary>
2023-09-22 10:52:17 -05:00
/// Create new doclink.
2023-08-31 18:00:51 -05:00
/// </summary>
[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-09-22 10:52:17 -05:00
Models.Doculink DocuLink= uploadService.UploadDocument(documents.counter, documentInfo);
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>
[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-09-13 15:49:59 -05:00
2023-08-31 18:00:51 -05:00
}
}