Skip to content

Commit 1ee9496

Browse files
Merge branch 'temp_main' into docker_maybe
2 parents a864cfd + 1484d20 commit 1ee9496

File tree

11 files changed

+229
-5
lines changed

11 files changed

+229
-5
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 string StartDate { get; set; }
17+
public string 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:MMMM} {model.Cohort.StartDate:yyyy}";
29+
EndDate = $"{model.Cohort.EndDate:MMMM} {model.Cohort.EndDate:yyyy}";
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: 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/DTOs/Users/UserDTO.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class UserDTO
3535
public Specialism? Specialism { get; set; }
3636

3737
public int? CohortId { get; set; }
38+
public int? CohortCourseId { get; set; }
3839

3940
public DateTime? CurrentStartdate { get; set; }
4041
public DateTime? CurrentEnddate { get; set; }
@@ -63,7 +64,8 @@ public UserDTO(User model)
6364
Mobile = model.Mobile;
6465
Specialism = model.Specialism;
6566
Role = model.Role.ToString();
66-
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc
67+
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId;
68+
CohortCourseId = model.User_CC?.LastOrDefault()?.Id; //autofetching the first element of usercc
6769
CurrentStartdate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
6870
CurrentEnddate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
6971
Notes = model.Notes.Select(n => new NoteDTO(n)).ToList();
@@ -81,7 +83,8 @@ public UserDTO(User model, PrivilegeLevel privilegeLevel)
8183
Mobile = model.Mobile;
8284
Specialism = model.Specialism;
8385
Role = model.Role.ToString();
84-
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc
86+
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId;
87+
CohortCourseId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc
8588
CurrentStartdate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
8689
CurrentEnddate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
8790

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/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)

exercise.wwwapi/Endpoints/UserEndpoints.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,11 @@ public static async Task<IResult> UpdateUser(IRepository<User> userRepository, i
386386
public static async Task<IResult> DeleteUser(IRepository<User> userRepository, int id,
387387
ClaimsPrincipal claimsPrincipal)
388388
{
389+
var userRole = claimsPrincipal.Role();
390+
var authorizedAsTeacher = AuthorizeTeacher(claimsPrincipal);
391+
389392
var userIdClaim = claimsPrincipal.UserRealId();
390-
if (userIdClaim == null || userIdClaim != id)
393+
if (!authorizedAsTeacher && (userIdClaim == null || userIdClaim != id))
391394
{
392395
return Results.Unauthorized();
393396
}

0 commit comments

Comments
 (0)