add getTeam route
continuous-integration/drone/push Build is passing Details

master
Maxime BATISTA 8 months ago
parent e81b7dd24d
commit cfddcb9f81

@ -1,7 +1,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using API.Context; using API.Context;
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 +10,15 @@ namespace API.Controllers;
[ApiController] [ApiController]
[Authorize] [Authorize]
public class TeamsController(ITeamService service, ITacticService tactics,IContextAccessor accessor) : ControllerBase public class TeamsController(ITeamService service, ITacticService tactics, IContextAccessor accessor) : ControllerBase
{ {
public record CreateTeamRequest( public record CreateTeamRequest(
[Name] string Name, [Name] string Name,
[Url] string Picture, [Url] string Picture,
[RegularExpression("^#[0-9A-Fa-f]{6}$")] string FirstColor, [RegularExpression("^#[0-9A-Fa-f]{6}$")]
[RegularExpression("^#[0-9A-Fa-f]{6}$")] string SecondColor string FirstColor,
[RegularExpression("^#[0-9A-Fa-f]{6}$")]
string SecondColor
); );
[HttpPost("/teams")] [HttpPost("/teams")]
@ -29,6 +30,13 @@ 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)
{ {
@ -59,7 +67,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 +119,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 +141,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 +155,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 +166,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 +175,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();

@ -52,6 +52,12 @@ public class DbTeamService(AppContext.AppContext context) : ITeamService
return entity.ToModel(); return entity.ToModel();
} }
public async Task<Team?> GetTeam(int id)
{
var entity = await context.Teams.FirstOrDefaultAsync(t => t.Id == id);
return entity?.ToModel();
}
public async Task RemoveTeams(params int[] teams) public async Task RemoveTeams(params int[] teams)
{ {
await context.Teams await context.Teams

@ -26,6 +26,8 @@ public interface ITeamService
/// Adds a new team. /// Adds a new team.
/// </summary> /// </summary>
Task<Team> AddTeam(string name, string picture, string firstColor, string secondColor); Task<Team> AddTeam(string name, string picture, string firstColor, string secondColor);
Task<Team?> GetTeam(int id);
/// <summary> /// <summary>
/// Removes one or more teams. /// Removes one or more teams.

Loading…
Cancel
Save