using Azure;
using DamageAssesment.Api.Attachments.Interfaces;
using DamageAssesment.Api.Attachments.Models;
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;
public AttachmentsController(IAttachmentsProvider AttachmentsProvider, IUploadService uploadService)
{
this.AttachmentProvider = AttachmentsProvider;
this.UploadService = uploadService;
}
///
/// Get all attachments.
///
[HttpGet("attachments")]
public async Task GetAttachmentsAsync()
{
var result = await AttachmentProvider.GetAttachmentsAsync();
if (result.IsSuccess)
{
return Ok(result.Attachments);
}
return NoContent();
}
///
/// Get all attachments by attachmentId.
///
[HttpGet("attachments/{id}")]
public async Task 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 UploadAsync(int responseId, int answerId, List postedFile)
//{
// try
// {
// if (postedFile.Count > 0)
// {
// //Upload logic for all files
// var Attachments= await this.AttachmentProvider.DeleteAttachmentsAsync(responseId,answerId);
// List 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}");
// }
//}
///
/// Save new Attachment(s)
///
[HttpPost("attachments"), DisableRequestSizeLimit]
public async Task UploadAttachmentAsync(AttachmentInfo attachmentInfo)
{
try
{
if (attachmentInfo.Answers.Count > 0)
{
var Attachments = await this.AttachmentProvider.GetAttachmentCounter();
List 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}");
}
}
///
/// Modify an new attachment.
///
[HttpPut("attachments"), DisableRequestSizeLimit]
public async Task UpdateAttachmentAsync(AttachmentInfo attachmentInfo)
{
try
{
if (attachmentInfo.Answers.Count > 0)
{
var res = await this.AttachmentProvider.GetAttachmentInfo(attachmentInfo.Answers);
if (res.IsSuccess)
{
List 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}");
}
}
///
/// Delete an existing attachment.
///
[HttpDelete("attachments/{id}")]
public async Task 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();
}
}
}