Skip to content

Commit ddd1bda

Browse files
authored
Merge pull request #112 from boolean-uk/106-backend---implement-getcohortbyuserid
Implemented GetCohortByUserId. Added some checks
2 parents 42b3a42 + cdeedb5 commit ddd1bda

File tree

2 files changed

+48
-46
lines changed

2 files changed

+48
-46
lines changed

exercise.tests/IntegrationTests/CohortTests.cs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,4 @@
88
using System.Text.Json.Nodes;
99
using System.Threading.Tasks;
1010

11-
namespace exercise.tests.IntegrationTests
12-
{
13-
internal class CohortTests
14-
{
15-
private WebApplicationFactory<Program> _factory;
16-
private HttpClient _client;
17-
18-
[SetUp]
19-
public void SetUp()
20-
{
21-
// Arrange
22-
_factory = new WebApplicationFactory<Program>();
23-
_client = _factory.CreateClient();
24-
}
25-
26-
[TearDown]
27-
public void TearDown()
28-
{
29-
_client.Dispose();
30-
_factory.Dispose();
31-
}
32-
33-
[Test]
34-
public async Task GetAllCohorts()
35-
{
36-
var response = await _client.GetAsync("/cohorts");
37-
var contentString = await response.Content.ReadAsStringAsync();
38-
var message = string.IsNullOrWhiteSpace(contentString) ? null : JsonNode.Parse(contentString);
39-
40-
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
41-
Assert.That(message, Is.Not.Null);
42-
//Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("success"));
43-
44-
var data = message?["data"]?.AsArray();
45-
Assert.That(data, Is.Not.Null);
46-
Assert.That(data!.Count, Is.GreaterThan(0));
47-
48-
var cohort = data!.First();
49-
Assert.That(cohort["id"]?.GetValue<int>(), Is.GreaterThan(0));
50-
Assert.That(cohort["title"].GetValue<string>, Is.Not.Null);
51-
//Assert.That(cohort["courses"]["students"].GetValue<Array>, Is.Not.Null);
52-
}
53-
}
54-
}
11+
namespace exercise.tests.IntegrationTests{}

exercise.wwwapi/Endpoints/CohortEndpoints.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using exercise.wwwapi.DTOs.Posts;
55
using exercise.wwwapi.Models;
66
using exercise.wwwapi.Repository;
7+
using Microsoft.AspNetCore.Authorization;
78
using Microsoft.AspNetCore.Mvc;
89
using Microsoft.EntityFrameworkCore;
910
using System.Linq;
@@ -18,11 +19,13 @@ public static void ConfigureCohortEndpoints(this WebApplication app)
1819
cohorts.MapPost("/", CreateCohort).WithSummary("Create a cohort");
1920
cohorts.MapGet("/", GetAllCohorts).WithSummary("Get all cohorts");
2021
cohorts.MapGet("/cohortId/{cohortId}", GetCohort).WithSummary("Get a cohort by ID");
21-
//cohorts.MapGet("/{userId}", GetCohortByUserId).WithSummary("");
22+
cohorts.MapGet("/userId/{userId}", GetCohortByUserId).WithSummary("Get all cohorts a user is in by its Id");
2223
cohorts.MapPost("/cohortId/{cohortId}/userId/{userId}/courseId/{courseId}", AddUserToCohort).WithSummary("Add a user to a cohort");
2324
cohorts.MapDelete("/cohortId/{cohortId}/userId/{userId}/courseId/{courseId}", DeleteUserFromCohort).WithSummary("Delete a user from a cohort");
2425
}
2526

27+
[Authorize]
28+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
2629
[ProducesResponseType(StatusCodes.Status200OK)]
2730
public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper mapper, int cohortId)
2831
{
@@ -32,6 +35,14 @@ public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper
3235
.Include(c => c.CohortCourses)
3336
.ThenInclude(cc => cc.CohortCourseUsers)
3437
.ThenInclude(ccu => ccu.User));
38+
39+
if (result == null)
40+
return TypedResults.BadRequest(new ResponseDTO<object>
41+
{
42+
Message = $"Cohort with id '{cohortId}' does not exists",
43+
Data = result
44+
});
45+
3546
CohortDTO cohortDTO = mapper.Map<CohortDTO>(result);
3647
ResponseDTO<CohortDTO> response = new ResponseDTO<CohortDTO>()
3748
{
@@ -44,6 +55,37 @@ public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper
4455
//return TypedResults.Ok(cohortDTOs);
4556
}
4657

58+
[Authorize]
59+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
60+
[ProducesResponseType(StatusCodes.Status200OK)]
61+
public static async Task<IResult> GetCohortByUserId(IRepository<Cohort> cohortRepo, IMapper mapper, int userId)
62+
{
63+
var results = cohortRepo.GetWithIncludes(q => q
64+
.Include(c => c.CohortCourses)
65+
.ThenInclude(cc => cc.Course)
66+
.Include(c => c.CohortCourses)
67+
.ThenInclude(cc => cc.CohortCourseUsers)
68+
.ThenInclude(ccu => ccu.User)
69+
).Where(r => r.CohortCourses.Any(cc => cc.CohortCourseUsers.Any(ccu => ccu.UserId == userId)));
70+
71+
if (!results.Any())
72+
return TypedResults.BadRequest(new ResponseDTO<object>
73+
{
74+
Message = $"User with id {userId} either does not exist, or is not registered in any cohorts",
75+
Data = results
76+
});
77+
78+
IEnumerable<CohortDTO> cohortDTOs = mapper.Map<IEnumerable<CohortDTO>>(results);
79+
ResponseDTO<IEnumerable<CohortDTO>> response = new ResponseDTO<IEnumerable<CohortDTO>>()
80+
{
81+
Message = "Success",
82+
Data = cohortDTOs
83+
};
84+
85+
return TypedResults.Ok(response);
86+
}
87+
88+
[Authorize]
4789
[ProducesResponseType(StatusCodes.Status200OK)]
4890
public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortService, IMapper mapper)
4991
{
@@ -54,7 +96,6 @@ public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortServic
5496
.ThenInclude(cc => cc.CohortCourseUsers)
5597
.ThenInclude(ccu => ccu.User)
5698
);
57-
Console.WriteLine(results);
5899

59100
IEnumerable<CohortDTO> cohortDTOs = mapper.Map<IEnumerable<CohortDTO>>(results);
60101
ResponseDTO<IEnumerable<CohortDTO>> response = new ResponseDTO<IEnumerable<CohortDTO>>()
@@ -66,6 +107,8 @@ public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortServic
66107
return TypedResults.Ok(response);
67108
}
68109

110+
[Authorize]
111+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
69112
[ProducesResponseType(StatusCodes.Status201Created)]
70113
public static async Task<IResult> CreateCohort(
71114
IRepository<Cohort> cohortService,
@@ -119,6 +162,7 @@ public static async Task<IResult> CreateCohort(
119162
return TypedResults.Created($"/api/cohorts/{cohort.Id}", response);
120163
}
121164

165+
[Authorize]
122166
[ProducesResponseType(StatusCodes.Status200OK)]
123167
[ProducesResponseType(StatusCodes.Status400BadRequest)]
124168
public static async Task<IResult> AddUserToCohort(
@@ -207,6 +251,7 @@ public static async Task<IResult> AddUserToCohort(
207251
});
208252
}
209253

254+
[Authorize]
210255
[ProducesResponseType(StatusCodes.Status200OK)]
211256
[ProducesResponseType(StatusCodes.Status400BadRequest)]
212257
public static async Task<IResult> DeleteUserFromCohort(

0 commit comments

Comments
 (0)