API for deleting documents

This commit is contained in:
Lynne Megido 2020-09-20 14:12:41 +10:00
parent e5748ec8d5
commit 107fe6078c
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
2 changed files with 18 additions and 1 deletions

View file

@ -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<ActionResult<bool>> 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;
}
}
}

View file

@ -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)) {