Download ContactManagementAPI/Controllers/DocumentController.cs from abrahamcbe/contact-management-system: direct link, hf CLI and curl.
- Browser
- Download file 3.8 kB
-
https://huggingface.co/spaces/abrahamcbe/contact-management-system/resolve/main/ContactManagementAPI/Controllers/DocumentController.cs
- Command line
-
hf download hf://spaces/abrahamcbe/contact-management-system/ContactManagementAPI/Controllers/DocumentController.cs
-
curl -L -o DocumentController.cs https://huggingface.co/spaces/abrahamcbe/contact-management-system/resolve/main/ContactManagementAPI/Controllers/DocumentController.cs
3.8 kB
| 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 | |
| [] | |
| 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 | |
| [] | |
| [] | |
| 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 | |
| [] | |
| [] | |
| 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 | |
| [] | |
| 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); | |
| } | |
| } | |
| } | |