63 lines
1.9 KiB
C#
63 lines
1.9 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using StudyLib.API.Data;
|
|
using StudyLib.API.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StudyLib.API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[Authorize]
|
|
[ApiController]
|
|
public class GroupCandidatesController : ControllerBase
|
|
{
|
|
private readonly StudyLibContext _context;
|
|
|
|
public GroupCandidatesController(StudyLibContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[HttpGet("{groupId}")]
|
|
public async Task<ActionResult<IEnumerable<GroupCandidate>>> GetGroupCandidates(long groupId)
|
|
{
|
|
return await _context.GroupCandidates.Where(g => g.Group.ID == groupId).ToListAsync();
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<GroupCandidate>> GroupCandidate(GroupCandidate groupCandidate)
|
|
{
|
|
_context.GroupCandidates.Add(groupCandidate);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetGroupCandidate", groupCandidate);
|
|
}
|
|
|
|
[HttpDelete("{groupId}/{userId}")]
|
|
public async Task<IActionResult> DeleteGroupCandidate(long groupId, string userId)
|
|
{
|
|
var groupCandidate = await _context.GroupCandidates.Where(g => g.Group.ID == groupId && g.User.Id == userId).FirstAsync();
|
|
if (groupCandidate == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.GroupCandidates.Remove(groupCandidate);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
private bool GroupCandidateExists(long groupId, string userId)
|
|
{
|
|
return _context.GroupCandidates.Any(g => g.Group.ID == groupId && g.User.Id == userId);
|
|
}
|
|
}
|
|
|
|
}
|