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

97 lines
3.2 KiB
C#
Raw Normal View History

2020-12-21 23:31:53 +01:00
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
{
2020-12-30 00:28:50 +01:00
[Route("api/groupCandidates")]
2020-12-21 23:31:53 +01:00
[Authorize]
[ApiController]
public class GroupCandidatesController : ControllerBase
{
private readonly StudyLibContext _context;
public GroupCandidatesController(StudyLibContext context)
{
_context = context;
}
2020-12-23 23:35:08 +01:00
[HttpGet("list/{groupId}")]
2020-12-30 00:28:50 +01:00
public async Task<ActionResult<IEnumerable<User>>> GetGroupCandidates(long groupId)
2020-12-21 23:31:53 +01:00
{
2020-12-30 00:28:50 +01:00
return await _context.GroupCandidates.Where(g => g.Group.ID == groupId).Include(g => g.User).Select(g => g.User).ToListAsync();
2020-12-21 23:31:53 +01:00
}
2020-12-23 23:35:08 +01:00
[HttpPost("join-request")]
2020-12-30 00:28:50 +01:00
public async Task<IActionResult> GroupCandidate(GroupCandidateSaveModel groupCandidateSaveModel)
2020-12-21 23:31:53 +01:00
{
2020-12-30 00:28:50 +01:00
var user = await _context.Users.FindAsync(groupCandidateSaveModel.UserId);
var group = await _context.Groups.FindAsync(groupCandidateSaveModel.GroupId);
var groupCandidate = new GroupCandidate
{
GroupId = groupCandidateSaveModel.GroupId,
Group = group,
UserId = groupCandidateSaveModel.UserId,
User = user
};
2020-12-21 23:31:53 +01:00
_context.GroupCandidates.Add(groupCandidate);
await _context.SaveChangesAsync();
2020-12-30 00:28:50 +01:00
return NoContent();
2020-12-21 23:31:53 +01:00
}
2021-01-03 14:22:26 +01:00
[HttpGet("accept/{groupId}/{userId}")]
public async Task<IActionResult> AcceptGroupCandidate(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);
var user = await _context.Users.FindAsync(userId);
var group = await _context.Groups.Where(g => g.ID == groupId).Include(g => g.Users).FirstAsync();
if (!group.Users.Contains(user))
{
group.Users.Add(user);
_context.Entry(group).State = EntityState.Modified;
}
await _context.SaveChangesAsync();
return NoContent();
}
[HttpDelete("decline/{groupId}/{userId}")]
public async Task<IActionResult> DeclineGroupCandidate(long groupId, string userId)
2020-12-21 23:31:53 +01:00
{
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);
}
}
}