2023-08-31 18:00:51 -05:00
|
|
|
|
using DamageAssesment.Api.Documents.Db;
|
|
|
|
|
using DamageAssesment.Api.Documents.Interfaces;
|
|
|
|
|
using DamageAssesment.Api.Documents.Models;
|
|
|
|
|
using DamageAssesment.Api.Documents.Providers;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace DamageAssesment.Api.Documents.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class DocumentsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IDocumentsProvider documentsProvider;
|
|
|
|
|
private readonly IUploadService uploadService;
|
|
|
|
|
|
|
|
|
|
public DocumentsController(IDocumentsProvider documentsProvider,IUploadService uploadService)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
this.documentsProvider = documentsProvider;
|
|
|
|
|
this.uploadService = uploadService;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get all documnets.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
///
|
|
|
|
|
[Route("doculinks/{linktype:alpha}")]
|
|
|
|
|
[Route("doculinks/{linktype:alpha}/{language:alpha}")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> GetDocumentsbyFormsandLanguageAsync(string linktype, string? language)
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
2023-09-11 12:23:50 -05:00
|
|
|
|
var result = await this.documentsProvider.GetDocumnetsByLinkAsync(language, linktype);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.documents);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get all documnets.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
///
|
|
|
|
|
//[Route("doculinks/{language:alpha}")]
|
|
|
|
|
[Route("doculinks")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> GetDocumentsAsync(string? language)
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
2023-09-11 12:23:50 -05:00
|
|
|
|
var result = await this.documentsProvider.GetDocumnetsAsync(language);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.documents);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get a documnet by id.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("doculinks/{id}")]
|
|
|
|
|
[Route("doculinks/{id}/{language:alpha}")]
|
|
|
|
|
public async Task<IActionResult> GetDocumentAsync(int id,string? language)
|
2023-08-31 18:00:51 -05:00
|
|
|
|
{
|
2023-09-11 12:23:50 -05:00
|
|
|
|
var result = await this.documentsProvider.GetDocumentAsync(id,language);
|
2023-08-31 18:00:51 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Document);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Upload new document.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpPut]
|
|
|
|
|
[Route("doculinks/{id}")]
|
|
|
|
|
public async Task<IActionResult> UpdateDocument(int id,DocumentInfo 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)
|
|
|
|
|
{
|
|
|
|
|
Models.Document document = uploadService.UpdateDocuments(dbdoc.Document, documentInfo);
|
2023-09-11 12:23:50 -05:00
|
|
|
|
var result = await this.documentsProvider.UpdateDocumentAsync(id,document);
|
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>
|
|
|
|
|
/// update existing document.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("doculinks")]
|
2023-08-31 18:00:51 -05:00
|
|
|
|
public async Task<IActionResult> CreateDocument(DocumentInfo documentInfo)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (documentInfo != null)
|
|
|
|
|
{
|
|
|
|
|
var documents = await this.documentsProvider.GetDocumentCounter();
|
|
|
|
|
Models.Document document = uploadService.UploadDocument(documents.counter, documentInfo);
|
|
|
|
|
var result = await this.documentsProvider.PostDocumentAsync(document);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Document);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(documentInfo);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest($"Internal server error: {ex}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Delete documnet by id.
|
|
|
|
|
/// </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
|
|
|
|
|
uploadService.Movefile(result.Document.Path);
|
|
|
|
|
return Ok(result.Document);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get all document link type.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("doculinks/types")]
|
2023-08-31 18:00:51 -05:00
|
|
|
|
public async Task<IActionResult> GetLinkTypesAsync()
|
|
|
|
|
{
|
|
|
|
|
var result = await this.documentsProvider.GetLinkTypesAsync();
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.LinkTypes);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get a document link type by id.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("doculinks/types/{id}")]
|
2023-08-31 18:00:51 -05:00
|
|
|
|
public async Task<IActionResult> GetLinkTypeAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.documentsProvider.GetLinkTypeAsync(id);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.LinkType);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update a existing document link type.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpPut]
|
|
|
|
|
[Route("doculinks/types")]
|
2023-08-31 18:00:51 -05:00
|
|
|
|
public async Task<IActionResult> UpdateLinkType(Models.LinkType linkType)
|
|
|
|
|
{
|
|
|
|
|
if (linkType != null)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.documentsProvider.UpdateLinkTypeAsync(linkType);
|
|
|
|
|
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>
|
|
|
|
|
/// Create a new document link type.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("doculinks/types")]
|
2023-08-31 18:00:51 -05:00
|
|
|
|
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>
|
|
|
|
|
/// Delete a existing document link type by id.
|
|
|
|
|
/// </summary>
|
2023-09-11 12:23:50 -05:00
|
|
|
|
[HttpDelete]
|
|
|
|
|
[Route("doculinks/types/{id}")]
|
2023-08-31 18:00:51 -05:00
|
|
|
|
public async Task<IActionResult> DeleteLinkType(int id)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.documentsProvider.DeleteLinkTypeAsync(id);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.LinkType);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|