From 4034de6111d350de75c7076f96591b360fe6cbd0 Mon Sep 17 00:00:00 2001 From: Lucca Santangelo <luccasant95@gmail.com> Date: Sat, 14 Nov 2020 18:55:02 -0300 Subject: [PATCH] refactor --- .../Tsi1.Api/Controllers/CourseController.cs | 23 +- .../Controllers/SectionItemController.cs | 2 +- .../Tsi1.BusinessLayer/DataLoad/DataLoad.cs | 446 +++++++++--------- .../Dtos/CourseCreateDto.cs | 1 + .../Dtos/SectionCreateDto.cs | 2 + .../Helpers/MappingProfile.cs | 8 - .../Interfaces/ICourseService.cs | 3 +- .../Interfaces/ISectionItemService.cs | 3 +- .../Interfaces/ISectionService.cs | 1 + .../Services/CourseService.cs | 46 +- .../Services/SectionItemService.cs | 127 +++-- .../Services/SectionService.cs | 51 +- 12 files changed, 367 insertions(+), 346 deletions(-) diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 70af980..9097c57 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; +using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Tsi1.Api.Infrastructure; using Tsi1.BusinessLayer.Dtos; using Tsi1.BusinessLayer.Helpers; using Tsi1.BusinessLayer.Interfaces; @@ -237,7 +233,7 @@ namespace Tsi1.Api.Controllers return Ok(result.Data); } - [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor + ", " + UserTypes.FacultyAdmin)] [HttpGet("GetById/{courseId}")] public async Task<IActionResult> GetById(int courseId) { @@ -249,20 +245,5 @@ namespace Tsi1.Api.Controllers return Ok(result.Data); } - - [AllowAnonymous] - [Authorize(Roles = UserTypes.FacultyAdmin)] - [HttpPost("CreateFromTemplate")] - public async Task<IActionResult> CreateFromTemplate(CourseTemplateCreateDto courseTemplate) - { - var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); - - var result = await _courseService.CreateFromTemplate(courseTemplate, tenantId); - if (result.HasError) - { - return BadRequest(result.Message); - } - return Ok(); - } } } diff --git a/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs b/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs index 1a1a7c5..04e043b 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs @@ -28,7 +28,7 @@ namespace Tsi1.Api.Controllers { var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); - var result = await _sectionItemService.Create(sectionItem, tenantId); + var result = await _sectionItemService.Create(sectionItem); if (result.HasError) { return BadRequest(result.Message); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs index db3a8af..9f320e2 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs @@ -36,256 +36,256 @@ namespace Tsi1.BusinessLayer.DataLoad public async Task LoadDataAsync() { - // TENANTS - var adminTenant = new TenantCreateDto - { - Name = TenantAdmin.Name, - }; - var adminTenantId = await _tenantService.Create(adminTenant); + //// 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); + //var fingTenant = new TenantCreateDto + //{ + // Name = "fing", + //}; + //var fingTenantId = await _tenantService.Create(fingTenant); - // USER TYPES - var udelarAdminUserType = new UserType - { - Name = UserTypes.UdelarAdmin, - }; - _context.Add(udelarAdminUserType); + //// USER TYPES + //var udelarAdminUserType = new UserType + //{ + // Name = UserTypes.UdelarAdmin, + //}; + //_context.Add(udelarAdminUserType); - var facultyAdminUserType = new UserType - { - Name = UserTypes.FacultyAdmin, - }; - _context.Add(facultyAdminUserType); + //var facultyAdminUserType = new UserType + //{ + // Name = UserTypes.FacultyAdmin, + //}; + //_context.Add(facultyAdminUserType); - var professorUserType = new UserType - { - Name = UserTypes.Professor, - }; - _context.Add(professorUserType); + //var professorUserType = new UserType + //{ + // Name = UserTypes.Professor, + //}; + //_context.Add(professorUserType); - var studentUserType = new UserType - { - Name = UserTypes.Student, - }; - _context.Add(studentUserType); + //var studentUserType = new UserType + //{ + // Name = UserTypes.Student, + //}; + //_context.Add(studentUserType); - await _context.SaveChangesAsync(); + //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); + //// 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 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 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 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); + //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); + //// 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); + //var sectionItemTypeForum = new SectionItemType + //{ + // Name = SectionItemTypes.Forum, + //}; + //_context.SectionItemTypes.Add(sectionItemTypeForum); - var sectionItemTypeSurvey = new SectionItemType - { - Name = SectionItemTypes.Survey, - }; - _context.SectionItemTypes.Add(sectionItemTypeSurvey); + //var sectionItemTypeSurvey = new SectionItemType + //{ + // Name = SectionItemTypes.Survey, + //}; + //_context.SectionItemTypes.Add(sectionItemTypeSurvey); - await _context.SaveChangesAsync(); + //await _context.SaveChangesAsync(); - // COURSES - var course1Dto = new CourseCreateDto - { - Name = "Calculo 1", - TenantId = fingTenantId.Data.Id - }; - var course1 = await _courseService.Create(course1Dto); + //// 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 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 - }; - await _courseService.Create(course3Dto); + //var course3Dto = new CourseCreateDto + //{ + // Name = "Fisica 1", + // TenantId = fingTenantId.Data.Id + //}; + //await _courseService.Create(course3Dto); - // SECTIONS - var section1 = new SectionCreateDto - { - CourseId = course1.Data.Id, - Name = "General", - Order = 1 - }; - var section1Id = await _sectionService.Create(section1); + //// 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 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); + //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, fingTenantId.Data.Id); + //// 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, fingTenantId.Data.Id); + //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, fingTenantId.Data.Id); + //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, fingTenantId.Data.Id); + //var sectionItem4 = new SectionItemCreateDto + //{ + // SectionId = section3Id.Data, + // Order = 1, + // SectionItemTypeId = sectionItemTypeForum.Id, + // Forum = new ForumCreateDto + // { + // Name = "Tema 1" + // } + //}; + //await _sectionItemService.Create(sectionItem4); - // ANSWER OPTIONS - var answerOptions = new List<AnswerOption> - { - new AnswerOption - { - Name = "Excelente" - }, - new AnswerOption - { - Name = "Conforme" - }, - new AnswerOption - { - Name = "Neutro" - }, - new AnswerOption - { - Name = "No Conforme" - }, - new AnswerOption - { - Name = "Mal" - }, - }; + //// ANSWER OPTIONS + //var answerOptions = new List<AnswerOption> + //{ + // new AnswerOption + // { + // Name = "Excelente" + // }, + // new AnswerOption + // { + // Name = "Conforme" + // }, + // new AnswerOption + // { + // Name = "Neutro" + // }, + // new AnswerOption + // { + // Name = "No Conforme" + // }, + // new AnswerOption + // { + // Name = "Mal" + // }, + //}; - _context.AnswerOptions.AddRange(answerOptions); - await _context.SaveChangesAsync(); + //_context.AnswerOptions.AddRange(answerOptions); + //await _context.SaveChangesAsync(); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs index a3540f4..97aae1c 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs @@ -9,6 +9,7 @@ namespace Tsi1.BusinessLayer.Dtos { public string Name { get; set; } public bool IsTemplate { get; set; } + public List<SectionCreateDto> Sections { get; set; } [JsonIgnore] public int TenantId { get; set; } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionCreateDto.cs index 3eee0e0..0b1df64 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionCreateDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionCreateDto.cs @@ -9,5 +9,7 @@ namespace Tsi1.BusinessLayer.Dtos public int CourseId { get; set; } public string Name { get; set; } public int Order { get; set; } + + public List<SectionItemCreateDto> SectionItems { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs index 00f542a..d7f378c 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs @@ -87,14 +87,6 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<SurveyResponseDetailDto, SurveyResponse>(); CreateMap<SurveyAnswerDetailDto, SurveyAnswer>(); CreateMap<SurveyAnswerCreateDto, SurveyAnswer>(); - - - CreateMap<Course, Course>().ForMember(x => x.Id, opt => opt.Ignore()); - CreateMap<Tenant, Tenant>().ForMember(x => x.Id, opt => opt.Ignore()); - CreateMap<Section, Section>().ForMember(x => x.Id, opt => opt.Ignore()); - CreateMap<SectionItem, SectionItem>().ForMember(x => x.Id, opt => opt.Ignore()); - CreateMap<SectionItemType, SectionItemType>().ForMember(x => x.Id, opt => opt.Ignore()); - CreateMap<Forum, Forum>().ForMember(x => x.Id, opt => opt.Ignore()); } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs index 640e6ec..9bd530c 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs @@ -10,8 +10,7 @@ namespace Tsi1.BusinessLayer.Interfaces { Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType); - Task<ServiceResult<Course>> Create(CourseCreateDto newCourse); - Task<ServiceResult<bool>> CreateFromTemplate(CourseTemplateCreateDto courseTemplate, int tenantId); + Task<ServiceResult<int>> Create(CourseCreateDto newCourse); Task<ServiceResult<bool>> Matriculate(int userId, int courseId); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs index ad04881..2c8a8d4 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs @@ -9,8 +9,9 @@ namespace Tsi1.BusinessLayer.Interfaces { public interface ISectionItemService { - Task<ServiceResult<bool>> Create(SectionItemCreateDto newSectionItem, int tenantId); + Task<ServiceResult<int>> Create(SectionItemCreateDto newSectionItem); Task<ServiceResult<bool>> Delete(int sectionItemId); Task<ServiceResult<bool>> OrderSectionItems(List<OrderDto> orderDtos); + Task<ServiceResult<int>> CreateValidations(List<SectionItemCreateDto> sectionItems, bool isFromCreateSection = false); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionService.cs index c73668e..6fc24d9 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionService.cs @@ -13,5 +13,6 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<bool>> Delete(int sectionId); Task<ServiceResult<bool>> OrderSections(List<OrderDto> orderDtos); Task<ServiceResult<bool>> Modify(int sectionId, string name); + Task<ServiceResult<int>> CreateValidations(List<SectionCreateDto> sections, bool isFromCreateCourse = false); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index 64cc381..cd9387a 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -17,18 +17,19 @@ namespace Tsi1.BusinessLayer.Services public class CourseService : ICourseService { private readonly Tsi1Context _context; - private readonly IMapper _mapper; + private readonly ISectionService _sectionService; - public CourseService(Tsi1Context context, IMapper mapper) + public CourseService(Tsi1Context context, IMapper mapper, ISectionService sectionService) { _context = context; _mapper = mapper; + _sectionService = sectionService; } - public async Task<ServiceResult<Course>> Create(CourseCreateDto newCourse) + public async Task<ServiceResult<int>> Create(CourseCreateDto newCourse) { - var result = new ServiceResult<Course>(); + var result = new ServiceResult<int>(); var existingCourse = await _context.Courses .FirstOrDefaultAsync(x => x.Name == newCourse.Name && x.TenantId == newCourse.TenantId); @@ -40,13 +41,17 @@ namespace Tsi1.BusinessLayer.Services return result; } + result = await _sectionService.CreateValidations(newCourse.Sections, true); + if (result.HasError) + { + return result; + } + var course = _mapper.Map<Course>(newCourse); _context.Courses.Add(course); - await _context.SaveChangesAsync(); - result.Data = course; return result; } @@ -378,34 +383,5 @@ namespace Tsi1.BusinessLayer.Services return result; } - - public async Task<ServiceResult<bool>> CreateFromTemplate(CourseTemplateCreateDto courseTemplate, int tenantId) - { - var result = new ServiceResult<bool>(); - - var course = await _context.Courses.AsNoTracking() - .Include(x => x.Sections) - .ThenInclude(x => x.SectionItems) - .ThenInclude(x => x.Forum) - .FirstOrDefaultAsync(x => - x.TenantId == tenantId - && x.IsTemplate - && x.Id == courseTemplate.CourseTemplateId); - - if (course == null) - { - result.HasError = true; - result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, courseTemplate.CourseTemplateId)); - return result; - } - - var newCourse = _mapper.Map<Course>(course); - newCourse.Name = courseTemplate.Name; - - _context.Add(newCourse); - await _context.SaveChangesAsync(); - - return result; - } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs index 70142c4..56cab25 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs @@ -27,25 +27,31 @@ namespace Tsi1.BusinessLayer.Services _fileService = fileService; } - public async Task<ServiceResult<bool>> Create(SectionItemCreateDto newSectionItem, int tenantId) + public async Task<ServiceResult<int>> Create(SectionItemCreateDto newSectionItem) { - var sectionItem = _mapper.Map<SectionItem>(newSectionItem); + var result = await this.CreateValidations(new List<SectionItemCreateDto>() { newSectionItem }); + if (result.HasError) + { + return result; + } - var sectionItemType = await _context.SectionItemTypes - .FirstOrDefaultAsync(x => x.Id == sectionItem.SectionItemTypeId); + var sectionItem = _mapper.Map<SectionItem>(newSectionItem); + _context.SectionItems.Add(sectionItem); + await _context.SaveChangesAsync(); - var result = this.SectionItemValidations(sectionItem, sectionItemType); + return result; + } + public async Task<ServiceResult<int>> Create(List<SectionItemCreateDto> newSectionItem) + { + var result = await this.CreateValidations(newSectionItem); if (result.HasError) { return result; } - if (sectionItemType.Name == SectionItemTypes.Survey) - { - sectionItem.Survey.Tenant.Id = tenantId; - } - _context.SectionItems.Add(sectionItem); + var sectionItem = _mapper.Map<List<SectionItem>>(newSectionItem); + _context.SectionItems.AddRange(sectionItem); await _context.SaveChangesAsync(); return result; @@ -107,66 +113,91 @@ namespace Tsi1.BusinessLayer.Services return result; } - private ServiceResult<bool> SectionItemValidations(SectionItem sectionItem, SectionItemType sectionItemType) + public async Task<ServiceResult<int>> CreateValidations(List<SectionItemCreateDto> sectionItems, bool isFromCreateSection = false) { - var result = new ServiceResult<bool>(); - - if (sectionItemType == null) - { - result.HasError = true; - result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemType, sectionItem.SectionItemTypeId)); - return result; - } + var result = new ServiceResult<int>(); - if (sectionItemType.Name == SectionItemTypes.Forum) + if (!isFromCreateSection) { - if (sectionItem.Forum == null) - { - result.HasError = true; - result.AddMessage(ErrorMessages.ForumIsNull); - } + var sectionId = sectionItems.Select(x => x.SectionId).SingleOrDefault(); + var section = await _context.SectionItems.AsNoTracking() + .FirstOrDefaultAsync(x => sectionId == x.Id); - if (sectionItem.File != null || sectionItem.Survey != null) + if (section == null) { result.HasError = true; - result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemData, sectionItemType.Name, SectionItemTypes.File)); + result.AddMessage(string.Format(ErrorMessages.SectionDoesNotExist, sectionId)); + return result; } } - if (sectionItemType.Name == SectionItemTypes.File) + var sectionItemTypeIds = sectionItems.Select(x => x.SectionItemTypeId).Distinct(); + + var sectionItemTypes = await _context.SectionItemTypes.AsNoTracking() + .Where(x => sectionItemTypeIds.Contains(x.Id)) + .ToListAsync(); + + foreach (var sectionItem in sectionItems) { - if (sectionItem.File == null) - { - result.HasError = true; - result.AddMessage(ErrorMessages.FileIsNull); - } + var sectionItemType = sectionItemTypes.FirstOrDefault(x => x.Id == sectionItem.SectionItemTypeId); - if (sectionItem.Forum != null || sectionItem.Survey != null) + if (sectionItemType == null) { result.HasError = true; - result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemData, sectionItemType.Name, SectionItemTypes.Forum)); + result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemType, sectionItem.SectionItemTypeId)); + continue; } - if (!_fileService.ExistFile(sectionItem.File.Path)) + if (sectionItemType.Name == SectionItemTypes.Forum) { - result.HasError = true; - result.AddMessage(string.Format(ErrorMessages.FileDoesNotExist, sectionItem.File.Path)); + if (sectionItem.Forum == null) + { + result.HasError = true; + result.AddMessage(ErrorMessages.ForumIsNull); + } + + if (sectionItem.File != null || sectionItem.Survey != null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemData, sectionItemType.Name, SectionItemTypes.File)); + } } - } - if (sectionItemType.Name == SectionItemTypes.Survey) - { - if (sectionItem.Survey == null) + if (sectionItemType.Name == SectionItemTypes.File) { - result.HasError = true; - result.AddMessage(ErrorMessages.SurveyIsNull); + if (sectionItem.File == null) + { + result.HasError = true; + result.AddMessage(ErrorMessages.FileIsNull); + } + + if (sectionItem.Forum != null || sectionItem.Survey != null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemData, sectionItemType.Name, SectionItemTypes.Forum)); + } + + if (!_fileService.ExistFile(sectionItem.File.Path)) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.FileDoesNotExist, sectionItem.File.Path)); + } } - if (sectionItem.File != null || sectionItem.Forum != null) + if (sectionItemType.Name == SectionItemTypes.Survey) { - result.HasError = true; - result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemData, sectionItemType.Name, SectionItemTypes.File)); - } + if (sectionItem.Survey == null) + { + result.HasError = true; + result.AddMessage(ErrorMessages.SurveyIsNull); + } + + if (sectionItem.File != null || sectionItem.Forum != null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemData, sectionItemType.Name, SectionItemTypes.File)); + } + } } return result; diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionService.cs index 765fce4..97e47a4 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionService.cs @@ -17,22 +17,20 @@ namespace Tsi1.BusinessLayer.Services { private readonly Tsi1Context _context; private readonly IMapper _mapper; + private readonly ISectionItemService _sectionItemService; - public SectionService(Tsi1Context context, IMapper mapper) + public SectionService(Tsi1Context context, IMapper mapper, ISectionItemService sectionItemService) { _context = context; _mapper = mapper; + _sectionItemService = sectionItemService; } public async Task<ServiceResult<int>> Create(SectionCreateDto newSection) { - var result = new ServiceResult<int>(); - - var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == newSection.CourseId); - if (course == null) + var result = await this.CreateValidations(new List<SectionCreateDto>() { newSection }); + if (result.HasError) { - result.HasError = true; - result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, newSection.CourseId)); return result; } @@ -44,6 +42,21 @@ namespace Tsi1.BusinessLayer.Services return result; } + public async Task<ServiceResult<int>> Create(List<SectionCreateDto> newSections) + { + var result = await this.CreateValidations(newSections); + if (result.HasError) + { + return result; + } + + var sections = _mapper.Map<List<Section>>(newSections); + _context.Sections.AddRange(sections); + await _context.SaveChangesAsync(); + + return result; + } + public async Task<ServiceResult<bool>> Delete(int sectionId) { var result = new ServiceResult<bool>(); @@ -115,5 +128,29 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<int>> CreateValidations(List<SectionCreateDto> sections, bool isFromCreateCourse = false) + { + var result = new ServiceResult<int>(); + + if (!isFromCreateCourse) + { + var courseId = sections.Select(x => x.CourseId).SingleOrDefault(); + var course = await _context.Courses.AsNoTracking() + .SingleOrDefaultAsync(x => x.Id == courseId); + + if (course == null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, courseId)); + return result; + } + } + + var sectionItems = sections.SelectMany(x => x.SectionItems).ToList(); + result = await _sectionItemService.CreateValidations(sectionItems, true); + + return result; + } } } -- GitLab