Skip to content

Commit 7616bc2

Browse files
authored
Merge pull request #103 from boolean-uk/moveStudentEndpoint
added ability to move student
2 parents 0751f45 + 35ae950 commit 7616bc2

File tree

5 files changed

+86
-23
lines changed

5 files changed

+86
-23
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using exercise.wwwapi.Models;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
5+
namespace exercise.wwwapi.DTOs.CohortCourse
6+
{
7+
public class PostUserCohortCourseDTO
8+
{
9+
public int CohortId { get; set; }
10+
public int CourseId { get; set; }
11+
}
12+
}

exercise.wwwapi/DTOs/Users/UserDTO.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public UserDTO(User model)
6262
Github = model.Github;
6363
Username = model.Username;
6464
Mobile = model.Mobile;
65-
Specialism = model.User_CC?.LastOrDefault()?.CohortCourse.Course.SpecialismName;
65+
Specialism = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse.Course.SpecialismName;
6666
Role = model.Role.ToString();
67-
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId;
68-
CohortCourseId = model.User_CC?.LastOrDefault()?.Id; //autofetching the first element of usercc
69-
CurrentStartdate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
70-
CurrentEnddate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
67+
CohortId = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse.CohortId;
68+
CohortCourseId = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse.Id; //autofetching the first element of usercc
69+
CurrentStartdate = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
70+
CurrentEnddate = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
7171
Notes = model.Notes.Select(n => new NoteDTO(n)).ToList();
7272
}
7373

@@ -81,12 +81,12 @@ public UserDTO(User model, PrivilegeLevel privilegeLevel)
8181
Github = model.Github;
8282
Username = model.Username;
8383
Mobile = model.Mobile;
84-
Specialism = model.User_CC?.LastOrDefault()?.CohortCourse?.Course?.SpecialismName;
84+
Specialism = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse?.Course?.SpecialismName;
8585
Role = model.Role.ToString();
86-
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId;
87-
CohortCourseId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc
88-
CurrentStartdate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
89-
CurrentEnddate = model.User_CC?.LastOrDefault()?.CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
86+
CohortId = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse.CohortId;
87+
CohortCourseId = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse.Id; //autofetching the first element of usercc
88+
CurrentStartdate = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse.Cohort.StartDate; //autofetching the first element of usercc
89+
CurrentEnddate = model.User_CC?.OrderBy(a => a.Id).LastOrDefault()?.CohortCourse.Cohort.EndDate; //autofetching the first element of usercc
9090

9191

9292
if (privilegeLevel == PrivilegeLevel.Teacher)

exercise.wwwapi/Endpoints/CohortCourseEndpoints.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
using exercise.wwwapi.DTOs.Courses;
44
using exercise.wwwapi.DTOs.Exercises;
55
using exercise.wwwapi.Enums;
6+
using exercise.wwwapi.Helpers;
67
using exercise.wwwapi.Models;
78
using exercise.wwwapi.Repository;
89
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.EntityFrameworkCore;
1011
using Npgsql;
12+
using System.Net.NetworkInformation;
13+
using System.Security.Claims;
1114

1215
namespace exercise.wwwapi.Endpoints;
1316

