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

183 lines
5.2 KiB
C#
Raw Normal View History

2020-12-21 23:31:53 +01:00
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]")]
[ApiController]
public class GroupsController : ControllerBase
{
private readonly StudyLibContext _context;
public GroupsController(StudyLibContext context)
{
_context = context;
}
2020-12-23 23:35:08 +01:00
[HttpGet("list")]
2020-12-29 00:18:53 +01:00
public async Task<ActionResult<IEnumerable<GroupViewModel>>> GetGroups()
2020-12-21 23:31:53 +01:00
{
2020-12-29 00:18:53 +01:00
var groups = await _context.Groups.Include(g => g.Users).Include(g => g.Subjects).Select(g => new GroupViewModel
{
ID = g.ID,
Name = g.Name,
Year = g.Year,
Admin = _context.Users.Where(u => u.Id == g.AdminId).FirstOrDefault(),
Users = g.Users,
GroupCandidates = g.GroupCandidates,
Subjects = g.Subjects
}).ToListAsync();
return groups;
2020-12-21 23:31:53 +01:00
}
2020-12-23 23:35:08 +01:00
[HttpGet("current-user-groups/{userId}")]
public async Task<ActionResult<IEnumerable<Group>>> GetCurrentUserGroups(string userId)
{
2020-12-29 00:18:53 +01:00
var user = await _context.Users.Where(u => u.Id == userId).Include(u => u.Groups).SingleOrDefaultAsync();
2020-12-23 23:35:08 +01:00
if (user == null)
{
return NoContent();
}
2020-12-29 00:18:53 +01:00
return Ok(user.Groups.Select(g => new GroupViewModel
{
ID = g.ID,
Name = g.Name,
Year = g.Year,
Admin = _context.Users.Where(u => u.Id == g.AdminId).FirstOrDefault(),
Users = g.Users,
GroupCandidates = g.GroupCandidates,
Subjects = g.Subjects
}));
2020-12-23 23:35:08 +01:00
}
[HttpGet("get-by-id/{id}")]
2020-12-29 00:18:53 +01:00
public async Task<ActionResult<GroupViewModel>> GetGroup(long id)
2020-12-21 23:31:53 +01:00
{
2020-12-30 00:28:50 +01:00
var group = await _context.Groups.Where(g => g.ID == id).Include(g => g.Users).Include(g => g.Subjects).Include(g => g.GroupCandidates).SingleOrDefaultAsync();
2020-12-21 23:31:53 +01:00
if (group == null)
{
return NotFound();
}
2020-12-29 00:18:53 +01:00
var admin = await _context.Users.Where(u => u.Id == group.AdminId).SingleOrDefaultAsync();
var groupViewModel = new GroupViewModel
{
ID = group.ID,
Name = group.Name,
Year = group.Year,
Admin = admin,
Users = group.Users,
GroupCandidates = group.GroupCandidates,
Subjects = group.Subjects
};
return groupViewModel;
2020-12-21 23:31:53 +01:00
}
2020-12-23 23:35:08 +01:00
[HttpPut("update/{id}")]
2020-12-21 23:31:53 +01:00
public async Task<IActionResult> PutGroup(long id, Group group)
{
if (id != group.ID)
{
return BadRequest();
}
_context.Entry(group).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!GroupExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
2020-12-23 23:35:08 +01:00
[HttpGet("leave/{id}/{userId}")]
public async Task<IActionResult> LeaveGroup(long id, string userId)
{
2021-01-03 23:31:11 +01:00
var user = await _context.Users.Where(u => u.Id == userId).Include(u => u.Groups).SingleOrDefaultAsync();
2020-12-23 23:35:08 +01:00
var groups = user.Groups.Where(g => g.ID != id).ToList();
user.Groups = groups;
_context.Entry(user).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!GroupExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
[HttpPost("add")]
2020-12-21 23:31:53 +01:00
public async Task<ActionResult<Group>> PostGroup(Group group)
{
2020-12-29 00:18:53 +01:00
var admin = await _context.Users.FindAsync(group.AdminId);
group.Users = new List<User>{ admin };
2020-12-21 23:31:53 +01:00
_context.Groups.Add(group);
await _context.SaveChangesAsync();
return CreatedAtAction("GetGroup", new { id = group.ID }, group);
}
2020-12-23 23:35:08 +01:00
[HttpDelete("delete/{id}")]
2020-12-21 23:31:53 +01:00
public async Task<ActionResult<Group>> DeleteGroup(long id)
{
var group = await _context.Groups.FindAsync(id);
if (group == null)
{
return NotFound();
}
_context.Groups.Remove(group);
await _context.SaveChangesAsync();
return group;
}
private bool GroupExists(long id)
{
return _context.Groups.Any(g => g.ID == id);
}
}
}