Skip to content

Commit 95d35ae

Browse files
committed
wip
1 parent 52a2257 commit 95d35ae

10 files changed

Lines changed: 27 additions & 24 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<CSharpLanguageVersion>8</CSharpLanguageVersion>
4+
<WarningsAsErrors>0612</WarningsAsErrors>
45
</PropertyGroup>
56

67
<PropertyGroup>

HwProj.Common/HwProj.Repositories/ReadOnlyRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task<TEntity> GetAsync(TKey id)
3232
return await Context.FindAsync<TEntity>(id).ConfigureAwait(false);
3333
}
3434

35-
public async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate)
35+
public virtual async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate)
3636
{
3737
return await Context.Set<TEntity>().AsNoTracking().FirstOrDefaultAsync(predicate).ConfigureAwait(false);
3838
}

HwProj.CoursesService/HwProj.CoursesService.API/Domains/MappingExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static CourseDTO ToCourseDto(this Course course)
8585
Name = course.Name,
8686
GroupName = course.GroupName,
8787
IsCompleted = course.IsCompleted,
88-
MentorIds = course.MentorIds.Split("/"),
88+
MentorIds = course.Mentors.Select(x => x.UserId).ToArray(),
8989
IsOpen = course.IsOpen,
9090
InviteCode = course.InviteCode,
9191
CourseMates = course.CourseMates.Select(cm => cm.ToCourseMateViewModel()).ToArray(),
@@ -99,7 +99,7 @@ public static CoursePreview ToCoursePreview(this Course course)
9999
Name = course.Name,
100100
GroupName = course.GroupName,
101101
IsCompleted = course.IsCompleted,
102-
MentorIds = course.MentorIds.Split("/"),
102+
MentorIds = course.Mentors.Select(x => x.UserId).ToArray(),
103103
};
104104

105105
public static HomeworkTask ToHomeworkTask(this CreateTaskViewModel createTaskViewModel)

HwProj.CoursesService/HwProj.CoursesService.API/Events/LecturerAcceptToCourseEvent.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public class LecturerAcceptToCourseEvent : Event
66
{
77
public long CourseId { get; set; }
88
public string CourseName { get; set; }
9-
public string MentorIds { get; set; }
109
public string StudentId { get; set; }
1110
}
1211
}

HwProj.CoursesService/HwProj.CoursesService.API/Events/LecturerRejectToCourseEvent.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public class LecturerRejectToCourseEvent : Event
66
{
77
public long CourseId { get; set; }
88
public string CourseName { get; set; }
9-
public string MentorIds { get; set; }
109
public string StudentId { get; set; }
1110
}
1211
}

HwProj.CoursesService/HwProj.CoursesService.API/Events/NewCourseMateEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class NewCourseMateEvent : Event
66
{
77
public long CourseId { get; set; }
88
public string CourseName { get; set; }
9-
public string MentorIds { get; set; }
9+
public string[] MentorIds { get; set; }
1010
public string StudentId { get; set; }
1111
public bool IsAccepted { get; set; }
1212
}

