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{} diff --git a/exercise.wwwapi/Endpoints/CohortEndpoints.cs b/exercise.wwwapi/Endpoints/CohortEndpoints.cs index 8405b1b..ef522a2 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; @@ -18,11 +19,13 @@ public static void ConfigureCohortEndpoints(this WebApplication app) cohorts.MapPost("/", CreateCohort).WithSummary("Create a cohort"); cohorts.MapGet("/", GetAllCohorts).WithSummary("Get all cohorts"); cohorts.MapGet("/cohortId/{cohortId}", GetCohort).WithSummary("Get a cohort by ID"); - //cohorts.MapGet("/{userId}", GetCohortByUserId).WithSummary(""); + cohorts.MapGet("/userId/{userId}", GetCohortByUserId).WithSummary("Get all cohorts a user is in by its Id"); cohorts.MapPost("/cohortId/{cohortId}/userId/{userId}/courseId/{courseId}", AddUserToCohort).WithSummary("Add a user to a cohort"); cohorts.MapDelete("/cohortId/{cohortId}/userId/{userId}/courseId/{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) { @@ -32,6 +35,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 +55,37 @@ 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) + { + 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); + } + + [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] public static async Task GetAllCohorts(IRepository cohortService, IMapper mapper) { @@ -54,7 +96,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>() @@ -66,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, @@ -119,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( @@ -207,6 +251,7 @@ public static async Task AddUserToCohort( }); } + [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public static async Task DeleteUserFromCohort(