diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 4a8d4b838b69c414438d9802c0ded5433fa78b0a..5469334a34225ac449c54dc52e9e4cfe9f9ca849 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -21,12 +21,9 @@ namespace Tsi1.Api.Controllers { private readonly ICourseService _courseService; - private readonly IFileService _fileService; - - public CourseController(ICourseService courseService, IFileService fileService) + public CourseController(ICourseService courseService) { _courseService = courseService; - _fileService = fileService; } [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] diff --git a/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs b/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs index 4e278ca49c8c101e03e2c593a2cc5a895c3196d1..1a1a7c5e582cf3d7caf234e21ccf243de7bb0c8b 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -25,7 +26,9 @@ namespace Tsi1.Api.Controllers [HttpPost("Create")] public async Task<IActionResult> Create(SectionItemCreateDto sectionItem) { - var result = await _sectionItemService.Create(sectionItem); + var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); + + var result = await _sectionItemService.Create(sectionItem, tenantId); 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 531c3ddb99ebef6b6e5483525ea8ef9a7855db45..db3a8af45db895fce706b8f731064675f59f73d5 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs @@ -13,29 +13,23 @@ 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; } @@ -161,6 +155,12 @@ namespace Tsi1.BusinessLayer.DataLoad }; _context.SectionItemTypes.Add(sectionItemTypeForum); + var sectionItemTypeSurvey = new SectionItemType + { + Name = SectionItemTypes.Survey, + }; + _context.SectionItemTypes.Add(sectionItemTypeSurvey); + await _context.SaveChangesAsync(); // COURSES @@ -183,7 +183,7 @@ namespace Tsi1.BusinessLayer.DataLoad Name = "Fisica 1", TenantId = fingTenantId.Data.Id }; - var course3 = await _courseService.Create(course3Dto); + await _courseService.Create(course3Dto); // SECTIONS var section1 = new SectionCreateDto @@ -221,7 +221,7 @@ namespace Tsi1.BusinessLayer.DataLoad Name = "Novedades" } }; - await _sectionItemService.Create(sectionItem1); + await _sectionItemService.Create(sectionItem1, fingTenantId.Data.Id); var sectionItem2 = new SectionItemCreateDto { @@ -233,7 +233,7 @@ namespace Tsi1.BusinessLayer.DataLoad Name = "Novedades" } }; - await _sectionItemService.Create(sectionItem2); + await _sectionItemService.Create(sectionItem2, fingTenantId.Data.Id); var sectionItem3 = new SectionItemCreateDto { @@ -245,7 +245,7 @@ namespace Tsi1.BusinessLayer.DataLoad Name = "General" } }; - await _sectionItemService.Create(sectionItem3); + await _sectionItemService.Create(sectionItem3, fingTenantId.Data.Id); var sectionItem4 = new SectionItemCreateDto { @@ -257,7 +257,7 @@ namespace Tsi1.BusinessLayer.DataLoad Name = "Tema 1" } }; - await _sectionItemService.Create(sectionItem4); + await _sectionItemService.Create(sectionItem4, fingTenantId.Data.Id); // ANSWER OPTIONS var answerOptions = new List<AnswerOption> diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemCreateDto.cs index 9da60f96c22ec4674298ff8503a8d29b0b70cfb8..6ddb3a53587dc01c31fbe715054739d69f4cc871 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemCreateDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemCreateDto.cs @@ -12,5 +12,7 @@ namespace Tsi1.BusinessLayer.Dtos public ForumCreateDto Forum { get; set; } public FileDto File { get; set; } + + public SurveyCreateDto Survey { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SurveyCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SurveyCreateDto.cs index 9ef1eef2348f743caa5fe4a70168ff38c28f0881..7eb3692f0ed30ef400fd70a1799e500c039954bd 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SurveyCreateDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SurveyCreateDto.cs @@ -15,6 +15,6 @@ namespace Tsi1.BusinessLayer.Dtos [JsonIgnore] public int TenantId { get; set; } - public ICollection<SurveyQuestionCreateDto> SurveyQuestions { get; set; } + public List<SurveyQuestionCreateDto> SurveyQuestions { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs index 0f99100c3ee37acf131bedaab038d08f55ab333f..37f629edce74953e6daffecc5ae5acd4cd78f52d 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs @@ -56,5 +56,7 @@ namespace Tsi1.BusinessLayer.Helpers public const string InvalidSectionItemData = "El tipo de item de seccion '{0}' no coincide con su contenido '{1}'"; public const string SurveyDoesNotExist = "La encuesta con id '{0}' no existe"; + public const string SurveyIsNull = "La encuesta es nula"; + public const string SurveyHasNotQuestions = "La encuesta no tiene preguntas"; } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/SectionItemTypes.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/SectionItemTypes.cs index 0eee4f0c3bd2dd3c53204e5539652b839534a794..a889446941100b1eb36fb20075cd6b8228dd614c 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/SectionItemTypes.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/SectionItemTypes.cs @@ -8,5 +8,6 @@ namespace Tsi1.BusinessLayer.Helpers { public const string Forum = nameof(Forum); public const string File = nameof(File); + public const string Survey = nameof(Survey); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs index 4834f03114b891bfae163d7d87ac1be83dfced28..ad048812770a41c268427b9a0d0c1aa1a8ca7812 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs @@ -9,7 +9,7 @@ namespace Tsi1.BusinessLayer.Interfaces { public interface ISectionItemService { - Task<ServiceResult<bool>> Create(SectionItemCreateDto newSectionItem); + Task<ServiceResult<bool>> Create(SectionItemCreateDto newSectionItem, int tenantId); Task<ServiceResult<bool>> Delete(int sectionItemId); Task<ServiceResult<bool>> OrderSectionItems(List<OrderDto> orderDtos); } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index fffcc93913bd4653c1fc9228a6d362b6e2acd8f8..326206b791ac86d65b5575c47373ff84fb7aab6e 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -353,6 +353,9 @@ namespace Tsi1.BusinessLayer.Services .Include(x => x.Sections) .ThenInclude(x => x.SectionItems) .ThenInclude(x => x.File) + .Include(x => x.Sections) + .ThenInclude(x => x.SectionItems) + .ThenInclude(x => x.Survey) .FirstOrDefaultAsync(x => x.Id == courseId); if (course == null) diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs index 9519fc5b0cf3037535e101a866a297392b786825..6be1396f8a82712544d99e643c9550d44407dfcd 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs @@ -29,14 +29,16 @@ namespace Tsi1.BusinessLayer.Services message.Sender = MailboxAddress.Parse(_mailSettings.Mail); - var client = new SmtpClient(); - client.CheckCertificateRevocation = false; + var client = new SmtpClient + { + CheckCertificateRevocation = false + }; try { await client.ConnectAsync(_mailSettings.Host, _mailSettings.Port, SecureSocketOptions.StartTls); } - catch (Exception e) + catch (Exception) { result.HasError = true; result.Message = ErrorMessages.CannotConnectToSmtpServer; diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs index ae0eedca11130f202ca5cc31688ea5725eea6525..b632f12b796cafad645cf6fbfbf56216e20d299d 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs @@ -27,17 +27,24 @@ namespace Tsi1.BusinessLayer.Services _fileService = fileService; } - public async Task<ServiceResult<bool>> Create(SectionItemCreateDto newSectionItem) + public async Task<ServiceResult<bool>> Create(SectionItemCreateDto newSectionItem, int tenantId) { var sectionItem = _mapper.Map<SectionItem>(newSectionItem); - var result = await this.SectionItemValidations(sectionItem); + var sectionItemType = await _context.SectionItemTypes + .FirstOrDefaultAsync(x => x.Id == sectionItem.SectionItemTypeId); + + var result = this.SectionItemValidations(sectionItem, sectionItemType); if (result.HasError) { return result; } + if (sectionItemType.Name == SectionItemTypes.Survey) + { + sectionItem.Survey.TenantId = tenantId; + } _context.SectionItems.Add(sectionItem); await _context.SaveChangesAsync(); @@ -100,13 +107,10 @@ namespace Tsi1.BusinessLayer.Services return result; } - private async Task<ServiceResult<bool>> SectionItemValidations(SectionItem sectionItem) + private ServiceResult<bool> SectionItemValidations(SectionItem sectionItem, SectionItemType sectionItemType) { var result = new ServiceResult<bool>(); - - var sectionItemType = await _context.SectionItemTypes - .FirstOrDefaultAsync(x => x.Id == sectionItem.SectionItemTypeId); - + if (sectionItemType == null) { result.HasError = true; @@ -122,7 +126,7 @@ namespace Tsi1.BusinessLayer.Services result.AddMessage(ErrorMessages.ForumIsNull); } - if (sectionItem.File != null) + if (sectionItem.File != null || sectionItem.Survey != null) { result.HasError = true; result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemData, sectionItemType.Name, SectionItemTypes.File)); @@ -137,7 +141,7 @@ namespace Tsi1.BusinessLayer.Services result.AddMessage(ErrorMessages.FileIsNull); } - if (sectionItem.Forum != null) + if (sectionItem.Forum != null || sectionItem.Survey != null) { result.HasError = true; result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemData, sectionItemType.Name, SectionItemTypes.Forum)); @@ -150,6 +154,21 @@ namespace Tsi1.BusinessLayer.Services } } + if (sectionItemType.Name == SectionItemTypes.Survey) + { + 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/SurveyService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs index b442eb88a30ada87472ab07edc8002590222e3ba..e8933693d701449b6961f9209ef5d94352117a2d 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs @@ -39,6 +39,13 @@ namespace Tsi1.BusinessLayer.Services var survey = _mapper.Map<Survey>(newSurvey); + if (!survey.SurveyQuestions.Any()) + { + result.HasError = true; + result.AddMessage(ErrorMessages.SurveyHasNotQuestions); + return result; + } + _context.Surveys.Add(survey); await _context.SaveChangesAsync();