HwProj.CoursesService/HwProj.CoursesService.API/Models/CourseContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace HwProj.CoursesService.API.Models
55
public sealed class CourseContext : DbContext
66
{
77
public DbSet<Course> Courses { get; set; }
8+
public DbSet<CourseMentor> Mentors { get; set; }
89
public DbSet<CourseMate> CourseMates { get; set; }
910
public DbSet<Group> Groups { get; set; }
1011
public DbSet<GroupMate> GroupMates { get; set; }

HwProj.CoursesService/HwProj.CoursesService.API/Repositories/CoursesRepository.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
3+
using System.Linq.Expressions;
24
using System.Threading.Tasks;
35
using HwProj.CoursesService.API.Models;
46
using HwProj.Repositories;
57
using Microsoft.EntityFrameworkCore;
68

79
namespace HwProj.CoursesService.API.Repositories
810
{
11+
// TODO: include mentors by default
912
public class CoursesRepository : CrudRepository<Course, long>, ICoursesRepository
1013
{
1114
public CoursesRepository(CourseContext context)
1215
: base(context)
1316
{
1417
}
1518

19+
public override async Task<Course> FindAsync(Expression<Func<Course, bool>> predicate)
20+
{
21+
return await Context.Set<Course>().AsNoTracking().Include(x => x.Mentors).FirstOrDefaultAsync(predicate);
22+
}
23+
1624
public Task<Course?> GetWithCourseMates(long id) =>
1725
Context.Set<Course>()
26+
.Include(x => x.Mentors)
1827
.Include(c => c.CourseMates)
1928
.ThenInclude(c => c.Characteristics)
2029
.AsNoTracking()
2130
.FirstOrDefaultAsync(c => c.Id == id);
2231

2332
public async Task<Course?> GetWithHomeworksAsync(long id)
2433
=> await Context.Set<Course>()
34+
.Include(x => x.Mentors)
2535
.Include(c => c.Homeworks)
2636
.ThenInclude(h => h.Tasks)
2737
.AsNoTracking()
@@ -30,6 +40,7 @@ public CoursesRepository(CourseContext context)
3040
public async Task<Course?> GetWithCourseMatesAndHomeworksAsync(long id)
3141
{
3242
var course = await Context.Set<Course>()
43+
.Include(x => x.Mentors)
3344
.Include(c => c.CourseMates)
3445
.ThenInclude(c => c.Characteristics)
3546
.Include(c => c.Homeworks)
@@ -45,6 +56,7 @@ public CoursesRepository(CourseContext context)
4556
public IQueryable<Course> GetAllWithCourseMatesAndHomeworks()
4657
{
4758
return Context.Set<Course>()
59+
.Include(x => x.Mentors)
4860
.Include(c => c.CourseMates)
4961
.ThenInclude(c => c.Characteristics)
5062
.Include(c => c.Homeworks)

HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ public async Task<long> AddFromTemplateAsync(CourseTemplate courseTemplate, List
113113
using var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
114114

115115
var course = courseTemplate.ToCourse();
116-
course.MentorIds = mentorId;
117116
course.InviteCode = Guid.NewGuid().ToString();
118117
var courseId = await _coursesRepository.AddAsync(course);
119118

119+
await _context.Mentors.AddAsync(new CourseMentor() { CourseId = courseId, UserId = mentorId });
120+
120121
var homeworks = courseTemplate.Homeworks.Select(hwTemplate => hwTemplate.ToHomework(courseId));
121122
var homeworkIds = await _homeworksRepository.AddRangeAsync(homeworks);
122123

@@ -141,7 +142,6 @@ public async Task<long> AddFromTemplateAsync(CourseTemplate courseTemplate, List
141142
{
142143
CourseId = courseId,
143144
CourseName = course.Name,
144-
MentorIds = course.MentorIds,
145145
StudentId = student.StudentId
146146
});
147147
}
@@ -187,7 +187,7 @@ public async Task<bool> AddStudentAsync(long courseId, string studentId)
187187
{
188188
CourseId = courseId,
189189
CourseName = course.Name,
190-
MentorIds = course.MentorIds,
190+
MentorIds = course.Mentors.Select(x => x.UserId).ToArray(),
191191
StudentId = studentId,
192192
IsAccepted = false
193193
});
@@ -209,7 +209,6 @@ public async Task<bool> AcceptCourseMateAsync(long courseId, string studentId)
209209
{
210210
CourseId = courseId,
211211
CourseName = course.Name,
212-
MentorIds = course.MentorIds,
213212
StudentId = studentId
214213
});
215214

@@ -230,7 +229,6 @@ public async Task<bool> RejectCourseMateAsync(long courseId, string studentId)
230229
{
231230
CourseId = courseId,
232231
CourseName = course.Name,
233-
MentorIds = course.MentorIds,
234232
StudentId = studentId
235233
});
236234
return true;
@@ -241,7 +239,7 @@ public async Task<CourseDTO[]> GetUserCoursesAsync(string userId, string role)
241239
var isMentor = role == Roles.LecturerRole || role == Roles.ExpertRole;
242240

243241
var courses = isMentor
244-
? _coursesRepository.FindAll(c => c.MentorIds.Contains(userId))
242+
? _coursesRepository.FindAll(c => c.Mentors.Any(m => m.UserId == userId))
245243
: _coursesRepository.FindAll(c => c.CourseMates.Any(cm => cm.IsAccepted && cm.StudentId == userId));
246244

247245
var coursesWithValues = await courses
@@ -271,23 +269,16 @@ public async Task<bool> AcceptLecturerAsync(long courseId, string lecturerEmail,
271269
{
272270
var course = await _coursesRepository.GetAsync(courseId);
273271
if (course == null) return false;
274-
if (!course.MentorIds.Contains(lecturerId))
272+
if (_context.Mentors.All(x => x.UserId != lecturerId))
275273
{
276-
var newMentors = course.MentorIds + "/" + lecturerId;
277-
await _coursesRepository.UpdateAsync(courseId, с => new Course
278-
{
279-
MentorIds = newMentors,
280-
});
281-
274+
_context.Mentors.Add(new CourseMentor() { CourseId = courseId, UserId = lecturerId });
282275
_eventBus.Publish(new LecturerInvitedToCourseEvent
283276
{
284277
CourseId = courseId,
285278
CourseName = course.Name,
286279
MentorId = lecturerId,
287280
MentorEmail = lecturerEmail
288281
});
289-
//TODO: remove
290-
await RejectCourseMateAsync(courseId, lecturerId);
291282
}
292283

293284
return true;
@@ -296,7 +287,7 @@ public async Task<bool> AcceptLecturerAsync(long courseId, string lecturerEmail,
296287
public async Task<string[]> GetCourseLecturers(long courseId)
297288
{
298289
var course = await _coursesRepository.GetAsync(courseId);
299-
return course.MentorIds.Split('/');
290+
return course.Mentors.Select(x => x.UserId).ToArray();
300291
}
301292

302293
public async Task<bool> HasStudent(long courseId, string studentId)

HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public override async Task HandleAsync(NewCourseMateEvent @event)
3838
var user = await _authServiceClient.GetAccountData(@event.StudentId);
3939
var url = _configuration["Url"];
4040

41-
var mentorIds = @event.MentorIds.Split('/');
41+
var mentorIds = @event.MentorIds;
4242
var mentors = await _authServiceClient.GetAccountsData(mentorIds);
4343

4444
//TODO: fix

0 commit comments

Comments
 (0)