using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using StudyLib.API.Data; using StudyLib.API.Models; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace StudyLib.API.Controllers { [Route("api/[controller]")] [Authorize] [ApiController] public class SubjectDeleteRequestsController : ControllerBase { private readonly StudyLibContext _context; public SubjectDeleteRequestsController(StudyLibContext context) { _context = context; } [HttpGet("list")] public async Task>> GetSubjectDeleteRequest() { return await _context.SubjectDeleteRequests.Include(s => s.Subject).ToListAsync(); } [HttpPost("approveDeletion")] public async Task> ApproveSubjectDeleteRequest(SubjectDeleteRequest deletionRequest) { var subjectDeleteRequest = await _context.SubjectDeleteRequests.Where(s => s.ID == deletionRequest.ID).Include(s => s.Subject).SingleOrDefaultAsync(); if (subjectDeleteRequest == null) { return NotFound(); } _context.Subjects.Remove(subjectDeleteRequest.Subject); await _context.SaveChangesAsync(); return NoContent(); } [HttpPost("addDeletionRequest")] public async Task> AddSubjectDeleteRequest(SubjectDeleteRequest deletionRequest) { _context.SubjectDeleteRequests.Add(deletionRequest); await _context.SaveChangesAsync(); return NoContent(); } [HttpDelete("cancelDeletion/{id}")] public async Task CancelSubjectDeleteRequest(long id) { var subjectDeleteRequest = await _context.SubjectDeleteRequests.FindAsync(id); if (subjectDeleteRequest == null) { return NotFound(); } _context.SubjectDeleteRequests.Remove(subjectDeleteRequest); await _context.SaveChangesAsync(); return NoContent(); } private bool SubjectDeleteRequestExists(long id) { return _context.SubjectDeleteRequests.Any(e => e.ID == id); } } }