diff --git a/Controllers/DocumentController.cs b/Controllers/DocumentController.cs index e13cf1d..ec655c7 100644 --- a/Controllers/DocumentController.cs +++ b/Controllers/DocumentController.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using System.IO; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Snootalogue.Models; @@ -30,5 +31,21 @@ namespace Snootalogue.Controllers { return document; } + + [HttpDelete("{id}")] + public async Task> DeleteDocument(string id) { + var document = await _context.Document.FindAsync(id); + if (document == null) { + return NotFound(); + } + + var uploadDirectory = Path.Combine("wwwroot", "Content"); + var destination = Path.Combine(uploadDirectory, $"{id}.pdf"); + System.IO.File.Delete(destination); + + _context.Remove(document); + await _context.SaveChangesAsync(); + return true; + } } } diff --git a/Pages/Upload.cshtml.cs b/Pages/Upload.cshtml.cs index ca186ff..ed7131b 100644 --- a/Pages/Upload.cshtml.cs +++ b/Pages/Upload.cshtml.cs @@ -34,7 +34,7 @@ namespace Snootalogue.Pages { var id = Document.NewID(_context); // var uploadDirectory = Path.Combine(filename); - var uploadDirectory = "wwwroot/Content"; + var uploadDirectory = Path.Combine("wwwroot", "Content"); var destination = Path.Combine(uploadDirectory, $"{id}.pdf"); using (var fs = new FileStream(destination, FileMode.Create)) {