Skip to content

Commit dfd69b5

Browse files
authored
Merge pull request #97 from boolean-uk/cohortCourseEndpoints
new endpoint
2 parents efa02df + 934b8c4 commit dfd69b5

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using exercise.wwwapi.DTOs.Users;
2+
using exercise.wwwapi.Models;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.ComponentModel.DataAnnotations.Schema;
5+
6+
namespace exercise.wwwapi.DTOs.CohortCourse;
7+
8+
public class GetCohortCourseDTO
9+
{
10+
public int Id { get; set; }
11+
public int CohortId { get; set; }
12+
public int CourseId { get; set; }
13+
14+
public string CohortName { get; set; }
15+
public string CourseName { get; set; }
16+
public DateTime StartDate { get; set; }
17+
public DateTime EndDate { get; set; }
18+
public ICollection<UserDTO> Users { get; set; } = new List<UserDTO>();
19+
20+
public GetCohortCourseDTO() { }
21+
public GetCohortCourseDTO(Models.CohortCourse model)
22+
{
23+
Id = model.Id;
24+
CohortId = model.CohortId;
25+
CourseId = model.CourseId;
26+
CohortName = model.Cohort.CohortName;
27+
CourseName = model.Course.Name;
28+
StartDate = model.Cohort.StartDate;
29+
EndDate = model.Cohort.EndDate;
30+
Users = model.UserCCs.Select(uc => new UserDTO(uc.User)).ToList();
31+
32+
33+
}
34+
}
35+

exercise.wwwapi/DTOs/Cohorts/CohortDTO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public CohortDTO(Cohort model)
2424
CohortName = model.CohortName;
2525
StartDate = model.StartDate;
2626
EndDate = model.EndDate;
27-
Courses = model.CohortCourses.Select(cc => new CourseDTO(cc)).ToList();
27+
Courses = model.CohortCourses.Select(cc => new CourseDTO(cc.Course)).ToList();
2828
}
2929
}

exercise.wwwapi/DTOs/Courses/CourseDTO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public CourseDTO(Course model)
1313
Id = model.Id;
1414
Name = model.Name;
1515
}
16-
public CourseDTO(CohortCourse model)
16+
public CourseDTO(Models.CohortCourse model)
1717
{
1818
Id = model.Course.Id;
1919
Name = model.Course.Name;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using exercise.wwwapi.DTOs;
2+
using exercise.wwwapi.DTOs.CohortCourse;
3+
using exercise.wwwapi.DTOs.Courses;
4+
using exercise.wwwapi.DTOs.Exercises;
5+
using exercise.wwwapi.Enums;
6+
using exercise.wwwapi.Models;
7+
using exercise.wwwapi.Repository;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.EntityFrameworkCore;
10+
using Npgsql;
11+
12+
namespace exercise.wwwapi.Endpoints;
13+
14+
public static class CohortCourseEndpoints
15+
{
16+
public static void ConfigureCohortCourseEndpoints(this WebApplication app)
17+
{
18+
var cohortcourses = app.MapGroup("cohortcourses");
19+
cohortcourses.MapGet("/", GetAllCohortCourses).WithSummary("Get all cohort_courses");
20+
cohortcourses.MapGet("/{id}", GetCohortCourseById).WithSummary("Get cohort_course by id");
21+
}
22+
23+
[ProducesResponseType(StatusCodes.Status200OK)]
24+
[ProducesResponseType(StatusCodes.Status404NotFound)]
25+
public static async Task<IResult> GetAllCohortCourses(IRepository<CohortCourse> cohortCourseRepository)
26+
{
27+
var response = await cohortCourseRepository.GetWithIncludes(a => a
28+
.Include(b => b.Cohort)
29+
.Include(c => c.Course)
30+
.Include(d => d.UserCCs)
31+
.ThenInclude(e => e.User));
32+
33+
var result = response.Select(cc => new GetCohortCourseDTO(cc)).ToList();
34+
35+
return TypedResults.Ok(result);
36+
37+
38+
}
39+
[ProducesResponseType(StatusCodes.Status200OK)]
40+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
41+
[ProducesResponseType(StatusCodes.Status404NotFound)]
42+
public static async Task<IResult> GetCohortCourseById(IRepository<CohortCourse> cohortCourseRepository, int id)
43+
{
44+
var response = await cohortCourseRepository.GetByIdWithIncludes(a => a
45+
.Include(b => b.Cohort)
46+
.Include(c => c.Course)
47+
.Include(d => d.UserCCs)
48+
.ThenInclude(e => e.User), id);
49+
50+
if (response == null) return TypedResults.NotFound("No cohort_course with that id exists");
51+
52+
var result = new GetCohortCourseDTO(response);
53+
54+
return TypedResults.Ok(result);
55+
}
56+
57+
58+
}

exercise.wwwapi/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
app.ConfigureExerciseEndpoints();
219219
app.ConfigureCourseEndpoints();
220220
app.ConfigureLikeEndpoints();
221+
app.ConfigureCohortCourseEndpoints();
221222
app.Run();
222223

223224
static string CreateToken(IConfigurationSettings configurationSettings)

0 commit comments

Comments
 (0)