using System.ComponentModel.DataAnnotations; using API.Context; using API.Validation; using AppContext.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Model; using Services; namespace API.Controllers; [ApiController] [Authorize] public class TeamsController(ITeamService service, ITacticService tactics,IContextAccessor accessor) : ControllerBase { public record CreateTeamRequest( [Name] string Name, [Url] string Picture, [RegularExpression("^#[0-9A-F]{6}$")] string FirstColor, [RegularExpression("^#[0-9A-F]{6}$")] string SecondColor ); [HttpPost("/teams")] public async Task CreateTeam([FromBody] CreateTeamRequest req) { var userId = accessor.CurrentUserId(HttpContext); var team = await service.AddTeam(req.Name, req.Picture, req.FirstColor, req.SecondColor); await service.AddMember(team.Id, userId, MemberRole.Coach); return Ok(team); } [HttpGet("/teams/{teamId:int}/members")] public IActionResult GetMembersOf(int teamId) { return Ok(service.GetMembersOf(teamId)); } public record AddMemberRequest( int UserId, [AllowedValues("PLAYER", "COACH")] string Role ); [HttpPost("/teams/{teamId:int}/members")] public async Task AddMember(int teamId, [FromBody] AddMemberRequest req) { if (!Enum.TryParse(req.Role, true, out var role)) { throw new Exception($"Unable to convert string input '{req.Role}' to a role enum variant."); } return Ok(await service.AddMember(teamId, req.UserId, role)); } public record UpdateMemberRequest( [AllowedValues("PLAYER", "COACH")] string Role ); [HttpPut("/team/{teamId:int}/members/{userId:int}")] public async Task UpdateMember(int teamId, int userId, [FromBody] UpdateMemberRequest req) { if (!Enum.TryParse(req.Role, true, out var role)) { throw new Exception($"Unable to convert string input '{req.Role}' to a role enum variant."); } var updated = await service.UpdateMember(new Member(teamId, userId, role)); return updated ? Ok() : NotFound(); } [HttpDelete("/team/{teamId:int}/members/{userId:int}")] public async Task RemoveMember(int teamId, int userId) { var removed = await service.RemoveMember(teamId, userId); return removed ? Ok() : NotFound(); } public record ShareTacticToTeamRequest( int TacticId, int TeamId ); [HttpPost("/team/share-tactic")] [Authorize] public async Task ShareTactic([FromBody] ShareTacticToTeamRequest sharedTactic) { var userId = accessor.CurrentUserId(HttpContext); var success = await tactics.ShareTactic(sharedTactic.TacticId, null, sharedTactic.TeamId); return success ? Ok() : BadRequest(); } [HttpGet("/tactics/shared/team/{teamId:int}")] [Authorize] public async Task GetSharedTacticsToTeam(int teamId) { var currentUserId = accessor.CurrentUserId(HttpContext); if (!await service.IsUserInTeam(currentUserId, teamId)) { return Unauthorized(); } var sharedTactics = await service.GetSharedTacticsToTeam(teamId); return sharedTactics != null ? Ok(sharedTactics) : NotFound(); } }