Skip to content

Commit 0751f45

Browse files
authored
Merge pull request #102 from boolean-uk/moveSpecialismFromUserToCourse
specialism is now linked to a course
2 parents 1484d20 + f8c7cd6 commit 0751f45

File tree

11 files changed

+78
-49
lines changed

11 files changed

+78
-49
lines changed

exercise.wwwapi/DTOs/Courses/CoursePostDTO.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace exercise.wwwapi.DTOs.Courses
44
public class CoursePostDTO
55
{
66
public string Name { get; set; }
7+
public string SpecialismName { get; set; }
78

89
}
910
}

exercise.wwwapi/DTOs/Courses/GetCourseDTO.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ public class GetCourseDTO
88
{
99
public int Id { get; set; }
1010
public string Name { get; set; }
11+
public string SpecialismName { get; set; }
1112
public ICollection<GetCourseModuleDTO> CourseModules { get; set; } = new List<GetCourseModuleDTO>();
1213
public GetCourseDTO(){}
1314
public GetCourseDTO(Course model)
1415
{
1516
Id = model.Id;
1617
Name = model.Name;
18+
SpecialismName = model.SpecialismName;
1719
CourseModules = model.CourseModules.Select(cm => new GetCourseModuleDTO(cm)).ToList();
1820
}
1921
}

exercise.wwwapi/DTOs/Notes/User_noNotesDTO.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class User_noNoteDTO
1919
public string? Github { get; set; }
2020
public string? Bio { get; set; }
2121
public string? PhotoUrl { get; set; }
22-
public Specialism? Specialism { get; set; }
2322

2423
public User_noNoteDTO() { }
2524
public User_noNoteDTO(User model)
@@ -34,7 +33,6 @@ public User_noNoteDTO(User model)
3433
Github = model.Github;
3534
Bio = model.Bio;
3635
PhotoUrl = model.PhotoUrl;
37-
Specialism = model.Specialism;
3836
}
3937
}
4038
}

exercise.wwwapi/DTOs/Users/PatchUserDTO.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class PatchUserDTO
1616
public string? Github { get; set; }
1717
public string? Username { get; set; }
1818
public string? Mobile { get; set; }
19-
public Specialism? Specialism { get; set; }
2019
public Role? Role { get; set; }
2120
}
2221
}

exercise.wwwapi/DTOs/Users/UserDTO.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class UserDTO
3232
public string? Mobile { get; set; }
3333

3434
[JsonPropertyName("specialism")]
35-
public Specialism? Specialism { get; set; }
35+
public string? Specialism { get; set; }
3636

3737
public int? CohortId { get; set; }
3838
public int? CohortCourseId { get; set; }
@@ -62,7 +62,7 @@ public UserDTO(User model)
6262
Github = model.Github;
6363
Username = model.Username;
6464
Mobile = model.Mobile;
65-
Specialism = model.Specialism;
65+
Specialism = model.User_CC?.LastOrDefault()?.CohortCourse.Course.SpecialismName;
6666
Role = model.Role.ToString();
6767
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId;
6868
CohortCourseId = model.User_CC?.LastOrDefault()?.Id; //autofetching the first element of usercc
@@ -80,8 +80,8 @@ public UserDTO(User model, PrivilegeLevel privilegeLevel)
8080
Bio = model.Bio;
8181
Github = model.Github;
8282
Username = model.Username;
83-
Mobile = model.Mobile;
84-
Specialism = model.Specialism;
83+
Mobile = model.Mobile;
84+
Specialism = model.User_CC?.LastOrDefault()?.CohortCourse?.Course?.SpecialismName;
8585
Role = model.Role.ToString();
8686
CohortId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId;
8787
CohortCourseId = model.User_CC?.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc

exercise.wwwapi/Data/ModelSeeder.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,7 @@ public class ModelSeeder
9999
Role.Student,
100100
Role.Teacher
101101
};
102-
private List<Specialism> _specialisms = new List<Specialism>()
103-
{
104-
Specialism.Frontend,
105-
Specialism.Backend,
106-
Specialism.Fullstack,
107-
Specialism.None
108-
};
102+
109103

