study-lib-backend/API/Controllers/SubjectDeleteRequestsController.cs
2020-12-20 18:53:24 +01:00

76 lines
2.4 KiB
C#

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<ActionResult<IEnumerable<SubjectDeleteRequest>>> GetSubjectDeleteRequest()
{
return await _context.SubjectDeleteRequests.Include(s => s.Subject).ToListAsync();
}
[HttpPost("approveDeletion")]
public async Task<ActionResult<SubjectDeleteRequest>> 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<ActionResult<SubjectDeleteRequest>> AddSubjectDeleteRequest(SubjectDeleteRequest deletionRequest)
{
_context.SubjectDeleteRequests.Add(deletionRequest);
await _context.SaveChangesAsync();
return NoContent();
}
[HttpDelete("cancelDeletion/{id}")]
public async Task<IActionResult> 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);
}
}
}