52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Snootalogue.Models;
|
|
using Snootalogue.Data;
|
|
|
|
namespace Snootalogue.Controllers {
|
|
[Route("/api/[controller]")]
|
|
[ApiController]
|
|
public class DocumentController : ControllerBase {
|
|
private readonly SnootalogueContext _context;
|
|
public DocumentController(SnootalogueContext context) {
|
|
_context = context;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<Document>>> GetDocuments() {
|
|
return await _context.Document.Take(25).ToListAsync();
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Document>> GetDocumentById(string id) {
|
|
var document = await _context.Document.FindAsync(id);
|
|
|
|
if (document == null) {
|
|
return NotFound();
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|