@ -1,7 +1,7 @@
using System.ComponentModel.DataAnnotations ;
using System.ComponentModel.DataAnnotations ;
using API.Context ;
using API.Context ;
using API.DTO ;
using API.Validation ;
using API.Validation ;
using AppContext.Entities ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.Mvc ;
using Model ;
using Model ;
@ -11,13 +11,20 @@ namespace API.Controllers;
[ApiController]
[ApiController]
[Authorize]
[Authorize]
public class TeamsController ( ITeamService service , ITacticService tactics , IContextAccessor accessor ) : ControllerBase
public class TeamsController (
ITeamService service ,
ITacticService tactics ,
IUserService users ,
IContextAccessor accessor
) : ControllerBase
{
{
public record CreateTeamRequest (
public record CreateTeamRequest (
[Name] string Name ,
[Name] string Name ,
[Url] string Picture ,
[Url] string Picture ,
[RegularExpression("^#[0-9A-F] { 6 } $ ")] string FirstColor,
[RegularExpression("^#[0-9A-Fa-f] { 6 } $ ")]
[RegularExpression("^#[0-9A-F] { 6 } $ ")] string SecondColor
string FirstColor ,
[RegularExpression("^#[0-9A-Fa-f] { 6 } $ ")]
string SecondColor
) ;
) ;
[HttpPost("/teams")]
[HttpPost("/teams")]
@ -29,6 +36,14 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte
return Ok ( team ) ;
return Ok ( team ) ;
}
}
[HttpGet("/teams/{id:int}")]
public async Task < IActionResult > GetTeam ( int id )
{
var team = await service . GetTeam ( id ) ;
return team = = null ? NotFound ( ) : Ok ( team ) ;
}
[HttpGet("/teams/{teamId:int}/members")]
[HttpGet("/teams/{teamId:int}/members")]
public async Task < IActionResult > GetMembersOf ( int teamId )
public async Task < IActionResult > GetMembersOf ( int teamId )
{
{
@ -38,7 +53,16 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte
switch ( accessibility )
switch ( accessibility )
{
{
case ITeamService . TeamAccessibility . Authorized :
case ITeamService . TeamAccessibility . Authorized :
return Ok ( await service . GetMembersOf ( teamId ) ) ;
var members = ( await service . GetMembersOf ( teamId ) ) . ToList ( ) ;
var membersDto = new List < MemberDto > ( ) ;
foreach ( var m in members )
{
membersDto . Add ( new MemberDto ( ( await users . GetUser ( m . UserId ) ) ! , m . Role ) ) ;
}
return Ok ( membersDto ) ;
case ITeamService . TeamAccessibility . NotFound :
case ITeamService . TeamAccessibility . NotFound :
case ITeamService . TeamAccessibility . Unauthorized :
case ITeamService . TeamAccessibility . Unauthorized :
return NotFound ( ) ;
return NotFound ( ) ;
@ -59,7 +83,7 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte
{
{
throw new Exception ( $"Unable to convert string input '{req.Role}' to a role enum variant." ) ;
throw new Exception ( $"Unable to convert string input '{req.Role}' to a role enum variant." ) ;
}
}
var accessibility =
var accessibility =
await service . EnsureAccessibility ( accessor . CurrentUserId ( HttpContext ) , teamId , MemberRole . Coach ) ;
await service . EnsureAccessibility ( accessor . CurrentUserId ( HttpContext ) , teamId , MemberRole . Coach ) ;
@ -111,8 +135,6 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte
default : //unreachable
default : //unreachable
return Problem ( ) ;
return Problem ( ) ;
}
}
}
}
[HttpDelete("/team/{teamId:int}/members/{userId:int}")]
[HttpDelete("/team/{teamId:int}/members/{userId:int}")]
@ -135,12 +157,12 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte
return Problem ( ) ;
return Problem ( ) ;
}
}
}
}
public record ShareTacticToTeamRequest (
public record ShareTacticToTeamRequest (
int TacticId ,
int TacticId ,
int TeamId
int TeamId
) ;
) ;
[HttpPost("/team/share-tactic")]
[HttpPost("/team/share-tactic")]
public async Task < IActionResult > ShareTactic ( [ FromBody ] ShareTacticToTeamRequest sharedTactic )
public async Task < IActionResult > ShareTactic ( [ FromBody ] ShareTacticToTeamRequest sharedTactic )
{
{
@ -149,7 +171,7 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte
return success ? Ok ( ) : BadRequest ( ) ;
return success ? Ok ( ) : BadRequest ( ) ;
}
}
[HttpDelete("/tactics/shared/{tacticId:int}/team/{teamId:int}")]
[HttpDelete("/tactics/shared/{tacticId:int}/team/{teamId:int}")]
public async Task < IActionResult > UnshareTactic ( int tacticId , int teamId )
public async Task < IActionResult > UnshareTactic ( int tacticId , int teamId )
{
{
@ -160,6 +182,7 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte
{
{
return NotFound ( ) ;
return NotFound ( ) ;
}
}
if ( currentUserId ! = tactic . OwnerId )
if ( currentUserId ! = tactic . OwnerId )
{
{
return Unauthorized ( ) ;
return Unauthorized ( ) ;
@ -168,12 +191,12 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte
var success = await tactics . UnshareTactic ( tacticId , null , teamId ) ;
var success = await tactics . UnshareTactic ( tacticId , null , teamId ) ;
return success ? Ok ( ) : NotFound ( ) ;
return success ? Ok ( ) : NotFound ( ) ;
}
}
[HttpGet("/tactics/shared/team/{teamId:int}")]
[HttpGet("/tactics/shared/team/{teamId:int}")]
public async Task < IActionResult > GetSharedTacticsToTeam ( int teamId )
public async Task < IActionResult > GetSharedTacticsToTeam ( int teamId )
{
{
var currentUserId = accessor . CurrentUserId ( HttpContext ) ;
var currentUserId = accessor . CurrentUserId ( HttpContext ) ;
if ( ! await service . IsUserInTeam ( currentUserId , teamId ) )
if ( ! await service . IsUserInTeam ( currentUserId , teamId ) )
{
{
return Unauthorized ( ) ;
return Unauthorized ( ) ;