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
{
[Route("api")]
[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;
}
///
/// Get all documnets.
///
[HttpGet("{language}/{LinkType}/documents")]
public async Task GetDocumentsbyFormsandLanguageAsync(string language,string LinkType)
{
var result = await this.documentsProvider.GetDocumnetsByLinkAsync(language, LinkType);
if (result.IsSuccess)
{
return Ok(result.documents);
}
return NoContent();
}
///
/// Get all documnets.
///
[HttpGet("{LinkType}/documents")]
public async Task GetDocumentsbyFormAsync(string LinkType)
{
var result = await this.documentsProvider.GetDocumnetsByLinkAsync(null, LinkType);
if (result.IsSuccess)
{
return Ok(result.documents);
}
return NoContent();
}
///
/// Get all documnets.
///
[HttpGet("documents")]
public async Task GetDocumentsAsync()
{
var result = await this.documentsProvider.GetDocumnetsAsync();
if (result.IsSuccess)
{
return Ok(result.documents);
}
return NoContent();
}
///
/// Get a documnet by id.
///
[HttpGet("documents/{id}")]
public async Task GetDocumentAsync(int id)
{
var result = await this.documentsProvider.GetDocumentAsync(id);
if (result.IsSuccess)
{
return Ok(result.Document);
}
return NotFound();
}
///
/// Upload new document.
///
[HttpPut("documents")]
public async Task UpdateDocument(DocumentInfo documentInfo)
{
if (documentInfo != null)
{
var dbdoc = await this.documentsProvider.GetDocumentAsync(documentInfo.Id);
if (dbdoc.IsSuccess)
{
Models.Document document = uploadService.UpdateDocuments(dbdoc.Document, documentInfo);
var result = await this.documentsProvider.UpdateDocumentAsync(document);
if (result.IsSuccess)
{
return Ok(result.Document);
}
return NoContent();
}
return NoContent();
}
return BadRequest(documentInfo);
}
///
/// update existing document.
///
[HttpPost("documents")]
public async Task 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}");
}
}
///
/// Delete documnet by id.
///
[HttpDelete("documents/{id}")]
public async Task 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();
}
///
/// Get all document link type.
///
[HttpGet("LinkTypes")]
public async Task GetLinkTypesAsync()
{
var result = await this.documentsProvider.GetLinkTypesAsync();
if (result.IsSuccess)
{
return Ok(result.LinkTypes);
}
return NoContent();
}
///
/// Get a document link type by id.
///
[HttpGet("LinkTypes/{id}")]
public async Task GetLinkTypeAsync(int id)
{
var result = await this.documentsProvider.GetLinkTypeAsync(id);
if (result.IsSuccess)
{
return Ok(result.LinkType);
}
return NotFound();
}
///
/// Update a existing document link type.
///
[HttpPut("LinkTypes")]
public async Task 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);
}
///
/// Create a new document link type.
///
[HttpPost("LinkTypes")]
public async Task 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);
}
///
/// Delete a existing document link type by id.
///
[HttpDelete("LinkTypes/{id}")]
public async Task DeleteLinkType(int id)
{
var result = await this.documentsProvider.DeleteLinkTypeAsync(id);
if (result.IsSuccess)
{
return Ok(result.LinkType);
}
return NotFound();
}
}
}