From 1332683193f0dc96c04499039819b0eeaf1715a3 Mon Sep 17 00:00:00 2001
From: esantangelo <enzo020895@gmail.com>
Date: Sat, 7 Nov 2020 21:00:24 -0300
Subject: [PATCH] data load

---
 .../Controllers/DataLoadController.cs         |  32 +++
 Tsi1.Api/Tsi1.Api/Startup.cs                  |   2 +
 .../Tsi1.BusinessLayer/DataLoad/DataLoad.cs   | 263 ++++++++++++++++++
 .../Tsi1.BusinessLayer/DataLoad/IDataLoad.cs  |  12 +
 .../Interfaces/ISectionService.cs             |   2 +-
 .../Services/SectionService.cs                |   5 +-
 6 files changed, 313 insertions(+), 3 deletions(-)
 create mode 100644 Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs
 create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs

diff --git a/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs b/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs
new file mode 100644
index 0000000..d97bd21
--- /dev/null
+++ b/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs
@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Tsi1.BusinessLayer.DataLoad;
+
+namespace Tsi1.Api.Controllers
+{
+    [Route("api/[controller]")]
+    [ApiController]
+    public class DataLoadController : ControllerBase
+    {
+        private readonly IDataLoad _dataLoad;
+
+        public DataLoadController(IDataLoad dataLoad)
+        {
+            _dataLoad = dataLoad;
+        }
+
+        [AllowAnonymous]
+        [HttpGet("LoadData")]
+        public async Task<IActionResult> LoadData()
+        {
+            await _dataLoad.LoadDataAsync();
+
+            return Ok();
+        }
+    }
+}
diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs
index 6e94cdd..f095493 100644
--- a/Tsi1.Api/Tsi1.Api/Startup.cs
+++ b/Tsi1.Api/Tsi1.Api/Startup.cs
@@ -23,6 +23,7 @@ using Tsi1.BusinessLayer.Helpers;
 using Tsi1.BusinessLayer.HostedServices;
 using Tsi1.BusinessLayer.Interfaces;
 using Tsi1.BusinessLayer.Services;
+using Tsi1.BusinessLayer.DataLoad;
 using Tsi1.DataLayer;
 using Tsi1.DataLayer.MongoDbConfiguration;
 
@@ -79,6 +80,7 @@ namespace Tsi1.Api
             services.AddScoped<ISectionService, SectionService>();
             services.AddScoped<ISectionItemService, SectionItemService>();
             services.AddScoped<ISectionItemTypeService, SectionItemTypeService>();
