From 178b7c6916bc11cb75e015aa9c3752060e02f605 Mon Sep 17 00:00:00 2001 From: esantangelo <enzo020895@gmail.com> Date: Tue, 10 Nov 2020 20:02:17 -0300 Subject: [PATCH] add survey to section item --- .../Tsi1.Api/Controllers/CourseController.cs | 5 +-- .../Controllers/SectionItemController.cs | 5 ++- .../Tsi1.BusinessLayer/DataLoad/DataLoad.cs | 22 +++++------ .../Dtos/SectionItemCreateDto.cs | 2 + .../Dtos/SurveyCreateDto.cs | 2 +- .../Helpers/ErrorMessages.cs | 2 + .../Helpers/SectionItemTypes.cs | 1 + .../Interfaces/ISectionItemService.cs | 2 +- .../Services/CourseService.cs | 3 ++ .../Services/EmailService.cs | 8 ++-- .../Services/SectionItemService.cs | 37 ++++++++++++++----- .../Services/SurveyService.cs | 7 ++++ 12 files changed, 66 insertions(+), 30 deletions(-) diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 4a8d4b8..5469334 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 4e278ca..1a1a7c5 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 531c3dd..db3a8af 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 9da60f9..6ddb3a5 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 9ef1eef..7eb3692 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 0f99100..37f629e 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 0eee4f0..a889446 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 4834f03..ad04881 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 fffcc93..326206b 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 9519fc5..6be1396 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 ae0eedc..b632f12 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 b442eb8..e893369 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(); -- GitLab