185 lines
6.8 KiB
C#
185 lines
6.8 KiB
C#
using Azure;
|
|
using DamageAssesment.Api.Attachments.Interfaces;
|
|
using DamageAssesment.Api.Attachments.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Net.Http.Headers;
|
|
|
|
namespace DamageAssesment.Api.Attachments.Controllers
|
|
{
|
|
[ApiController]
|
|
public class AttachmentsController : ControllerBase
|
|
{
|
|
private IAttachmentsProvider AttachmentProvider;
|
|
private IUploadService UploadService;
|
|
private IAzureBlobService azureBlobService;
|
|
|
|
public AttachmentsController(IAttachmentsProvider AttachmentsProvider, IUploadService UploadService)
|
|
{
|
|
this.AttachmentProvider = AttachmentsProvider;
|
|
this.UploadService = UploadService;
|
|
}
|
|
/// <summary>
|
|
/// Get all attachments.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpGet("attachments")]
|
|
public async Task<ActionResult> GetAttachmentsAsync()
|
|
{
|
|
|
|
var result = await AttachmentProvider.GetAttachmentsAsync();
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Attachments);
|
|
}
|
|
return NoContent();
|
|
|
|
}
|
|
/// <summary>
|
|
/// Get all attachments by attachmentId.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpGet("attachments/{id}")]
|
|
public async Task<ActionResult> GetAttachmentbyIdAsync(int id)
|
|
{
|
|
|
|
var result = await AttachmentProvider.GetAttachmentByIdAsync(id);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Attachment);
|
|
}
|
|
return NotFound();
|
|
|
|
}
|
|
////Save new Attachment
|
|
//[HttpPost("Attachments"), DisableRequestSizeLimit]
|
|
//public async Task<IActionResult> UploadAsync(int responseId, int answerId, List<IFormFile> postedFile)
|
|
//{
|
|
// try
|
|
// {
|
|
|
|
// if (postedFile.Count > 0)
|
|
// {
|
|
// //Upload logic for all files
|
|
// var Attachments= await this.AttachmentProvider.DeleteAttachmentsAsync(responseId,answerId);
|
|
// List<Db.Attachment> attachments = UploadService.UploadAttachment(responseId, answerId, Attachments.counter, postedFile);
|
|
// //inserting all uploaded files in database
|
|
// var result = await this.AttachmentProvider.PostAttachmentAsync(attachments);
|
|
// if (result.IsSuccess)
|
|
// {
|
|
// return Ok(result.Attachments);
|
|
// }
|
|
// return BadRequest(result.ErrorMessage);
|
|
// }
|
|
// return NoContent();
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// return BadRequest($"Internal server error: {ex}");
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// Save new Attachment(s)
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpPost("attachments"), DisableRequestSizeLimit]
|
|
public async Task<IActionResult> UploadAttachmentAsync(AttachmentInfo attachmentInfo)
|
|
{
|
|
try
|
|
{
|
|
if (attachmentInfo.Answers.Count > 0)
|
|
{
|
|
var Attachments = await this.AttachmentProvider.GetAttachmentCounter();
|
|
List<Models.Attachment> attachments = UploadService.UploadAttachment(attachmentInfo.ResponseId, Attachments.counter, attachmentInfo.Answers);
|
|
var result = await this.AttachmentProvider.PostAttachmentAsync(attachments);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Attachments);
|
|
}
|
|
return NoContent();
|
|
}
|
|
return BadRequest();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest($"Internal server error: {ex}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Modify an new attachment.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpPut("attachments"), DisableRequestSizeLimit]
|
|
public async Task<IActionResult> UpdateAttachmentAsync(AttachmentInfo attachmentInfo)
|
|
{
|
|
try
|
|
{
|
|
if (attachmentInfo.Answers.Count > 0)
|
|
{
|
|
var res = await this.AttachmentProvider.GetAttachmentInfo(attachmentInfo.Answers);
|
|
if (res.IsSuccess)
|
|
{
|
|
List<Models.Attachment> attachments = UploadService.UpdateAttachments(attachmentInfo.ResponseId, attachmentInfo.Answers, res.Attachments);
|
|
var result = await this.AttachmentProvider.PutAttachmentAsync(attachments);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Attachments);
|
|
}
|
|
return NoContent();
|
|
}
|
|
return NoContent();
|
|
}
|
|
return BadRequest();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest($"Internal server error: {ex}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// download an existing attachment.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpGet("attachments/download/{id}")]
|
|
public async Task<IActionResult> downloadfile1(int id)
|
|
{
|
|
try
|
|
{
|
|
var result = await this.AttachmentProvider.GetDownloadAttachmentAsync(id);
|
|
if(!result.IsSuccess)
|
|
return NotFound();
|
|
byte[] fileContent = await UploadService.DownloadFile(result.Attachment.URI);
|
|
if (fileContent == null || fileContent.Length == 0)
|
|
return NotFound();
|
|
var contentType = "application/octet-stream";
|
|
return File(fileContent, contentType, result.Attachment.FileName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Handle the exception here or log it
|
|
return StatusCode(500, "An error occurred: " + ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Delete an existing attachment.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpDelete("attachments/{id}")]
|
|
public async Task<IActionResult> DeleteAttachment(int id)
|
|
{
|
|
// database soft delete
|
|
var result = await this.AttachmentProvider.DeleteAttachmentAsync(id);
|
|
if (result.IsSuccess)
|
|
{
|
|
// deleting file from folder
|
|
UploadService.Movefile(result.Attachment.URI);
|
|
return Ok(result.Attachment);
|
|
}
|
|
return NotFound();
|
|
}
|
|
|
|
}
|
|
}
|