From c735d83a72d49f845327d598c9d82e5e0599972c Mon Sep 17 00:00:00 2001 From: Roman Eriksen Date: Mon, 22 Sep 2025 11:51:13 +0200 Subject: [PATCH 1/3] Implemented GetCohortByUserId. Added some checks --- exercise.wwwapi/Endpoints/CohortEndpoints.cs | 42 ++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/exercise.wwwapi/Endpoints/CohortEndpoints.cs b/exercise.wwwapi/Endpoints/CohortEndpoints.cs index 5130962..a3c5198 100644 --- a/exercise.wwwapi/Endpoints/CohortEndpoints.cs +++ b/exercise.wwwapi/Endpoints/CohortEndpoints.cs @@ -17,8 +17,8 @@ public static void ConfigureCohortEndpoints(this WebApplication app) var cohorts = app.MapGroup("cohorts"); cohorts.MapPost("/", CreateCohort).WithSummary("Create a cohort"); cohorts.MapGet("/", GetAllCohorts).WithSummary("Get all cohorts"); - cohorts.MapGet("/{id}", GetCohort).WithSummary("Get a cohort by ID"); - //cohorts.MapGet("/{userId}", GetCohortByUserId).WithSummary(""); + cohorts.MapGet("/cohortId/{id}", GetCohort).WithSummary("Get a cohort by ID"); + cohorts.MapGet("/userId/{userId}", GetCohortByUserId).WithSummary("Get all cohorts a user is in by its Id"); cohorts.MapPost("/{cohortId}/{userId}/{courseId}", AddUserToCohort).WithSummary("Add a user to a cohort"); cohorts.MapDelete("/{cohortId}/{userId}/{courseId}", DeleteUserFromCohort).WithSummary("Delete a user from a cohort"); } @@ -32,6 +32,14 @@ public static async Task GetCohort(IRepository service, IMapper .Include(c => c.CohortCourses) .ThenInclude(cc => cc.CohortCourseUsers) .ThenInclude(ccu => ccu.User)); + + if (result == null) + return TypedResults.BadRequest(new ResponseDTO + { + Message = $"Cohort with id '{cohortId}' does not exists", + Data = result + }); + CohortDTO cohortDTO = mapper.Map(result); ResponseDTO response = new ResponseDTO() { @@ -44,6 +52,35 @@ public static async Task GetCohort(IRepository service, IMapper //return TypedResults.Ok(cohortDTOs); } + + [ProducesResponseType(StatusCodes.Status200OK)] + public static async Task GetCohortByUserId(IRepository cohortRepo, IMapper mapper, int userId) + { + var results = cohortRepo.GetWithIncludes(q => q + .Include(c => c.CohortCourses) + .ThenInclude(cc => cc.Course) + .Include(c => c.CohortCourses) + .ThenInclude(cc => cc.CohortCourseUsers) + .ThenInclude(ccu => ccu.User) + ).Where(r => r.CohortCourses.Any(cc => cc.CohortCourseUsers.Any(ccu => ccu.UserId == userId))); + + if (!results.Any()) + return TypedResults.BadRequest(new ResponseDTO + { + Message = $"User with id {userId} either does not exist, or is not registered in any cohorts", + Data = results + }); + + IEnumerable cohortDTOs = mapper.Map>(results); + ResponseDTO> response = new ResponseDTO>() + { + Message = "Success", + Data = cohortDTOs + }; + + return TypedResults.Ok(response); + } + [ProducesResponseType(StatusCodes.Status200OK)] public static async Task GetAllCohorts(IRepository cohortService, IMapper mapper) { @@ -54,7 +91,6 @@ public static async Task GetAllCohorts(IRepository cohortServic .ThenInclude(cc => cc.CohortCourseUsers) .ThenInclude(ccu => ccu.User) ); - Console.WriteLine(results); IEnumerable cohortDTOs = mapper.Map>(results); ResponseDTO> response = new ResponseDTO>() From afbe03350317452b1bc7f56ef2cd6b16ad9cabd4 Mon Sep 17 00:00:00 2001 From: Roman Eriksen Date: Tue, 23 Sep 2025 08:52:22 +0200 Subject: [PATCH 2/3] Added authorization to cohort endpoints --- exercise.wwwapi/Endpoints/CohortEndpoints.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/exercise.wwwapi/Endpoints/CohortEndpoints.cs b/exercise.wwwapi/Endpoints/CohortEndpoints.cs index a3c5198..8a95136 100644 --- a/exercise.wwwapi/Endpoints/CohortEndpoints.cs +++ b/exercise.wwwapi/Endpoints/CohortEndpoints.cs @@ -4,6 +4,7 @@ using exercise.wwwapi.DTOs.Posts; using exercise.wwwapi.Models; using exercise.wwwapi.Repository; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Linq; @@ -23,6 +24,8 @@ public static void ConfigureCohortEndpoints(this WebApplication app) cohorts.MapDelete("/{cohortId}/{userId}/{courseId}", DeleteUserFromCohort).WithSummary("Delete a user from a cohort"); } + [Authorize] + [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status200OK)] public static async Task GetCohort(IRepository service, IMapper mapper, int cohortId) { @@ -52,7 +55,8 @@ public static async Task GetCohort(IRepository service, IMapper //return TypedResults.Ok(cohortDTOs); } - + [Authorize] + [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status200OK)] public static async Task GetCohortByUserId(IRepository cohortRepo, IMapper mapper, int userId) { @@ -81,6 +85,7 @@ public static async Task GetCohortByUserId(IRepository cohortRe return TypedResults.Ok(response); } + [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] public static async Task GetAllCohorts(IRepository cohortService, IMapper mapper) { @@ -102,6 +107,8 @@ public static async Task GetAllCohorts(IRepository cohortServic return TypedResults.Ok(response); } + [Authorize] + [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status201Created)] public static async Task CreateCohort( IRepository cohortService, @@ -155,6 +162,7 @@ public static async Task CreateCohort( return TypedResults.Created($"/api/cohorts/{cohort.Id}", response); } + [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public static async Task AddUserToCohort( @@ -243,6 +251,7 @@ public static async Task AddUserToCohort( }); } + [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public static async Task DeleteUserFromCohort( From 70fae81fb3ab887eea8d426f8dcad8f62d8a244c Mon Sep 17 00:00:00 2001 From: Roman Eriksen Date: Tue, 23 Sep 2025 09:20:46 +0200 Subject: [PATCH 3/3] Removed old cohort test --- .../IntegrationTests/CohortTests.cs | 45 +------------------ 1 file changed, 1 insertion(+), 44 deletions(-) diff --git a/exercise.tests/IntegrationTests/CohortTests.cs b/exercise.tests/IntegrationTests/CohortTests.cs index e169495..881464a 100644 --- a/exercise.tests/IntegrationTests/CohortTests.cs +++ b/exercise.tests/IntegrationTests/CohortTests.cs @@ -8,47 +8,4 @@ using System.Text.Json.Nodes; using System.Threading.Tasks; -namespace exercise.tests.IntegrationTests -{ - internal class CohortTests - { - private WebApplicationFactory _factory; - private HttpClient _client; - - [SetUp] - public void SetUp() - { - // Arrange - _factory = new WebApplicationFactory(); - _client = _factory.CreateClient(); - } - - [TearDown] - public void TearDown() - { - _client.Dispose(); - _factory.Dispose(); - } - - [Test] - public async Task GetAllCohorts() - { - var response = await _client.GetAsync("/cohorts"); - var contentString = await response.Content.ReadAsStringAsync(); - var message = string.IsNullOrWhiteSpace(contentString) ? null : JsonNode.Parse(contentString); - - Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); - Assert.That(message, Is.Not.Null); - //Assert.That(message?["message"]?.GetValue(), Is.EqualTo("success")); - - var data = message?["data"]?.AsArray(); - Assert.That(data, Is.Not.Null); - Assert.That(data!.Count, Is.GreaterThan(0)); - - var cohort = data!.First(); - Assert.That(cohort["id"]?.GetValue(), Is.GreaterThan(0)); - Assert.That(cohort["title"].GetValue, Is.Not.Null); - //Assert.That(cohort["courses"]["students"].GetValue, Is.Not.Null); - } - } -} +namespace exercise.tests.IntegrationTests{}