Skip to content

Commit

Permalink
Made getting all user courses
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaM1 committed Jan 6, 2024
1 parent 5f0d825 commit c5d0ed7
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Courses/Api/CourseController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backend.Base.Dto;
using Backend.Auth.Dto;
using Backend.Base.Dto;
using Backend.Courses.Dto;
using Backend.Courses.Logic;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -30,6 +31,13 @@ public async Task<CourseGetAllDto> GetAllCourses()
return await _courseService.GetAllCoursesAsync();
}

[HttpGet]
[Route(template: "users")]
public async Task<CourseGetAllDto> GetAllUserCoursesByUserId([FromHeader] UserAuthInfo authInfo)
{
return await _courseService.GetAllUserCoursesByUserIdAsync(authInfo);
}

[HttpPost]
public async Task<BaseIdDto> CreateCourse([FromBody] CourseCreateDto createCourseDto)
{
Expand Down
7 changes: 7 additions & 0 deletions Courses/Dal/CourseRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Backend.Courses.Dal.Interfaces;
using Backend.Courses.Dal.Models;

using Microsoft.EntityFrameworkCore;

namespace Backend.Courses.Dal;

public class CourseRepo : BaseRepo<CourseModel>, ICourseRepo
Expand All @@ -11,6 +13,11 @@ public CourseRepo(AppDatabase database) : base(database)

}

public async Task<List<CourseModel>> GetAllTeacherCoursesByCreatorId(int creatorId)
{
return await table.Where(course=> course.CreatorId == creatorId).ToListAsync();
}

public async Task UpdatePhotoPathAsync(int courseId, string photoPath)
{
var course = await this.GetEntityByIdAsync(courseId);
Expand Down
6 changes: 6 additions & 0 deletions Courses/Dal/GroupCoursesRepo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Backend.Base.Dal;
using Backend.Courses.Dal.Models;
using Backend.Courses.Dal.Interfaces;
using Microsoft.EntityFrameworkCore;

namespace Backend.Courses.Dal;

Expand All @@ -10,4 +11,9 @@ public GroupCoursesRepo(AppDatabase database) : base(database)
{

}

public async Task<List<int>> GetCourseIdsByGroupIdAsync(int groupId)
{
return await table.Where(x => x.GroupId == groupId).Select(x => x.CourseId).ToListAsync();
}
}
1 change: 1 addition & 0 deletions Courses/Dal/Interfaces/ICourseRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ namespace Backend.Courses.Dal.Interfaces;
public interface ICourseRepo : IBaseRepo<CourseModel>
{
public Task UpdatePhotoPathAsync(int courseId, string photoPath);
public Task<List<CourseModel>> GetAllTeacherCoursesByCreatorId(int creatorId);
}
2 changes: 1 addition & 1 deletion Courses/Dal/Interfaces/IGroupCoursesRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Backend.Courses.Dal.Interfaces;

public interface IGroupCoursesRepo : IBaseRepo<GroupCoursesModel>
{

public Task<List<int>> GetCourseIdsByGroupIdAsync(int groupId);
}
1 change: 1 addition & 0 deletions Courses/Dal/Interfaces/IStudentGroupsRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface IStudentGroupsRepo : IBaseRepo<StudentGroupsModel>
{
public Task<StudentGroupsModel?> GetStudentGroupByUserAndGroupIdAsync(int studentId, int groupId);
public Task<List<int>> GetAllStudentIdsByGroupIdAsync(int groupId);
public Task<List<int>> GetGroupIdsByStudentIdAsync(int studentId);
}
5 changes: 5 additions & 0 deletions Courses/Dal/StudentGroupsRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public async Task<List<int>> GetAllStudentIdsByGroupIdAsync(int groupId)
return await table.Where(studentConnection => studentConnection.GroupId == groupId).Select(studentConnection => studentConnection.StudentId).ToListAsync();
}

public async Task<List<int>> GetGroupIdsByStudentIdAsync(int studentId)
{
return await table.Where(conn => conn.StudentId == studentId).Select(conn => conn.GroupId).ToListAsync();
}

