Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 1 addition & 44 deletions exercise.tests/IntegrationTests/CohortTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,4 @@
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace exercise.tests.IntegrationTests
{
internal class CohortTests
{
private WebApplicationFactory<Program> _factory;
private HttpClient _client;

[SetUp]
public void SetUp()
{
// Arrange
_factory = new WebApplicationFactory<Program>();
_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<string>(), 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<int>(), Is.GreaterThan(0));
Assert.That(cohort["title"].GetValue<string>, Is.Not.Null);
//Assert.That(cohort["courses"]["students"].GetValue<Array>, Is.Not.Null);
}
}
}
namespace exercise.tests.IntegrationTests{}
49 changes: 47 additions & 2 deletions exercise.wwwapi/Endpoints/CohortEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<IResult> GetCohort(IRepository<Cohort> service, IMapper mapper, int cohortId)
{
Expand All @@ -32,6 +35,14 @@ public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper
.Include(c => c.CohortCourses)
.ThenInclude(cc => cc.CohortCourseUsers)
.ThenInclude(ccu => ccu.User));

if (result == null)
return TypedResults.BadRequest(new ResponseDTO<object>
{
Message = $"Cohort with id '{cohortId}' does not exists",
Data = result
});

CohortDTO cohortDTO = mapper.Map<CohortDTO>(result);
ResponseDTO<CohortDTO> response = new ResponseDTO<CohortDTO>()
{
Expand All @@ -44,6 +55,37 @@ public static async Task<IResult> GetCohort(IRepository<Cohort> service, IMapper
//return TypedResults.Ok(cohortDTOs);
}

[Authorize]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetCohortByUserId(IRepository<Cohort> 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<object>
{
Message = $"User with id {userId} either does not exist, or is not registered in any cohorts",
Data = results
});

IEnumerable<CohortDTO> cohortDTOs = mapper.Map<IEnumerable<CohortDTO>>(results);
ResponseDTO<IEnumerable<CohortDTO>> response = new ResponseDTO<IEnumerable<CohortDTO>>()
{
Message = "Success",
Data = cohortDTOs
};

return TypedResults.Ok(response);
}

[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortService, IMapper mapper)
{
Expand All @@ -54,7 +96,6 @@ public static async Task<IResult> GetAllCohorts(IRepository<Cohort> cohortServic
.ThenInclude(cc => cc.CohortCourseUsers)
.ThenInclude(ccu => ccu.User)
);
Console.WriteLine(results);

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

[Authorize]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status201Created)]
public static async Task<IResult> CreateCohort(
IRepository<Cohort> cohortService,
Expand Down Expand Up @@ -119,6 +162,7 @@ public static async Task<IResult> CreateCohort(
return TypedResults.Created($"/api/cohorts/{cohort.Id}", response);
}

[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> AddUserToCohort(
Expand Down Expand Up @@ -207,6 +251,7 @@ public static async Task<IResult> AddUserToCohort(
});
}

[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> DeleteUserFromCohort(
Expand Down
Loading