diff --git a/Tsi1.Api/Tsi1.Api/Controllers/SurveyController.cs b/Tsi1.Api/Tsi1.Api/Controllers/SurveyController.cs index 48ae7c43759b9c5743eb0cddd78675961e71c601..990851a82ad18d1753b8c68371fe7a3a2dc20041 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/SurveyController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/SurveyController.cs @@ -132,5 +132,14 @@ namespace Tsi1.Api.Controllers return Ok(result.Data); } + [Authorize(Roles = UserTypes.FacultyAdmin + ", " + UserTypes.UdelarAdmin + ", " + UserTypes.Professor + ", " + UserTypes.Student)] + [HttpGet("GetAnswerOptions")] + public async Task<IActionResult> GetAnswerOptions() + { + var result = await _surveyService.GetAnswerOptions(); + + return Ok(result.Data); + } + } } diff --git a/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs b/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs index 0c54af61a8bd5ac751eec8dd173a9f1140b4f5c8..575b05897d81008f78475837aee031ca50e3ae94 100644 --- a/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs +++ b/Tsi1.Api/Tsi1.Api/SignalR/ChatHub.cs @@ -66,6 +66,8 @@ namespace Tsi1.Api.SignalR var userId = int.Parse(Context.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); var tenantId = int.Parse(Context.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); + newMessage.TenantId = tenantId; + if (userId == newMessage.ReceiverId) throw new HubException("You cannot send messages to yourself"); @@ -93,41 +95,18 @@ namespace Tsi1.Api.SignalR await Clients.Group(groupName).SendAsync("NewMessage", messageDto); } - private async Task<Group> AddToGroup(string groupName) + private async Task<bool> AddToGroup(string groupName) { - var group = await _chatService.GetGroupByName(groupName); - - if (group == null) - { - throw new HubException($"No existe el grupo {groupName}"); - } - var userId = int.Parse(Context.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); - var connection = new Connection - { - ConnectionId = Context.ConnectionId, - UserId = userId, - }; - - if (group == null) - { - group = new Group - { - Name = groupName, - }; - } - - group.Connections.Add(connection); - - var result = await _chatService.CreateGroup(group); + var result = await _chatService.CreateGroup(groupName, Context.ConnectionId, userId); if (result.HasError) { - throw new HubException($"Error al crear el grupo {group}"); + throw new HubException($"Error al crear el grupo {groupName}"); } - return group; + return true; } private async Task<bool> RemoveFromGroup() diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs index d6ded58781ccb783a67987c34c64e5e00e7247b8..ac4bab571f338bc994f8abe9472af9c7dd3f33d6 100644 --- a/Tsi1.Api/Tsi1.Api/Startup.cs +++ b/Tsi1.Api/Tsi1.Api/Startup.cs @@ -93,6 +93,9 @@ namespace Tsi1.Api services.AddScoped<IDataLoad, DataLoad>(); services.AddScoped<ISurveyService, SurveyService>(); services.AddScoped<ICommunicationService, CommunicationService>(); + services.AddScoped<IChatService, ChatService>(); + + services.AddSingleton<PresenceTracker>(); 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 index 9f320e2a92c3c02d50fcce9a0249ba1f0acca4b0..812be89f9bcda5b145759ccc30f356a60387299b 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs @@ -36,256 +36,288 @@ namespace Tsi1.BusinessLayer.DataLoad 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); - - //var sectionItemTypeSurvey = new SectionItemType - //{ - // Name = SectionItemTypes.Survey, - //}; - //_context.SectionItemTypes.Add(sectionItemTypeSurvey); - - //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 - //}; - //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); - - //// 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(); + // TENANTS + var tenants = new List<Tenant> + { + new Tenant + { + Name = TenantAdmin.Name, + }, + new Tenant + { + Name = "fing", + }, + }; + _context.Tenants.AddRange(tenants); + + await _context.SaveChangesAsync(); + + // 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 users = new List<User> + { + new User + { + UserTypeId = udelarAdminUserType.Id, + Username = "admin", + Password = "admin", + FirstName = "admin", + LastName = "admin", + Email = "admin@mail.com", + TenantId = 1, + }, + + new User + { + UserTypeId = studentUserType.Id, + Username = "enzo", + Password = "enzo", + FirstName = "Enzo", + LastName = "Santangelo", + Email = "enzo@mail.com", + Student = new Student + { + TenantId = 2, + IdentityCard = "13242344-5", + Age = 25 + }, + TenantId = 2, + }, + + new User + { + UserTypeId = studentUserType.Id, + Username = "mathias", + Password = "mathias", + FirstName = "Mathias", + LastName = "Martinez", + Email = "mathias@mail.com", + Student = new Student + { + IdentityCard = "3782346-5", + TenantId = 2, + Age = 26 + }, + TenantId = 2, + }, + + new User + { + UserTypeId = professorUserType.Id, + Username = "prof1", + Password = "prof1", + FirstName = "Juan", + LastName = "Perez", + Email = "jp@mail.com", + Professor = new Professor + { + TenantId = 2, + IdentityCard = "98754342-5", + }, + TenantId = 2, + }, + + new User + { + UserTypeId = facultyAdminUserType.Id, + Username = "fing", + Password = "fing", + FirstName = "fing", + LastName = "fing", + Email = "fing@mail.com", + TenantId = 2, + }, + }; + + _context.Users.AddRange(users); + + try + { + await _context.SaveChangesAsync(); + } + catch (Exception ex) + { + + Console.WriteLine(ex.Message); + } + + // 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 sectionItemTypeSurvey = new SectionItemType + { + Name = SectionItemTypes.Survey, + }; + _context.SectionItemTypes.Add(sectionItemTypeSurvey); + + await _context.SaveChangesAsync(); + + // COURSES + var courses = new List<Course> + { + new Course + { + Id = 1, + Name = "Calculo 1", + TenantId = 2 + }, + + new Course + { + Id = 2, + Name = "GAL 1", + TenantId = 2 + }, + + new Course + { + Id = 3, + Name = "Fisica 1", + TenantId = 2 + } + }; + + _context.Courses.AddRange(courses); + + await _context.SaveChangesAsync(); + + + // SECTIONS + var section1 = new Section + { + CourseId = 1, + Name = "General", + Order = 1, + SectionItems = new List<SectionItem> + { + new SectionItem + { + Order = 1, + SectionItemTypeId = sectionItemTypeForum.Id, + Forum = new Forum + { + Name = "Novedades" + } + }, + new SectionItem + { + Order = 2, + SectionItemTypeId = sectionItemTypeForum.Id, + Forum = new Forum + { + Name = "General" + } + }, + } + }; + _context.Sections.Add(section1); + + var section2 = new Section + { + CourseId = 2, + Name = "General", + Order = 1, + SectionItems = new List<SectionItem> + { + new SectionItem + { + + Order = 1, + SectionItemTypeId = sectionItemTypeForum.Id, + Forum = new Forum + { + Name = "Novedades" + } + } + } + }; + _context.Sections.Add(section2); + + var section3 = new Section + { + CourseId = 1, + Name = "Tema 1", + Order = 2, + SectionItems = new List<SectionItem> + { + new SectionItem + { + Order = 1, + SectionItemTypeId = sectionItemTypeForum.Id, + Forum = new Forum + { + Name = "Tema 1" + } + } + } + }; + _context.Sections.Add(section3); + + await _context.SaveChangesAsync(); + + // 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(); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/AnswerOptionDetailDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/AnswerOptionDetailDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..0acb224a908f81bafc6338f50f8108510709035b --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/AnswerOptionDetailDto.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class AnswerOptionDetailDto + { + public int Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs index 2793c823175e8474e98d39514c7789927d23a2a0..d56100cf0435165372be069b17082b2438628864 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs @@ -51,6 +51,7 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<SurveyAnswer, SurveyAnswerCreateDto>(); CreateMap<Communication, CommunicationCreateDto>(); CreateMap<Communication, CommunicationPreviewDto>(); + CreateMap<AnswerOption, AnswerOptionDetailDto>(); CreateMap<ForumCreateDto, Forum>(); CreateMap<ForumPreviewDto, Forum>(); @@ -91,6 +92,7 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<SurveyAnswerCreateDto, SurveyAnswer>(); CreateMap<CommunicationCreateDto, Communication>(); CreateMap<CommunicationPreviewDto, Communication>(); + CreateMap<AnswerOptionDetailDto, AnswerOption>(); } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IChatService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IChatService.cs index cf332b0a11d11b5b77f9058d9d1a95488293b9ea..a79f4b1782a5111636d70efa79a199682db1816a 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IChatService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IChatService.cs @@ -11,7 +11,7 @@ namespace Tsi1.BusinessLayer.Interfaces { Task<Group> GetGroupByName(string groupName); - Task<ServiceResult<Group>> CreateGroup(Group group); + Task<ServiceResult<bool>> CreateGroup(string groupName, string connectionId, int userId); Task<bool> RemoveConnectionFromGroup(string connectionId); } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISurveyService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISurveyService.cs index 166da72495011ee0b30a6ccaee67e8f108288c3b..b521e5ae22ecb73203a5adfbaf5b3b7fb0570c4a 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISurveyService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISurveyService.cs @@ -15,5 +15,6 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<SurveyDetailDto>> GetMySurvey(int surveyId, int userId); Task<ServiceResult<bool>> Complete(SurveyResponseCreateDto surveyResponse); Task<ServiceResult<List<SurveyResponseDetailDto>>> GetAllResponses(int surveyId, string userType, int tenantId); + Task<ServiceResult<List<AnswerOptionDetailDto>>> GetAnswerOptions(); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/ChatService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/ChatService.cs index 4e4ac8e76aaca0017a0d35dca73cd1e57503da3e..93573625ea418e32ad924cf5124c8e93028e6689 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/ChatService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/ChatService.cs @@ -18,11 +18,35 @@ namespace Tsi1.BusinessLayer.Services { _tsi1Context = tsi1Context; } - public async Task<ServiceResult<Group>> CreateGroup(Group group) + + public async Task<ServiceResult<bool>> CreateGroup(string groupName, string connectionId, int userId) { - var result = new ServiceResult<Group>(); + var result = new ServiceResult<bool>(); + var existingGroup = true; + var group = await this.GetGroupByName(groupName); + + if (group == null) + { + existingGroup = false; + group = new Group + { + Name = groupName, + }; + } - _tsi1Context.Groups.Add(group); + var connection = new Connection + { + ConnectionId = connectionId, + UserId = userId, + }; + + group.Connections.Add(connection); + + if (!existingGroup) + { + _tsi1Context.Groups.Add(group); + } + try { await _tsi1Context.SaveChangesAsync(); @@ -51,6 +75,8 @@ namespace Tsi1.BusinessLayer.Services if (connection != null) { connection.Group = null; + _tsi1Context.Connections.Remove(connection); + await _tsi1Context.SaveChangesAsync(); } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs index 51beed6b69060913983701c8c88f8b1889fc5e1e..88f08b21443dc80e01e1decf4cd7feaf2080950f 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/SurveyService.cs @@ -206,5 +206,16 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<List<AnswerOptionDetailDto>>> GetAnswerOptions() + { + var result = new ServiceResult<List<AnswerOptionDetailDto>>(); + + var answerOptions = await _context.AnswerOptions.AsNoTracking().ToListAsync(); + + result.Data = _mapper.Map<List<AnswerOptionDetailDto>>(answerOptions); + + return result; + } } }