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; } [HttpGet("list")] public async Task>> GetGroups() { 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; } [HttpGet("current-user-groups/{userId}")] public async Task>> GetCurrentUserGroups(string userId) { var user = await _context.Users.Where(u => u.Id == userId).Include(u => u.Groups).SingleOrDefaultAsync(); if (user == null) { return NoContent(); } 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 })); } [HttpGet("get-by-id/{id}")] public async Task> GetGroup(long id) { var group = await _context.Groups.Where(g => g.ID == id).Include(g => g.Users).Include(g => g.Subjects).Include(g => g.GroupCandidates).SingleOrDefaultAsync(); if (group == null) { return NotFound(); } 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; } [HttpPut("update/{id}")] public async Task 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(); } [HttpGet("leave/{id}/{userId}")] public async Task LeaveGroup(long id, string userId) { var user = await _context.Users.Where(u => u.Id == userId).Include(u => u.Groups).SingleOrDefaultAsync(); 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")] public async Task> PostGroup(Group group) { var admin = await _context.Users.FindAsync(group.AdminId); group.Users = new List{ admin }; _context.Groups.Add(group); await _context.SaveChangesAsync(); return CreatedAtAction("GetGroup", new { id = group.ID }, group); } [HttpDelete("delete/{id}")] public async Task> 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); } } }