110104
private List<string> _firstPart = new List<string>()
111105
{
@@ -201,7 +195,6 @@ public void Seed(ModelBuilder modelBuilder)
201195
Mobile = "1234567890",
202196
Github = "",
203197
Bio = "",
204-
Specialism = Specialism.Frontend,
205198
PhotoUrl = ""
206199
};
207200
_users.Add(user1);
@@ -218,7 +211,6 @@ public void Seed(ModelBuilder modelBuilder)
218211
Mobile = "1234123",
219212
Github = "",
220213
Bio = "",
221-
Specialism = Specialism.Backend,
222214
PhotoUrl = ""
223215
};
224216
_users.Add(user2);
@@ -235,7 +227,6 @@ public void Seed(ModelBuilder modelBuilder)
235227
Mobile = "55555555",
236228
Github = "",
237229
Bio = "",
238-
Specialism = Specialism.Frontend,
239230
PhotoUrl = ""
240231
};
241232
_users.Add(user3);
@@ -252,7 +243,6 @@ public void Seed(ModelBuilder modelBuilder)
252243
Mobile = "98987878",
253244
Github = "",
254245
Bio = "",
255-
Specialism = Specialism.Backend,
256246
PhotoUrl = ""
257247
};
258248
_users.Add(user4);
@@ -269,7 +259,6 @@ public void Seed(ModelBuilder modelBuilder)
269259
Mobile = "111222333",
270260
Github = "",
271261
Bio = "",
272-
Specialism = Specialism.Frontend,
273262
PhotoUrl = ""
274263
};
275264
_users.Add(user5);
@@ -292,7 +281,6 @@ public void Seed(ModelBuilder modelBuilder)
292281
Mobile = userRandom.Next(12345678, 23456789).ToString(),
293282
Github = $"{username}git",
294283
Bio = $"{_firstword[userRandom.Next(_firstword.Count)]}{_secondword[userRandom.Next(_secondword.Count)]}{_thirdword[userRandom.Next(_thirdword.Count)]}",
295-
Specialism = _specialisms[userRandom.Next(_specialisms.Count)],
296284
PhotoUrl = ""
297285
};
298286
_users.Add(user);
@@ -455,14 +443,17 @@ public void Seed(ModelBuilder modelBuilder)
455443
Course course1 = new Course
456444
{
457445
Id = 1,
458-
Name = "Java",
446+
Name = "Java Development",
447+
SpecialismName = "Java Developer"
448+
459449
};
460450
_courses.Add(course1);
461451

462452
Course course2 = new Course
463453
{
464454
Id = 2,
465-
Name = ".NET",
455+
Name = ".NET Development",
456+
SpecialismName = ".NET Developer"
466457
};
467458
_courses.Add(course2);
468459

exercise.wwwapi/Endpoints/CourseEndpoints.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static async Task<IResult> CreateCourse(IRepository<Course> repository,
6464
return TypedResults.BadRequest("Course data missing in request");
6565
}
6666

67-
Course newCourse = new Course { Name = postedCourse.Name };
67+
Course newCourse = new Course(postedCourse);
6868
repository.Insert(newCourse);
6969
await repository.SaveAsync();
7070
GetCourseDTO response = new GetCourseDTO(newCourse);
@@ -114,6 +114,7 @@ private static async Task<IResult> UpdateCourse(IRepository<Course> repository,
114114
return TypedResults.BadRequest("Missing update data in request");
115115
}
116116
course.Name = updatedCourse.Name;
117+
course.SpecialismName = updatedCourse.SpecialismName;
117118
repository.Update(course);
118119
await repository.SaveAsync();
119120

