public async Task <ActionResult> AddMemberToGroup(SingleGroupMemberDTO dto) { //Validate DTO if (!dto.Validate()) { return(BadRequest("All fields must contain valid values")); } //Prevent duplicate if ((await _context.GroupAssignments.FindAsync(dto.GroupID, dto.UserUsername)) is not null) { return(Conflict("Assignment already exists")); } if (!await _context.Groups.AnyAsync(group => group.GroupID == dto.GroupID && group.Course.User.Username == GetUsername())) { return(BadRequest("Either group does not exist or the group is owned by another user")); } await _context.GroupAssignments .AddAsync(new GroupAssignment() { GroupID = dto.GroupID, UserUsername = dto.UserUsername.ToLower() }); await _context.SaveChangesAsync(); // ReSharper disable once Mvc.ActionNotResolved return(CreatedAtAction("members", new { groupid = dto.GroupID })); }
public async Task <ActionResult> RemoveMemberFromGroup(SingleGroupMemberDTO dto) { //Validate DTO if (!dto.Validate()) { return(BadRequest("All fields must contain valid values")); } //Ensure Existence var assignment = await _context.GroupAssignments.FindAsync(dto.GroupID, dto.UserUsername); if (assignment is null) { return(NotFound("No Such Assignment")); } if ( !_context.Groups.Any(group => @group.GroupID == dto.GroupID && @group.Course.User.Username == GetUsername()) ) { return(BadRequest("Either group does not exist or the group is not on a course owned by you")); } _context.GroupAssignments.Remove(assignment); await _context.SaveChangesAsync(); return(NoContent()); }