study-lib-backend/API/Controllers/SubjectDeleteRequestsController.cs

76 lines
2.4 KiB
C#
Raw Normal View History

2020-12-20 18:53:24 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2020-12-13 01:23:12 +01:00
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]")]
2020-12-20 18:53:24 +01:00
[Authorize]
2020-12-13 01:23:12 +01:00
[ApiController]
public class SubjectDeleteRequestsController : ControllerBase
{
private readonly StudyLibContext _context;
public SubjectDeleteRequestsController(StudyLibContext context)
{
_context = context;
}
2020-12-23 23:35:08 +01:00
[HttpGet("list-by-group-id/{groupId}")]
public async Task<ActionResult<IEnumerable<SubjectDeleteRequest>>> GetSubjectDeleteRequest(long groupId)
2020-12-13 01:23:12 +01:00
{
2020-12-23 23:35:08 +01:00
return await _context.SubjectDeleteRequests.Where(s => s.Subject.GroupId == groupId).Include(s => s.Subject).ToListAsync();
2020-12-13 01:23:12 +01:00
}
2020-12-23 23:35:08 +01:00
[HttpPost("approve")]
2020-12-13 01:23:12 +01:00
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();
}
2020-12-23 23:35:08 +01:00
[HttpPost("add")]
2020-12-13 01:23:12 +01:00
public async Task<ActionResult<SubjectDeleteRequest>> AddSubjectDeleteRequest(SubjectDeleteRequest deletionRequest)
{
_context.SubjectDeleteRequests.Add(deletionRequest);
await _context.SaveChangesAsync();
return NoContent();
}
2020-12-23 23:35:08 +01:00
[HttpDelete("cancel/{id}")]
2020-12-13 01:23:12 +01:00
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);
}
}
}