Skip to content

Commit a537acf

Browse files
authored
Merge pull request #95 from boolean-uk/getModulesEndpoint
created three dtos and changed exerciseEndpoint
2 parents 56c4fc4 + 62f4a47 commit a537acf

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using exercise.wwwapi.Models;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
5+
namespace exercise.wwwapi.DTOs.Exercises
6+
{
7+
public class GetExerciseForUserDTO
8+
{
9+
public int Id { get; set; }
10+
11+
public int UnitId { get; set; }
12+
public string Name { get; set; }
13+
14+
public string GitHubLink { get; set; }
15+
16+
public string Description { get; set; }
17+
18+
public bool isSubmitted { get; set; }
19+
20+
public GetExerciseForUserDTO()
21+
{
22+
23+
}
24+
25+
public GetExerciseForUserDTO(Exercise model, ICollection<UserExercise> userExercises)
26+
{
27+
Id = model.Id;
28+
UnitId = model.UnitId;
29+
Name = model.Name;
30+
isSubmitted = userExercises.Any(ue => ue.ExerciseId == model.Id && ue.Submitted);
31+
}
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using exercise.wwwapi.Models;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
5+
namespace exercise.wwwapi.DTOs.Exercises
6+
{
7+
public class GetModuleForUserDTO
8+
{
9+
public int Id { get; set; }
10+
11+
public string Title { get; set; }
12+
13+
public bool IsCompleted { get; set; }
14+
15+
public ICollection<GetUnitForUserDTO> Units { get; set; } = new List<GetUnitForUserDTO>();
16+
17+
public GetModuleForUserDTO()
18+
{
19+
20+
}
21+
22+
public GetModuleForUserDTO(Module model, ICollection<UserExercise> userExercises)
23+
{
24+
Id = model.Id;
25+
Title = model.Title;
26+
Units = model.Units.Select(u => new GetUnitForUserDTO(u, userExercises)).ToList();
27+
IsCompleted = Units.All(u => u.IsCompleted);
28+
}
29+
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using exercise.wwwapi.Models;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
5+
namespace exercise.wwwapi.DTOs.Exercises
6+
{
7+
public class GetUnitForUserDTO
8+
9+
{
10+
public int Id { get; set; }
11+
public int ModuleId { get; set; }
12+
public string Name { get; set; }
13+
14+
public bool IsCompleted { get; set; }
15+
16+
public ICollection<GetExerciseForUserDTO> Exercises { get; set; } = new List<GetExerciseForUserDTO>();
17+
18+
public GetUnitForUserDTO()
19+
{
20+
21+
}
22+
23+
public GetUnitForUserDTO(Unit model, ICollection<UserExercise> userExercises)
24+
{
25+
Id = model.Id;
26+
ModuleId = model.ModuleId;
27+
Name = model.Name;
28+
Exercises = model.Exercises.Select(e => new GetExerciseForUserDTO(e, userExercises)).ToList();
29+
IsCompleted = Exercises.All(e => e.isSubmitted);
30+
}
31+
}
32+
}

exercise.wwwapi/Endpoints/ExerciseEndpoints.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.EntityFrameworkCore;
2323
using exercise.wwwapi.Models.Exercises;
2424
using exercise.wwwapi.DTOs.Exercises;
25+
using System.Linq;
2526

2627
namespace exercise.wwwapi.EndPoints;
2728

@@ -45,6 +46,7 @@ public static void ConfigureExerciseEndpoints(this WebApplication app)
4546
units.MapPut("/{id}", UpdateUnit).WithSummary("Update unit with provided id");
4647

4748
var modules = app.MapGroup("modules");
49+
modules.MapGet("/by_user/{user_id}", GetModulesByUserId).WithSummary("Returns all modules for a given user");
4850
modules.MapGet("/", GetModules).WithSummary("Returns all modules");
4951
modules.MapGet("/{id}", GetModuleById).WithSummary("Returns module with provided id");
5052
modules.MapPost("/", CreateModule).WithSummary("Create a new module");
@@ -55,6 +57,32 @@ public static void ConfigureExerciseEndpoints(this WebApplication app)
5557

5658

5759
}
60+
[ProducesResponseType(StatusCodes.Status200OK)]
61+
[ProducesResponseType(StatusCodes.Status404NotFound)]
62+
[ProducesResponseType(StatusCodes.Status401Unauthorized)] // will implement if some users should not have access
63+
private static async Task<IResult> GetModulesByUserId(IRepository<User> userRepository, ClaimsPrincipal claimsPrincipal, int user_id)
64+
{
65+
var response = await userRepository.GetByIdWithIncludes(a => a
66+
.Include(b => b.User_CC)
67+
.ThenInclude(c => c.CohortCourse)
68+
.ThenInclude(d => d.Course)
69+
.ThenInclude(e => e.CourseModules)
70+
.ThenInclude(f => f.Module)
71+
.ThenInclude(g => g.Units)
72+
.ThenInclude(h => h.Exercises)
73+
.Include(i => i.User_Exercises), user_id);
74+
75+
if (response == null)
76+
{
77+
return TypedResults.NotFound("user does not exist");
78+
}
79+
80+
81+
82+
var result = response.User_CC.LastOrDefault().CohortCourse.Course.CourseModules.Select(a => new GetModuleForUserDTO(a.Module, response.User_Exercises)).ToList();
83+
return TypedResults.Ok(result);
84+
}
85+
5886

5987
[ProducesResponseType(StatusCodes.Status200OK)]
6088
private static async Task<IResult> GetModules(IRepository<Module> moduleRepository, ClaimsPrincipal claimsPrincipal)

0 commit comments

Comments
 (0)