File size: 3,803 Bytes
fc06b79 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ContactManagementAPI.Data;
using ContactManagementAPI.Models;
using ContactManagementAPI.Services;
using ContactManagementAPI.Security;
namespace ContactManagementAPI.Controllers
{
public class DocumentController : Controller
{
private readonly ApplicationDbContext _context;
private readonly FileUploadService _fileUploadService;
public DocumentController(ApplicationDbContext context, FileUploadService fileUploadService)
{
_context = context;
_fileUploadService = fileUploadService;
}
// GET: Document/List/5
[RequireRight(RightsCatalog.ContactsView)]
public async Task<IActionResult> List(int? id)
{
if (id == null)
return NotFound();
var contact = await _context.Contacts
.Include(c => c.Documents)
.FirstOrDefaultAsync(c => c.Id == id);
if (contact == null)
return NotFound();
return View(contact);
}
// POST: Document/Upload
[HttpPost]
[RequireRight(RightsCatalog.DocumentsManage)]
public async Task<IActionResult> Upload(int contactId, IFormFile documentFile, string documentType = "Other")
{
var contact = await _context.Contacts.FindAsync(contactId);
if (contact == null)
return Json(new { success = false, message = "Contact not found" });
var (success, filePath, errorMessage) = await _fileUploadService.UploadDocumentAsync(documentFile, contactId);
if (!success)
return Json(new { success = false, message = errorMessage });
var document = new ContactDocument
{
ContactId = contactId,
DocumentPath = filePath,
FileName = documentFile.FileName,
FileSize = documentFile.Length,
ContentType = documentFile.ContentType,
DocumentType = documentType,
UploadedAt = DateTime.Now
};
_context.ContactDocuments.Add(document);
await _context.SaveChangesAsync();
return Json(new { success = true, documentId = document.Id, documentPath = filePath });
}
// POST: Document/Delete
[HttpPost]
[RequireRight(RightsCatalog.DocumentsManage)]
public async Task<IActionResult> Delete(int id, int contactId)
{
var document = await _context.ContactDocuments.FirstOrDefaultAsync(d => d.Id == id && d.ContactId == contactId);
if (document == null)
return Json(new { success = false, message = "Document not found" });
_fileUploadService.DeleteFile(document.DocumentPath);
_context.ContactDocuments.Remove(document);
await _context.SaveChangesAsync();
return Json(new { success = true, message = "Document deleted" });
}
// GET: Document/Download
[RequireRight(RightsCatalog.ContactsView)]
public async Task<IActionResult> Download(int id, int contactId)
{
var document = await _context.ContactDocuments.FirstOrDefaultAsync(d => d.Id == id && d.ContactId == contactId);
if (document == null)
return NotFound();
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", document.DocumentPath.TrimStart('/'));
if (!System.IO.File.Exists(filePath))
return NotFound();
var fileBytes = System.IO.File.ReadAllBytes(filePath);
return File(fileBytes, document.ContentType, document.FileName);
}
}
}
|