From cfddcb9f817da257d8f314c0b965deebeaa89d45 Mon Sep 17 00:00:00 2001 From: maxime Date: Tue, 2 Apr 2024 23:10:30 +0200 Subject: [PATCH] add getTeam route --- API/Controllers/TeamsController.cs | 31 ++++++++++++++++++------------ DbServices/DbTeamService.cs | 6 ++++++ Services/ITeamService.cs | 2 ++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/API/Controllers/TeamsController.cs b/API/Controllers/TeamsController.cs index 348d7ae..a2f5d74 100644 --- a/API/Controllers/TeamsController.cs +++ b/API/Controllers/TeamsController.cs @@ -1,7 +1,6 @@ using System.ComponentModel.DataAnnotations; using API.Context; using API.Validation; -using AppContext.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Model; @@ -11,13 +10,15 @@ namespace API.Controllers; [ApiController] [Authorize] -public class TeamsController(ITeamService service, ITacticService tactics,IContextAccessor accessor) : ControllerBase +public class TeamsController(ITeamService service, ITacticService tactics, IContextAccessor accessor) : ControllerBase { public record CreateTeamRequest( [Name] string Name, [Url] string Picture, - [RegularExpression("^#[0-9A-Fa-f]{6}$")] string FirstColor, - [RegularExpression("^#[0-9A-Fa-f]{6}$")] string SecondColor + [RegularExpression("^#[0-9A-Fa-f]{6}$")] + string FirstColor, + [RegularExpression("^#[0-9A-Fa-f]{6}$")] + string SecondColor ); [HttpPost("/teams")] @@ -29,6 +30,13 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte return Ok(team); } + [HttpGet("/teams/{id:int}")] + public async Task GetTeam(int id) + { + var team = await service.GetTeam(id); + return team == null ? NotFound() : Ok(team); + } + [HttpGet("/teams/{teamId:int}/members")] public async Task 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."); } - + var accessibility = await service.EnsureAccessibility(accessor.CurrentUserId(HttpContext), teamId, MemberRole.Coach); @@ -111,8 +119,6 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte default: //unreachable return Problem(); } - - } [HttpDelete("/team/{teamId:int}/members/{userId:int}")] @@ -135,12 +141,12 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte return Problem(); } } - + public record ShareTacticToTeamRequest( int TacticId, int TeamId ); - + [HttpPost("/team/share-tactic")] public async Task ShareTactic([FromBody] ShareTacticToTeamRequest sharedTactic) { @@ -149,7 +155,7 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte return success ? Ok() : BadRequest(); } - + [HttpDelete("/tactics/shared/{tacticId:int}/team/{teamId:int}")] public async Task UnshareTactic(int tacticId, int teamId) { @@ -160,6 +166,7 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte { return NotFound(); } + if (currentUserId != tactic.OwnerId) { return Unauthorized(); @@ -168,12 +175,12 @@ public class TeamsController(ITeamService service, ITacticService tactics,IConte var success = await tactics.UnshareTactic(tacticId, null, teamId); return success ? Ok() : NotFound(); } - + [HttpGet("/tactics/shared/team/{teamId:int}")] public async Task GetSharedTacticsToTeam(int teamId) { var currentUserId = accessor.CurrentUserId(HttpContext); - + if (!await service.IsUserInTeam(currentUserId, teamId)) { return Unauthorized(); diff --git a/DbServices/DbTeamService.cs b/DbServices/DbTeamService.cs index 8d32533..2fdcd10 100644 --- a/DbServices/DbTeamService.cs +++ b/DbServices/DbTeamService.cs @@ -52,6 +52,12 @@ public class DbTeamService(AppContext.AppContext context) : ITeamService return entity.ToModel(); } + public async Task GetTeam(int id) + { + var entity = await context.Teams.FirstOrDefaultAsync(t => t.Id == id); + return entity?.ToModel(); + } + public async Task RemoveTeams(params int[] teams) { await context.Teams diff --git a/Services/ITeamService.cs b/Services/ITeamService.cs index bb2b1be..15cd4e1 100644 --- a/Services/ITeamService.cs +++ b/Services/ITeamService.cs @@ -26,6 +26,8 @@ public interface ITeamService /// Adds a new team. /// Task AddTeam(string name, string picture, string firstColor, string secondColor); + + Task GetTeam(int id); /// /// Removes one or more teams.