Skip to content

Commit 56c4fc4

Browse files
authored
Merge pull request #93 from boolean-uk/fix_jakub_elisabeth
added ID to userDTO and nullable user_cc
2 parents 3bc0e36 + 9c2c618 commit 56c4fc4

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

exercise.wwwapi/DTOs/Users/UserDTO.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace exercise.wwwapi.DTOs.Users;
99

1010
public class UserDTO
1111
{
12+
public int Id { get; set; }
1213
[JsonPropertyName("email")]
1314
public string Email { get; set; }
1415

@@ -52,6 +53,7 @@ public UserDTO()
5253

5354
public UserDTO(User model)
5455
{
56+
Id = model.Id;
5557
Email = model.Email;
5658
FirstName = model.FirstName;
5759
LastName = model.LastName;
@@ -61,14 +63,15 @@ public UserDTO(User model)
6163
Mobile = model.Mobile;
6264
Specialism = model.Specialism;
6365
Role = model.Role.ToString();
64-
CohortId = model.User_CC.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc
65-
CurrentStartdate = model.User_CC.LastOrDefault().CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
66-
CurrentEnddate = model.User_CC.LastOrDefault().CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
66+
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc
67+
CurrentStartdate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
68+
CurrentEnddate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
6769
Notes = model.Notes.Select(n => new NoteDTO(n)).ToList();
6870
}
6971

7072
public UserDTO(User model, PrivilegeLevel privilegeLevel)
7173
{
74+
Id = model.Id;
7275
Email = model.Email;
7376
FirstName = model.FirstName;
7477
LastName = model.LastName;
@@ -78,9 +81,9 @@ public UserDTO(User model, PrivilegeLevel privilegeLevel)
7881
Mobile = model.Mobile;
7982
Specialism = model.Specialism;
8083
Role = model.Role.ToString();
81-
CohortId = model.User_CC.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc
82-
CurrentStartdate = model.User_CC.LastOrDefault().CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
83-
CurrentEnddate = model.User_CC.LastOrDefault().CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
84+
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc
85+
CurrentStartdate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
86+
CurrentEnddate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
8487

8588

8689
if (privilegeLevel == PrivilegeLevel.Teacher)

exercise.wwwapi/Endpoints/UserEndpoints.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ private static async Task<IResult> GetUsersByCohort(IRepository<Cohort> reposito
9696
var results = response.CohortCourses.SelectMany(a => a.UserCCs).Select(a => a.User).ToList();
9797
var dto_results = results.Select(a => new UserDTO(a));
9898

99+
var userRole = claimsPrincipal.Role();
100+
var authorizedAsTeacher = AuthorizeTeacher(claimsPrincipal);
101+
99102
var userData = new UsersSuccessDTO
100103
{
101-
Users = results.Select(u => new UserDTO(u)).ToList() //if teacher loads students, also load notes for students.
104+
Users = results.Select(user => authorizedAsTeacher
105+
? new UserDTO(user, PrivilegeLevel.Teacher) //if teacher loads students, also load notes for students.
106+
: new UserDTO(user, PrivilegeLevel.Student)).ToList() //if teacher loads students, also load notes for students.
102107
};
103108

104109
var responseObject = new ResponseDTO<UsersSuccessDTO>
@@ -117,7 +122,23 @@ private static async Task<IResult> GetUsersByCohortCourse(IRepository<CohortCour
117122
var results = response.UserCCs.Select(a => a.User).ToList();
118123
var dto_results = results.Select(a => new UserDTO(a));
119124

120-
return TypedResults.Ok(dto_results);
125+
var userRole = claimsPrincipal.Role();
126+
var authorizedAsTeacher = AuthorizeTeacher(claimsPrincipal);
127+
128+
var userData = new UsersSuccessDTO
129+
{
130+
Users = results.Select(user => authorizedAsTeacher
131+
? new UserDTO(user, PrivilegeLevel.Teacher) //if teacher loads students, also load notes for students.
132+
: new UserDTO(user, PrivilegeLevel.Student)).ToList() //if teacher loads students, also load notes for students.
133+
};
134+
135+
var responseObject = new ResponseDTO<UsersSuccessDTO>
136+
{
137+
Status = "success",
138+
Data = userData
139+
};
140+
141+
return TypedResults.Ok(responseObject);
121142
}
122143

123144
[ProducesResponseType(StatusCodes.Status200OK)]
@@ -252,13 +273,20 @@ public static async Task<IResult> GetUserById(IRepository<User> userRepository,
252273
return TypedResults.NotFound();
253274
}
254275

255-
var userData = new UserDTO(response);
256-
// userData.CurrentStartdate = response.User_CC.ElementAt(0).CohortCourse.Cohort.StartDate;
276+
var userRole = claimsPrincipal.Role();
277+
var authorizedAsTeacher = AuthorizeTeacher(claimsPrincipal);
278+
279+
var userData = authorizedAsTeacher
280+
? new UserDTO(response, PrivilegeLevel.Teacher) //if teacher loads students, also load notes for students.
281+
: new UserDTO(response, PrivilegeLevel.Student); //if teacher loads students, also load notes for students.
282+
283+
257284
var responseObject = new ResponseDTO<UserDTO>
258285
{
259286
Status = "success",
260287
Data = userData
261288
};
289+
262290
return TypedResults.Ok(responseObject);
263291
}
264292

0 commit comments

Comments
 (0)