+            services.AddScoped<IDataLoad, DataLoad>();
 
             services.Configure<MailSettings>(Configuration.GetSection("MailSettings"));
             services.AddScoped<IEmailService, EmailService>();
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs
new file mode 100644
index 0000000..ea09ddf
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs
@@ -0,0 +1,263 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading.Tasks;
+using Tsi1.BusinessLayer.Dtos;
+using Tsi1.BusinessLayer.Helpers;
+using Tsi1.BusinessLayer.Interfaces;
+using Tsi1.DataLayer;
+using Tsi1.DataLayer.Entities;
+
+namespace Tsi1.BusinessLayer.DataLoad
+{
+    public class DataLoad : IDataLoad
+    {
+        private readonly Tsi1Context _context;
+        private readonly IUserTypeService _userTypeService;
+        private readonly ITenantService _tenantService;
+        private readonly IUserService _userService;
+        private readonly ICourseService _courseService;
+        private readonly ISectionItemTypeService _sectionItemTypeService;
+        private readonly ISectionService _sectionService;
+        private readonly ISectionItemService _sectionItemService;
+
+        public DataLoad(Tsi1Context context,
+            IUserTypeService userTypeService,
+            ITenantService tenantService,
+            IUserService userService,
+            ICourseService courseService,
+            ISectionItemTypeService sectionItemTypeService,
+            ISectionService sectionService,
+            ISectionItemService sectionItemService)
+        {
+            _context = context;
+            _userTypeService = userTypeService;
+            _tenantService = tenantService;
+            _userService = userService;
+            _courseService = courseService;
+            _sectionItemTypeService = sectionItemTypeService;
+            _sectionService = sectionService;
+            _sectionItemService = sectionItemService;
+        }
+
+        public async Task LoadDataAsync()
+        {
+            // TENANTS
+            var adminTenant = new TenantCreateDto
+            {
+                Name = TenantAdmin.Name,
+            };
+            var adminTenantId = await _tenantService.Create(adminTenant);
+            
+            var fingTenant = new TenantCreateDto
+            {
+                Name = "fing",
+            };
+            var fingTenantId = await _tenantService.Create(fingTenant);
+
+            // USER TYPES
+            var udelarAdminUserType = new UserType
+            {
+                Name = UserTypes.UdelarAdmin,
+            };
+            _context.Add(udelarAdminUserType);
+
+            var facultyAdminUserType = new UserType
+            {
+                Name = UserTypes.FacultyAdmin,
+            };
+            _context.Add(facultyAdminUserType);
+
+            var professorUserType = new UserType
+            {
+                Name = UserTypes.Professor,
+            };
+            _context.Add(professorUserType);
+
+            var studentUserType = new UserType
+            {
+                Name = UserTypes.Student,
+            };
+            _context.Add(studentUserType);
+
+            await _context.SaveChangesAsync();
+
+            // USERS
+            var userAdminUdelar = new UserRegisterDto
+            {
+                UserTypeId = udelarAdminUserType.Id,
+                Username = "admin",
+                Password = "admin",
+                FirstName = "admin",
+                LastName = "admin",
+                Email = "admin@mail.com",
+                IdentityCard = "12345678-9",
+                Age = 44
+            };
+            await _userService.Create(userAdminUdelar, udelarAdminUserType.Name, adminTenantId.Data.Id);
+
+            var userStudent1 = new UserRegisterDto
+            {
+                UserTypeId = studentUserType.Id,
+                Username = "enzo",
+                Password = "enzo",
+                FirstName = "Enzo",
+                LastName = "Santangelo",
+                Email = "enzo@mail.com",
+                IdentityCard = "13242344-5",
+                Age = 25
+            };
+            await _userService.Create(userStudent1, studentUserType.Name, fingTenantId.Data.Id);
+
+            var userStudent2 = new UserRegisterDto
+            {
+                UserTypeId = studentUserType.Id,
+                Username = "mathias",
+                Password = "mathias",
+                FirstName = "Mathias",
+                LastName = "Martinez",
+                Email = "mathias@mail.com",
+                IdentityCard = "3782346-5",
+                Age = 26
+            };
+            await _userService.Create(userStudent2, studentUserType.Name, fingTenantId.Data.Id);
+
+            var userProfessor = new UserRegisterDto
+            {
+                UserTypeId = professorUserType.Id,
+                Username = "prof1",
+                Password = "prof1",
+                FirstName = "Juan",
+                LastName = "Perez",
+                Email = "jp@mail.com",
+                IdentityCard = "98754342-5",
+                Age = 34
+            };
+            await _userService.Create(userProfessor, professorUserType.Name, fingTenantId.Data.Id);
+
+            var userFingAdmin = new UserRegisterDto
+            {
+                UserTypeId = facultyAdminUserType.Id,
+                Username = "fing",
+                Password = "fing",
+                FirstName = "fing",
+                LastName = "fing",
+                Email = "fing@mail.com",
+                IdentityCard = "89547821-5",
+                Age = 45
+            };
+            await _userService.Create(userFingAdmin, facultyAdminUserType.Name, fingTenantId.Data.Id);
+
+            // SECTION ITEM TYPES
+            var sectionItemTypeFile = new SectionItemType
+            {
+                Name = SectionItemTypes.File,
+            };
+            _context.SectionItemTypes.Add(sectionItemTypeFile);
+
+            var sectionItemTypeForum = new SectionItemType
+            {
+                Name = SectionItemTypes.Forum,
+            };
+            _context.SectionItemTypes.Add(sectionItemTypeForum);
+
+            await _context.SaveChangesAsync();
+
+            // COURSES  
+            var course1Dto = new CourseCreateDto
+            {
+                Name = "Calculo 1",
+                TenantId = fingTenantId.Data.Id
+            };
+            var course1 = await _courseService.Create(course1Dto);
+
+            var course2Dto = new CourseCreateDto
+            {
+                Name = "GAL 1",
+                TenantId = fingTenantId.Data.Id
+            };
+            var course2 = await _courseService.Create(course2Dto);
+
+            var course3Dto = new CourseCreateDto
+            {
+                Name = "Fisica 1",
+                TenantId = fingTenantId.Data.Id
+            };
+            var course3 = await _courseService.Create(course3Dto);
+
+            // SECTIONS
+            var section1 = new SectionCreateDto
+            {
+                CourseId = course1.Data.Id,
+                Name = "General",
+                Order = 1
+            };
+            var section1Id = await _sectionService.Create(section1);
+
+            var section2 = new SectionCreateDto
+            {
+                CourseId = course2.Data.Id,
+                Name = "General",
+                Order = 1
+            };
+            var section2Id = await _sectionService.Create(section2);
+
+            var section3 = new SectionCreateDto
+            {
+                CourseId = course1.Data.Id,
+                Name = "Tema 1",
+                Order = 2
+            };
+            var section3Id = await _sectionService.Create(section3);
+
+            // SECTION ITEMS
+            var sectionItem1 = new SectionItemCreateDto
+            {
+                SectionId = section1Id.Data,
+                Order = 1,
+                SectionItemTypeId = sectionItemTypeForum.Id,
+                Forum = new ForumCreateDto
+                {
+                    Name = "Novedades"
+                }
+            };
+            await _sectionItemService.Create(sectionItem1);
+
+            var sectionItem2 = new SectionItemCreateDto
+            {
+                SectionId = section2Id.Data,
+                Order = 1,
+                SectionItemTypeId = sectionItemTypeForum.Id,
+                Forum = new ForumCreateDto
+                {
+                    Name = "Novedades"
+                }
+            };
+            await _sectionItemService.Create(sectionItem2);
+
+            var sectionItem3 = new SectionItemCreateDto
+            {
+                SectionId = section1Id.Data,
+                Order = 2,
+                SectionItemTypeId = sectionItemTypeForum.Id,
+                Forum = new ForumCreateDto
+                {
+                    Name = "General"
+                }
+            };
+            await _sectionItemService.Create(sectionItem3);
+
+            var sectionItem4 = new SectionItemCreateDto
+            {
+                SectionId = section3Id.Data,
+                Order = 1,
+                SectionItemTypeId = sectionItemTypeForum.Id,
+                Forum = new ForumCreateDto
+                {
+                    Name = "Tema 1"
+                }
+            };
+            await _sectionItemService.Create(sectionItem4);
+        }
+    }
+}
\ No newline at end of file
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs
new file mode 100644
index 0000000..613fbe6
--- /dev/null
+++ b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tsi1.BusinessLayer.DataLoad
+{
+    public interface IDataLoad
+    {
+        Task LoadDataAsync();
+    }
+}
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionService.cs
index 4ba20e7..c73668e 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionService.cs
@@ -9,7 +9,7 @@ namespace Tsi1.BusinessLayer.Interfaces
 {
     public interface ISectionService
     {
-        Task<ServiceResult<bool>> Create(SectionCreateDto newSection);
+        Task<ServiceResult<int>> Create(SectionCreateDto newSection);
         Task<ServiceResult<bool>> Delete(int sectionId);
         Task<ServiceResult<bool>> OrderSections(List<OrderDto> orderDtos);
         Task<ServiceResult<bool>> Modify(int sectionId, string name);
diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionService.cs
index 9da29bb..765fce4 100644
--- a/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionService.cs
+++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionService.cs
@@ -24,9 +24,9 @@ namespace Tsi1.BusinessLayer.Services
             _mapper = mapper;
         }
 
-        public async Task<ServiceResult<bool>> Create(SectionCreateDto newSection)
+        public async Task<ServiceResult<int>> Create(SectionCreateDto newSection)
         {
-            var result = new ServiceResult<bool>();
+            var result = new ServiceResult<int>();
 
             var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == newSection.CourseId);
             if (course == null)
@@ -40,6 +40,7 @@ namespace Tsi1.BusinessLayer.Services
             _context.Sections.Add(section);
             await _context.SaveChangesAsync();
 
+            result.Data = section.Id;
             return result;
         }
 
-- 
GitLab