From bc5a1a587c42b1a31799ef77705ffa4ed2640c53 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Wed, 25 Nov 2020 22:41:38 -0300
Subject: [PATCH] add endpoint GetAllCourseCommunications

---
 .../Controllers/CommunicationController.cs    | 16 +++++
 .../Interfaces/ICommunicationService.cs       |  1 +
 .../Services/CommunicationService.cs          | 69 +++++++++++++++----
 3 files changed, 72 insertions(+), 14 deletions(-)

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CommunicationController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CommunicationController.cs
index d5e9b2d..479c0d7 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/CommunicationController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/CommunicationController.cs
@@ -99,6 +99,22 @@ namespace Tsi1.Api.Controllers
             return Ok(result.Data);
         }
 
+        [Authorize(Roles = UserTypes.FacultyAdmin)]
+        [HttpGet("GetAllCourseCommunications")]
+        public async Task<IActionResult> GetAllCourseCommunications()
+        {
+            var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
+
+            var result = await _communicationService.GetAllCourseCommunications(tenantId);
+
+            if (result.HasError)
+            {
+                return BadRequest(result.Message);
+            }
+
+            return Ok(result.Data);
+        }
+
         [Authorize(Roles = UserTypes.Student + ", " + UserTypes.FacultyAdmin + ", " + UserTypes.Professor + ", " + UserTypes.UdelarAdmin)]
         [HttpGet("GetGlobalCommunications")]
         public async Task<IActionResult> GetGlobalCommunications(int tenantId)
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICommunicationService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICommunicationService.cs
index b215bd9..0c14aae 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICommunicationService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICommunicationService.cs
@@ -21,5 +21,6 @@ namespace Tsi1.BusinessLayer.Interfaces
 
         Task<ServiceResult<bool>> TenantValidation(int tenantId, int courseId);
         Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllGlobalCommunications();
+        Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllCourseCommunications(int tenantId);
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CommunicationService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CommunicationService.cs
index 039347c..ffcd09d 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CommunicationService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CommunicationService.cs
@@ -102,6 +102,13 @@ namespace Tsi1.BusinessLayer.Services
 
             var validation = await this.TenantValidation(tenantId, courseId);
 
+            if (validation.HasError)
+            {
+                result.HasError = true;
+                result.AddMessage(validation.Message);
+                return result;
+            }
+
             var communications = await _context.Communications
                 .AsNoTracking()
                 .Include(x => x.Course)
@@ -142,6 +149,13 @@ namespace Tsi1.BusinessLayer.Services
             var tenantAdmin = await _context.Tenants.AsNoTracking().FirstOrDefaultAsync(x => x.Name == TenantAdmin.Name);
             var course = await _context.Courses.AsNoTracking().FirstOrDefaultAsync(x => x.Id == courseId);
 
+            if (course == null)
+            {
+                result.HasError = true;
+                result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, courseId));
+                return result;
+            }
+
             if (tenantAdmin.Id != tenantId)
             {
                 result.HasError = course.TenantId != tenantId;
@@ -151,6 +165,47 @@ namespace Tsi1.BusinessLayer.Services
             return result;
         }
 
+
+        public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllGlobalCommunications()
+        {
+            var result = new ServiceResult<List<CommunicationPreviewDto>>();
+
+            var communications = await _context.Communications
+                .AsNoTracking()
+                .Include(x => x.Tenant)
+                .Where(x => x.IsGlobal)
+                .ToListAsync();
+
+            result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications);
+
+            return result;
+        }
+
+        public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllCourseCommunications(int tenantId)
+        {
+            var result = new ServiceResult<List<CommunicationPreviewDto>>();
+
+            var tenant = await _context.Tenants.Include(x => x.Courses).FirstOrDefaultAsync(x => x.Id == tenantId);
+
+            if (tenant == null)
+            {
+                result.HasError = true;
+                result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, tenantId));
+            }
+
+            var courseIds = tenant.Courses.Select(x => x.Id);
+
+            var communications = await _context.Communications
+                .AsNoTracking()
+                .Include(x => x.Course)
+                .Where(x => courseIds.Contains(x.Course.Id))
+                .ToListAsync();
+
+            result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications);
+
+            return result;
+        }
+
         private async Task<ServiceResult<bool>> CreateValidation(int id, bool isGlobal, Communication commmunication)
         {
             var result = new ServiceResult<bool>();
@@ -185,19 +240,5 @@ namespace Tsi1.BusinessLayer.Services
             return result;
         }
 
-        public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetAllGlobalCommunications()
-        {
-            var result = new ServiceResult<List<CommunicationPreviewDto>>();
-
-            var communications = await _context.Communications
-                .AsNoTracking()
-                .Include(x => x.Tenant)
-                .Where(x => x.IsGlobal)
-                .ToListAsync();
-
-            result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications);
-
-            return result;
-        }
     }
 }
-- 
GitLab