public async Task<StudentGroupsModel?> GetStudentGroupByUserAndGroupIdAsync(int studentId, int groupId)
{
return await table.Where(connection => connection.StudentId == studentId && connection.GroupId == groupId).FirstOrDefaultAsync();
Expand Down
65 changes: 64 additions & 1 deletion Courses/Logic/CourseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,40 @@
using Backend.Courses.Dal.Models;
using Backend.Base.Services.Interfaces;
using Backend.Base.Services;
using Backend.Auth.Dto;
using Backend.Auth.Dal.Interfaces;

namespace Backend.Courses.Logic;

public class CourseService
{
private ICourseRepo _courseRepo;
private ICourseChaptersRepo _courseChaptersRepo;
private IStudentRepo _studentRepo;
private IStudentGroupsRepo _studentGroupsRepo;
private IGroupRepo _groupRepo;
private IGroupCoursesRepo _groupCoursesRepo;

private IFileManager _fileManager;
private IHttpContextAccessor _httpContextAccessor;

public CourseService(ICourseRepo courseRepo, ICourseChaptersRepo courseChaptersRepo, IFileManager fileManager, IHttpContextAccessor httpContextAccessor)
public CourseService(
ICourseRepo courseRepo,
ICourseChaptersRepo courseChaptersRepo,
IStudentRepo studentRepo,
IStudentGroupsRepo studentGroupsRepo,
IGroupRepo groupRepo,
IGroupCoursesRepo groupCoursesRepo,
IFileManager fileManager,
IHttpContextAccessor httpContextAccessor)
{
_courseRepo = courseRepo;
_courseChaptersRepo = courseChaptersRepo;
_studentRepo = studentRepo;
_studentGroupsRepo = studentGroupsRepo;
_groupRepo = groupRepo;
_groupCoursesRepo = groupCoursesRepo;

_fileManager = fileManager;
_httpContextAccessor = httpContextAccessor;
}
Expand All @@ -34,6 +54,15 @@ public async Task<CourseGetAllDto> GetAllCoursesAsync()
return MapCoursesToGetAllDto(courses);
}

public async Task<CourseGetAllDto> GetAllUserCoursesByUserIdAsync(UserAuthInfo authInfo)
{
var courses = authInfo.UserType == Auth.Enums.UserType.Teacher
? await GetAllTeacherCoursesByUserIdAsync(authInfo.Id)
: await GetCoursesOfAllGroups(await GetAllStudentGroupsByUserIdAsync(authInfo.Id));

return MapCoursesToGetAllDto(courses);
}

public async Task<int> CreateCourseAsync(CourseCreateDto courseCreateDto)
{
var newCourse = new CourseModel()
Expand All @@ -57,6 +86,40 @@ public async Task UpdateCourseAsync(int id, CourseUpdateDto courseUpdateDto)
await _courseRepo.UpdateEntityAsync(id, courseUpdateDto);
}

private async Task<List<GroupModel>> GetAllStudentGroupsByUserIdAsync(int userId)
{
var student = await _studentRepo.GetStudentByUserId(userId);
var studentGroupsIds = await _studentGroupsRepo.GetGroupIdsByStudentIdAsync(student.Id);

var groups = new List<GroupModel>();
foreach (var id in studentGroupsIds)
{
var group = await _groupRepo.GetEntityByIdAsync(id);
groups.Add(group);
}
return groups;
}

private async Task<List<CourseModel>> GetAllTeacherCoursesByUserIdAsync(int userId)
{
return await _courseRepo.GetAllTeacherCoursesByCreatorId(userId);
}

private async Task<List<CourseModel>> GetCoursesOfAllGroups(List<GroupModel> groups)
{
var courses = new List<CourseModel>();
foreach (var group in groups)
{
var groupId = group.Id;
var courseIds = await _groupCoursesRepo.GetCourseIdsByGroupIdAsync(groupId);

foreach (int courseId in courseIds)
courses.Add(await _courseRepo.GetEntityByIdAsync(courseId));
}

return courses;
}

private async Task<CourseGetOneDto> MapCourseToGetOneDto(CourseModel courseModel)
{
//var courseChapters = await GetChaptersByCourseId(courseModel.Id);
Expand Down

0 comments on commit c5d0ed7

Please sign in to comment.