exercise.wwwapi/Endpoints/UserEndpoints.cs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ public static void ConfigureAuthApi(this WebApplication app)
4545
private static async Task<IResult> GetUsers(IRepository<User> userRepository, string? searchTerm,
4646
ClaimsPrincipal claimPrincipal)
4747
{
48-
var results = await userRepository.GetWithIncludes(x => x.Include(u => u.User_CC).ThenInclude(c => c.CohortCourse).ThenInclude(d => d.Cohort).Include(p => p.Notes));
48+
var results = await userRepository.GetWithIncludes(x => x
49+
.Include(u => u.User_CC)
50+
.ThenInclude(c => c.CohortCourse)
51+
.ThenInclude(d => d.Cohort)
52+
.Include(u => u.User_CC)
53+
.ThenInclude(c => c.CohortCourse)
54+
.ThenInclude(d => d.Course)
55+
.Include(p => p.Notes));
4956

5057
if (!string.IsNullOrWhiteSpace(searchTerm))
5158
{
@@ -92,7 +99,14 @@ private static async Task<IResult> GetUsers(IRepository<User> userRepository, st
9299
[ProducesResponseType(StatusCodes.Status200OK)]
93100
private static async Task<IResult> GetUsersByCohort(IRepository<Cohort> repository, int cohort_id, ClaimsPrincipal claimsPrincipal)
94101
{
95-
var response = await repository.GetByIdWithIncludes(a => a.Include(p => p.CohortCourses).ThenInclude(b => b.UserCCs).ThenInclude(a => a.User).ThenInclude(u => u.Notes), cohort_id);
102+
var response = await repository.GetByIdWithIncludes(a => a
103+
.Include(p => p.CohortCourses)
104+
.ThenInclude(b => b.Course)
105+
.Include(p => p.CohortCourses)
106+
.ThenInclude(b => b.UserCCs)
107+
.ThenInclude(a => a.User)
108+
.ThenInclude(u => u.Notes), cohort_id);
109+
96110
var results = response.CohortCourses.SelectMany(a => a.UserCCs).Select(a => a.User).ToList();
97111
var dto_results = results.Select(a => new UserDTO(a));
98112

@@ -118,7 +132,13 @@ private static async Task<IResult> GetUsersByCohort(IRepository<Cohort> reposito
118132
[ProducesResponseType(StatusCodes.Status200OK)]
119133
private static async Task<IResult> GetUsersByCohortCourse(IRepository<CohortCourse> ccRepository, int cc_id, ClaimsPrincipal claimsPrincipal)
120134
{
121-
var response = await ccRepository.GetByIdWithIncludes(a => a.Include(z => z.Cohort).Include(b => b.UserCCs).ThenInclude(a => a.User).ThenInclude(u => u.Notes), cc_id);
135+
var response = await ccRepository.GetByIdWithIncludes(a => a
136+
.Include(z => z.Cohort)
137+
.Include(z => z.Course)
138+
.Include(b => b.UserCCs)
139+
.ThenInclude(a => a.User)
140+
.ThenInclude(u => u.Notes), cc_id);
141+
122142
var results = response.UserCCs.Select(a => a.User).ToList();
123143
var dto_results = results.Select(a => new UserDTO(a));
124144

@@ -192,7 +212,6 @@ private static async Task<IResult> Register(PostUserDTO request, IRepository<Use
192212
Mobile = string.IsNullOrEmpty(request.Mobile) ? string.Empty : request.Mobile,
193213
Bio = string.IsNullOrEmpty(request.Bio) ? string.Empty : request.Bio,
194214
Github = string.IsNullOrEmpty(request.Github) ? string.Empty : request.Github,
195-
Specialism = Specialism.None,
196215
PhotoUrl = ""
197216
};
198217

@@ -213,7 +232,6 @@ private static async Task<IResult> Register(PostUserDTO request, IRepository<Use
213232
Username = user.Username,
214233
Email = user.Email,
215234
Mobile = user.Mobile,
216-
Specialism = user.Specialism,
217235
}
218236
}
219237
};
@@ -266,7 +284,14 @@ private static async Task<IResult> Login(LoginRequestDTO request, IRepository<Us
266284
[ProducesResponseType(StatusCodes.Status200OK)]
267285
public static async Task<IResult> GetUserById(IRepository<User> userRepository, int id, ClaimsPrincipal claimsPrincipal)
268286
{
269-
var response = await userRepository.GetByIdWithIncludes(x => x.Include(u => u.User_CC).ThenInclude(c => c.CohortCourse).ThenInclude(d => d.Cohort).Include(p => p.Notes), id);
287+
var response = await userRepository.GetByIdWithIncludes(x => x
288+
.Include(u => u.User_CC)
289+
.ThenInclude(c => c.CohortCourse)
290+
.ThenInclude(d => d.Cohort)
291+
.Include(u => u.User_CC)
292+
.ThenInclude(c => c.CohortCourse)
293+
.ThenInclude(d => d.Course)
294+
.Include(p => p.Notes), id);
270295

271296
if (response == null)
272297
{
@@ -303,7 +328,7 @@ public static async Task<IResult> UpdateUser(IRepository<User> userRepository, i
303328
{
304329
// Only teacher can edit protected fields
305330
var authorized = AuthorizeTeacher(claimsPrinciple);
306-
if (!authorized && (request.Specialism is not null
331+
if (!authorized && (request is not null
307332
|| request.Role is not null))
308333
{
309334
return Results.Unauthorized();
@@ -360,15 +385,20 @@ public static async Task<IResult> UpdateUser(IRepository<User> userRepository, i
360385
if (request.Github != null) user.Github = GITHUB_URL + request.Github;
361386
if (request.FirstName != null) user.FirstName = request.FirstName;
362387
if (request.LastName != null) user.LastName = request.LastName;
363-
if (request.Specialism != null)
364-
user.Specialism = (Specialism)request.Specialism;
365388
if (request.Role != null)
366389
user.Role = (Role)request.Role;
367390

368391
userRepository.Update(user);
369392
await userRepository.SaveAsync();
370393

371-
var result = await userRepository.GetByIdWithIncludes(x => x.Include(u => u.User_CC).ThenInclude(c => c.CohortCourse).ThenInclude(d => d.Cohort).Include(p => p.Notes), id);
394+
var result = await userRepository.GetByIdWithIncludes(x => x
395+
.Include(u => u.User_CC)
396+
.ThenInclude(c => c.CohortCourse)
397+
.ThenInclude(d => d.Cohort)
398+
.Include(u => u.User_CC)
399+
.ThenInclude(c => c.CohortCourse)
400+
.ThenInclude(d => d.Course)
401+
.Include(p => p.Notes), id);
372402

373403
var response = new ResponseDTO<UserDTO>()
374404
{
@@ -395,7 +425,14 @@ public static async Task<IResult> DeleteUser(IRepository<User> userRepository, i
395425
return Results.Unauthorized();
396426
}
397427

398-
var user = await userRepository.GetByIdWithIncludes(x => x.Include(u => u.User_CC).ThenInclude(c => c.CohortCourse).ThenInclude(d => d.Cohort).Include(p => p.Notes), id);
428+
var user = await userRepository.GetByIdWithIncludes(x => x
429+
.Include(u => u.User_CC)
430+
.ThenInclude(c => c.CohortCourse)
431+
.ThenInclude(d => d.Cohort)
432+
.Include(u => u.User_CC)
433+
.ThenInclude(c => c.CohortCourse)
434+
.ThenInclude(d => d.Course)
435+
.Include(p => p.Notes), id);
399436
if (user == null)
400437
{
401438
return TypedResults.NotFound();

exercise.wwwapi/Factories/UserFactory.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,7 @@ public static class UserFactory
1010
{
1111
public static UserDTO GetUserDTO(User user, PrivilegeLevel privilegeLevel)
1212
{
13-
var userDTO = new UserDTO()
14-
{
15-
FirstName = user.FirstName,
16-
LastName = user.LastName,
17-
Bio = user.Bio,
18-
Github = user.Github,
19-
Username = user.Username,
20-
Email = user.Email,
21-
Mobile = user.Mobile,
22-
Specialism = user.Specialism,
23-
Role = user.Role.ToString()
24-
};
13+
var userDTO = new UserDTO(user);
2514

2615
if (privilegeLevel == PrivilegeLevel.Teacher)
2716
{

exercise.wwwapi/Models/Course.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using exercise.wwwapi.Repository;
1+
using exercise.wwwapi.DTOs.Courses;
2+
using exercise.wwwapi.Repository;
23
using System.ComponentModel.DataAnnotations;
34
using System.ComponentModel.DataAnnotations.Schema;
45

@@ -15,8 +16,20 @@ public class Course : IEntity
1516
[Column("name", TypeName = "varchar(100)")]
1617
public string Name { get; set; }
1718

19+
[Required]
20+
[Column("specialism_name", TypeName = "varchar(100)")]
21+
public string SpecialismName { get; set; }
22+
1823
public ICollection<CohortCourse> CohortCourses { get; set; } = new List<CohortCourse>();
1924
public ICollection<CourseModule> CourseModules { get; set; } = new List<CourseModule>();
2025

26+
public Course(){}
27+
28+
public Course(CoursePostDTO model)
29+
{
30+
Name = model.Name;
31+
SpecialismName = model.SpecialismName;
32+
}
33+
2134
}
2235

0 commit comments

Comments
 (0)