From 569811df162194af3135154d21943b2b2b3ed135 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Sat, 7 Nov 2020 21:00:54 -0300
Subject: [PATCH] endpoint GET api/courses/GetById

---
 .../Tsi1.Api/Controllers/CourseController.cs  | 13 ++++++++++
 .../Dtos/CourseDetailDto.cs                   | 14 +++++++++++
 .../Tsi1.BusinessLayer/Dtos/FileDetailDto.cs  | 15 +++++++++++
 .../Dtos/SectionDetailDto.cs                  | 14 +++++++++++
 .../Dtos/SectionItemDetailDto.cs              | 15 +++++++++++
 .../Helpers/MappingProfile.cs                 | 11 ++++++++
 .../Interfaces/ICourseService.cs              |  1 +
 .../Services/CourseService.cs                 | 25 +++++++++++++++++++
 8 files changed, 108 insertions(+)
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseDetailDto.cs
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/FileDetailDto.cs
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionDetailDto.cs
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemDetailDto.cs

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs
index 7c82f7a..4a8d4b8 100644
--- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs
+++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs
@@ -225,5 +225,18 @@ namespace Tsi1.Api.Controllers
             return Ok(result.Data);
         }
 
+        [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
+        [HttpGet("GetById/{courseId}")]
+        public async Task<IActionResult> GetById(int courseId)
+        {
+            var result = await _courseService.GetById(courseId);
+            if (result.HasError)
+            {
+                return BadRequest(result.Message);
+            }
+
+            return Ok(result.Data);
+        }
+
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseDetailDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseDetailDto.cs
new file mode 100644
index 0000000..2aacbde
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseDetailDto.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Dtos
+{
+    public class CourseDetailDto
+    {
+        public int Id { get; set; }
+        public string Name { get; set; }
+        public int TenantId { get; set; }
+        public List<SectionDetailDto> Sections { get; set; }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/FileDetailDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/FileDetailDto.cs
new file mode 100644
index 0000000..3f28969
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/FileDetailDto.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Dtos
+{
+    public class FileDetailDto
+    {
+        public int Id { get; set; }
+
+        public string Name { get; set; }
+
+        public string Path { get; set; }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionDetailDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionDetailDto.cs
new file mode 100644
index 0000000..4a2cc5b
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionDetailDto.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Dtos
+{
+    public class SectionDetailDto
+    {
+        public int Id { get; set; }
+        public string Name { get; set; }
+        public int Order { get; set; }
+        public List<SectionItemDetailDto> SectionItems { get; set; }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemDetailDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemDetailDto.cs
new file mode 100644
index 0000000..9b69290
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemDetailDto.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Tsi1.BusinessLayer.Dtos
+{
+    public class SectionItemDetailDto
+    {
+        public int Id { get; set; }
+        public int Order { get; set; }
+        public int SectionItemTypeId { get; set; }
+        public ForumPreviewDto Forum { get; set; }
+        public FileDetailDto File { get; set; }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
index 3691446..4d7d53b 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs
@@ -36,6 +36,12 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<SectionItem, SectionItemCreateDto>();
             CreateMap<Section, SectionCreateDto>();
             CreateMap<SectionItemType, SectionItemTypeDto>();
+            
+            CreateMap<Course, CourseDetailDto>();
+            CreateMap<Section, SectionDetailDto>();
+            CreateMap<SectionItem, SectionItemDetailDto>();
+            CreateMap<File, FileDetailDto>();
+          
 
             CreateMap<ForumCreateDto, Forum>();
             CreateMap<ForumPreviewDto, Forum>();
@@ -61,6 +67,11 @@ namespace Tsi1.BusinessLayer.Helpers
             CreateMap<SectionItemCreateDto, SectionItem>();
             CreateMap<SectionCreateDto, Section>();
             CreateMap<SectionItemTypeDto, SectionItemType>();
+
+            CreateMap<CourseDetailDto, Course>();
+            CreateMap<SectionDetailDto, Section>();
+            CreateMap<SectionItemDetailDto, SectionItem>();
+            CreateMap<FileDetailDto, File>();
         }
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs
index 54b167f..836612b 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs
@@ -28,5 +28,6 @@ namespace Tsi1.BusinessLayer.Interfaces
 
         Task<ServiceResult<bool>> RemoveProfessorToCourse(ProfessorCourseDto professorCourseDto);
         Task<ServiceResult<List<UserPreviewDto>>> GetProfessors(int courseId);
+        Task<ServiceResult<CourseDetailDto>> GetById(int courseId);
     }
 }
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs
index 88c4278..fffcc93 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs
@@ -341,5 +341,30 @@ namespace Tsi1.BusinessLayer.Services
 
             return result;
         }
+
+        public async Task<ServiceResult<CourseDetailDto>> GetById(int courseId)
+        {
+            var result = new ServiceResult<CourseDetailDto>();
+
+            var course = await _context.Courses
+                .Include(x => x.Sections)
+                    .ThenInclude(x => x.SectionItems)
+                        .ThenInclude(x => x.Forum)
+                .Include(x => x.Sections)
+                    .ThenInclude(x => x.SectionItems)
+                        .ThenInclude(x => x.File)
+                .FirstOrDefaultAsync(x => x.Id == courseId);
+
+            if (course == null)
+            {
+                result.HasError = true;
+                result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, courseId));
+                return result;
+            }
+
+            result.Data = _mapper.Map<CourseDetailDto>(course);
+
+            return result;
+        }
     }
 }
-- 
GitLab