@@ -18,9 +21,44 @@ public static void ConfigureCohortCourseEndpoints(this WebApplication app)
1821
var cohortcourses = app.MapGroup("cohortcourses");
1922
cohortcourses.MapGet("/", GetAllCohortCourses).WithSummary("Get all cohort_courses");
2023
cohortcourses.MapGet("/{id}", GetCohortCourseById).WithSummary("Get cohort_course by id");
24+
cohortcourses.MapPost("/moveUser{user_id}", MoveUser).WithSummary("Creates a new user_cc to move student");
2125
}
22-
26+
2327
[ProducesResponseType(StatusCodes.Status200OK)]
28+
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
29+
[ProducesResponseType(StatusCodes.Status404NotFound)]
30+
public static async Task<IResult> MoveUser(IRepository<UserCC> userCCRepository, IRepository<CohortCourse> cohortCourseRepository, IRepository<User> userRepository, int user_id, PostUserCohortCourseDTO userCC, ClaimsPrincipal claimPrincipal)
31+
{
32+
if (userCC == null)
33+
{
34+
return TypedResults.BadRequest("No user_id provided");
35+
}
36+
var cohortCourse = await cohortCourseRepository.GetWithIncludes(a => a.Where(b => b.CohortId == userCC.CohortId && b.CourseId == userCC.CourseId));
37+
if (cohortCourse == null || cohortCourse.Count == 0)
38+
{
39+
return TypedResults.NotFound("No cohort_course with that cohort_id and course_id exists");
40+
}
41+
//implement later
42+
//if usercc-combo already exists, delete old and create new
43+
44+
var userRole = claimPrincipal.Role();
45+
var authorizedAsTeacher = claimPrincipal.IsInRole("Teacher");
46+
if(!authorizedAsTeacher)
47+
{
48+
return TypedResults.Unauthorized();
49+
}
50+
userCCRepository.Insert(new UserCC
51+
{
52+
UserId = user_id,
53+
CcId = cohortCourse.First().Id
54+
});
55+
await userCCRepository.SaveAsync();
56+
return TypedResults.Ok("User moved successfully");
57+
}
58+
59+
60+
61+
[ProducesResponseType(StatusCodes.Status200OK)]
2462
[ProducesResponseType(StatusCodes.Status404NotFound)]
2563
public static async Task<IResult> GetAllCohortCourses(IRepository<CohortCourse> cohortCourseRepository)
2664
{

exercise.wwwapi/Endpoints/UserEndpoints.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,30 @@ private static async Task<IResult> GetUsers(IRepository<User> userRepository, st
9999
[ProducesResponseType(StatusCodes.Status200OK)]
100100
private static async Task<IResult> GetUsersByCohort(IRepository<Cohort> repository, int cohort_id, ClaimsPrincipal claimsPrincipal)
101101
{
102-
var response = await repository.GetByIdWithIncludes(a => a
102+
var response = await repository.GetWithIncludes(a => a
103103
.Include(p => p.CohortCourses)
104104
.ThenInclude(b => b.Course)
105105
.Include(p => p.CohortCourses)
106106
.ThenInclude(b => b.UserCCs)
107107
.ThenInclude(a => a.User)
108-
.ThenInclude(u => u.Notes), cohort_id);
108+
.ThenInclude(u => u.Notes));
109+
110+
var cohortCourses = response.SelectMany(c => c.CohortCourses).ToList();
111+
var userCohortCourses = cohortCourses.SelectMany(cc => cc.UserCCs).ToList();
112+
var groupedUserCohortCourses = userCohortCourses.GroupBy(uc => uc.UserId).ToList();
113+
var currentUserCohortCourses = groupedUserCohortCourses.Select(g => g.OrderBy(f => f.Id).LastOrDefault()).ToList();
114+
var userCohortCoursesInCohort = currentUserCohortCourses.Where(f => f.CohortCourse.CohortId == cohort_id).ToList();
115+
var usersInCohort = userCohortCoursesInCohort.Select(uc => uc.User).ToList();
116+
117+
109118

110-
var results = response.CohortCourses.SelectMany(a => a.UserCCs).Select(a => a.User).ToList();
111-
var dto_results = results.Select(a => new UserDTO(a));
112119

113120
var userRole = claimsPrincipal.Role();
114121
var authorizedAsTeacher = AuthorizeTeacher(claimsPrincipal);
115122

116123
var userData = new UsersSuccessDTO
117124
{
118-
Users = results.Select(user => authorizedAsTeacher
125+
Users = usersInCohort.Select(user => authorizedAsTeacher
119126
? new UserDTO(user, PrivilegeLevel.Teacher) //if teacher loads students, also load notes for students.
120127
: new UserDTO(user, PrivilegeLevel.Student)).ToList() //if teacher loads students, also load notes for students.
121128
};
@@ -132,22 +139,27 @@ private static async Task<IResult> GetUsersByCohort(IRepository<Cohort> reposito
132139
[ProducesResponseType(StatusCodes.Status200OK)]
133140
private static async Task<IResult> GetUsersByCohortCourse(IRepository<CohortCourse> ccRepository, int cc_id, ClaimsPrincipal claimsPrincipal)
134141
{
135-
var response = await ccRepository.GetByIdWithIncludes(a => a
142+
var response = await ccRepository.GetWithIncludes(a => a
136143
.Include(z => z.Cohort)
137144
.Include(z => z.Course)
138145
.Include(b => b.UserCCs)
139-
.ThenInclude(a => a.User)
140-
.ThenInclude(u => u.Notes), cc_id);
141-
142-
var results = response.UserCCs.Select(a => a.User).ToList();
143-
var dto_results = results.Select(a => new UserDTO(a));
146+
.ThenInclude(a => a.User)
147+
.ThenInclude(u => u.Notes));
148+
149+
var userCohortCourses = response.SelectMany(cc => cc.UserCCs).ToList();
150+
var groupedUserCohortCourses = userCohortCourses.GroupBy(uc => uc.UserId).ToList();
151+
var currentUserCohortCourses = groupedUserCohortCourses.Select(g => g.OrderBy(f => f.Id).LastOrDefault()).ToList();
152+
var userCohortCoursesInCohortCourse = currentUserCohortCourses.Where(f => f.CohortCourse.Id == cc_id).ToList();
153+
var usersInCohortCourse = userCohortCoursesInCohortCourse.Select(uc => uc.User).ToList();
154+
155+
144156

145157
var userRole = claimsPrincipal.Role();
146158
var authorizedAsTeacher = AuthorizeTeacher(claimsPrincipal);
147159

148160
var userData = new UsersSuccessDTO
149161
{
150-
Users = results.Select(user => authorizedAsTeacher
162+
Users = usersInCohortCourse.Select(user => authorizedAsTeacher
151163
? new UserDTO(user, PrivilegeLevel.Teacher) //if teacher loads students, also load notes for students.
152164
: new UserDTO(user, PrivilegeLevel.Student)).ToList() //if teacher loads students, also load notes for students.
153165
};

exercise.wwwapi/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
builder.Services.AddScoped<IRepository<Note>, Repository<Note>>();
4747
builder.Services.AddScoped<IRepository<CohortCourse>, Repository<CohortCourse>>();
4848
builder.Services.AddScoped<IRepository<Exercise>, Repository<Exercise>>();
49+
builder.Services.AddScoped<IRepository<UserCC>, Repository<UserCC>>();
4950

5051
// Register general services
5152
builder.Services.AddScoped<IConfigurationSettings, ConfigurationSettings>();

0 commit comments

Comments
 (0)