From cdce3c9a29975b20a3acb75e70ebfa88dc852c2a Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Sun, 25 Oct 2020 11:49:08 -0300
Subject: [PATCH] fix endpoint MyCourses

---
 Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs |  3 +--
 .../Interfaces/ICourseService.cs                  |  2 +-
 .../Tsi1.BusinessLayer/Services/CourseService.cs  | 15 ++++++++++++---
 3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs
index c94df79..19b56f8 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs
@@ -30,9 +30,8 @@ namespace Tsi1.Api.Controllers
         {
             var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
             var userType = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value;
-            var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
 
-            var result = await _courseService.GetCoursePreviews(userType, tenantId);
+            var result = await _courseService.GetCoursePreviews(userId, userType);
             if (result.HasError)
             {
                 return BadRequest(result.Message);
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs
index 2d89f2d..082798f 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs
@@ -10,7 +10,7 @@ namespace Tsi1.BusinessLayer.Interfaces
 {
     public interface ICourseService
     {
-        Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(string userType, int tenantId);
+        Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType);
 
         Task<ServiceResult<Course>> Create(CourseCreateDto newCourse);
 
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs
index 8ca7b40..4f8db3f 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs
@@ -48,17 +48,26 @@ namespace Tsi1.BusinessLayer.Services
             return result;
         }
 
-        public async Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(string userType, int tenantId)
+        public async Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType)
         {
             var result = new ServiceResult<List<CoursePreviewDto>>();
 
             var courses = new List<Course>();
 
+            var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);
+
+            if (user == null)
+            {
+                result.HasError = true;
+                result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
+                return result;
+            }
+
             if (userType == UserTypes.Student)
             {
                 courses = await _context.StudentCourses
                     .Include(x => x.Course)
-                    .Where(x => x.Course.TenantId == tenantId)
+                    .Where(x => x.StudentId == user.StudentId)
                     .Select(x => x.Course)
                     .ToListAsync();
             }
@@ -66,7 +75,7 @@ namespace Tsi1.BusinessLayer.Services
             {
                 courses = await _context.ProfessorCourses
                     .Include(x => x.Course)
-                    .Where(x => x.Course.TenantId == tenantId)
+                    .Where(x => x.ProfessorId == user.ProfessorId)
                     .Select(x => x.Course)
                     .ToListAsync();
             }
-- 
GitLab