From 04d60321bbbe004a8ec1af7db9d61a8e33a38918 Mon Sep 17 00:00:00 2001 From: Lucca Santangelo <luccasant95@gmail.com> Date: Fri, 6 Nov 2020 20:23:56 -0300 Subject: [PATCH] section items --- .../Tsi1.Api/Controllers/ForumController.cs | 4 +- .../Tsi1.Api/Controllers/SectionController.cs | 15 + .../Controllers/SectionItemController.cs | 59 ++ Tsi1.Api/Tsi1.Api/Startup.cs | 1 + Tsi1.Api/Tsi1.BusinessLayer/Dtos/FileDto.cs | 3 - .../Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs | 2 - .../Dtos/SectionItemCreateDto.cs | 18 + .../Dtos/SectionItemOrderDto.cs | 12 + .../Helpers/ErrorMessages.cs | 10 + .../Helpers/MappingProfile.cs | 2 + .../Helpers/SectionItemTypes.cs | 12 + .../Helpers/ServiceResult.cs | 12 + .../Interfaces/IFileService.cs | 3 +- .../Interfaces/IForumService.cs | 2 +- .../Interfaces/ISectionItemService.cs | 16 + .../Services/FileService.cs | 6 + .../Services/ForumService.cs | 37 +- .../Services/SectionItemService.cs | 156 +++++ Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs | 3 - Tsi1.Api/Tsi1.DataLayer/Entities/File.cs | 5 +- Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs | 7 +- Tsi1.Api/Tsi1.DataLayer/Entities/Section.cs | 19 + .../Tsi1.DataLayer/Entities/SectionItem.cs | 21 + .../Entities/SectionItemType.cs | 19 + .../EntityConfiguration/ForumConfiguration.cs | 12 +- .../SectionConfiguration.cs | 24 + .../SectionItemConfiguration.cs | 36 ++ .../SectionItemTypeConfiguration.cs | 21 + .../20201106232002_section_items.Designer.cs | 612 ++++++++++++++++++ .../20201106232002_section_items.cs | 191 ++++++ .../Migrations/Tsi1ContextModelSnapshot.cs | 112 +++- Tsi1.Api/Tsi1.DataLayer/Tsi1Context.cs | 6 + 32 files changed, 1406 insertions(+), 52 deletions(-) create mode 100644 Tsi1.Api/Tsi1.Api/Controllers/SectionController.cs create mode 100644 Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemCreateDto.cs create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemOrderDto.cs create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Helpers/SectionItemTypes.cs create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs create mode 100644 Tsi1.Api/Tsi1.DataLayer/Entities/Section.cs create mode 100644 Tsi1.Api/Tsi1.DataLayer/Entities/SectionItem.cs create mode 100644 Tsi1.Api/Tsi1.DataLayer/Entities/SectionItemType.cs create mode 100644 Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionConfiguration.cs create mode 100644 Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionItemConfiguration.cs create mode 100644 Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionItemTypeConfiguration.cs create mode 100644 Tsi1.Api/Tsi1.DataLayer/Migrations/20201106232002_section_items.Designer.cs create mode 100644 Tsi1.Api/Tsi1.DataLayer/Migrations/20201106232002_section_items.cs diff --git a/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs b/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs index ce849fa..b185203 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs @@ -24,9 +24,9 @@ namespace Tsi1.Api.Controllers [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] [HttpGet("GetForums/{courseId}")] - public async Task<IActionResult> GetForums(int courseId) + public async Task<IActionResult> GetForums(int sectionId) { - var result = await _forumService.GetForums(courseId); + var result = await _forumService.GetForums(sectionId); if (result.HasError) { diff --git a/Tsi1.Api/Tsi1.Api/Controllers/SectionController.cs b/Tsi1.Api/Tsi1.Api/Controllers/SectionController.cs new file mode 100644 index 0000000..a4ef022 --- /dev/null +++ b/Tsi1.Api/Tsi1.Api/Controllers/SectionController.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Tsi1.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class SectionController : ControllerBase + { + } +} diff --git a/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs b/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs new file mode 100644 index 0000000..1eae7d6 --- /dev/null +++ b/Tsi1.Api/Tsi1.Api/Controllers/SectionItemController.cs @@ -0,0 +1,59 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; +using Tsi1.BusinessLayer.Interfaces; + +namespace Tsi1.Api.Controllers +{ + [Authorize(Roles = UserTypes.Professor)] + [Route("api/[controller]")] + [ApiController] + public class SectionItemController : ControllerBase + { + private readonly ISectionItemService _sectionItemService; + + public SectionItemController(ISectionItemService sectionItemService) + { + _sectionItemService = sectionItemService; + } + + [HttpPost("Create")] + public async Task<IActionResult> Create(SectionItemCreateDto sectionItem) + { + var result = await _sectionItemService.Create(sectionItem); + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(); + } + + [HttpDelete("Delete/{sectionItemId}")] + public async Task<IActionResult> Delete(int sectionItemId) + { + var result = await _sectionItemService.Delete(sectionItemId); + if (result.HasError) + { + return NotFound(result.Message); + } + + return Ok(); + } + + [HttpPost("ChangeOrder")] + public async Task<IActionResult> ChangeOrder(List<SectionItemOrderDto> orderDtos) + { + var result = await _sectionItemService.OrderSectionItems(orderDtos); + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(); + } + } +} diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs index 135212e..71f448f 100644 --- a/Tsi1.Api/Tsi1.Api/Startup.cs +++ b/Tsi1.Api/Tsi1.Api/Startup.cs @@ -79,6 +79,7 @@ namespace Tsi1.Api services.AddScoped<IPostMessageService, PostMessageService>(); services.AddScoped<ITenantService, TenantService>(); services.AddScoped<IFileService, FileService>(); + services.AddScoped<ISectionItemService, SectionItemService>(); services.Configure<MailSettings>(Configuration.GetSection("MailSettings")); services.AddScoped<IEmailService, EmailService>(); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/FileDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/FileDto.cs index 977a059..f16d17f 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/FileDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/FileDto.cs @@ -6,10 +6,7 @@ namespace Tsi1.BusinessLayer.Dtos { public class FileDto { - public int Id { get; set; } - public string Name { get; set; } - public string Path { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs index b4ebbec..4c4d099 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs @@ -8,7 +8,5 @@ namespace Tsi1.BusinessLayer.Dtos public class ForumCreateDto { public string Name { get; set; } - - public int CourseId { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemCreateDto.cs new file mode 100644 index 0000000..60a7ac2 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemCreateDto.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class SectionItemCreateDto + { + public int Order { get; set; } + public int SectionId { get; set; } + public int SectionItemTypeId { get; set; } + public int? ForumId { get; set; } + public int? FileId { get; set; } + + public ForumCreateDto Forum { get; set; } + public FileDto File { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemOrderDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemOrderDto.cs new file mode 100644 index 0000000..15e252c --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/SectionItemOrderDto.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class SectionItemOrderDto + { + public int SectionItemId { get; set; } + public int Order { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs index 8dc7c9f..3f57fdf 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs @@ -42,5 +42,15 @@ namespace Tsi1.BusinessLayer.Helpers public const string ErrorSavingFile = "No se pudo guardar el archivo '{0}'"; public const string ErrorDeletingFile = "No se pudo borrar el archivo '{0}'"; public const string DuplicateFile = "Ya existe el archivo '{0}'"; + public const string FileDoesNotExist = "El archivo '{0}' no existe fisicamente en el file server"; + + public const string SectionDoesNotExist = "La seccion con id '{0}' no existe"; + public const string SectionItemDoesNotExist = "El item de seccion con id '{0}' no existe"; + public const string DuplicateSectionItemOrder = "Hay items de seccion con el mismo orden"; + + public const string InvalidSectionItemType = "El tipo de item de seccion '{0}' es invalido"; + public const string ForumIsNull = "El foro es nulo"; + public const string FileIsNull = "El archivo es nulo"; + public const string InvalidSectionItemData = "El tipo de item de seccion '{0}' no coincide con su contenido '{1}'"; } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs index 00a67f4..d9dff00 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MappingProfile.cs @@ -32,6 +32,7 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<Tenant, TenantCourseDto>().ForMember(x => x.Courses, opt => opt.Ignore()); CreateMap<UserType, UserTypeDto>(); CreateMap<File, FileDto>(); + CreateMap<SectionItem, SectionItemCreateDto>(); CreateMap<ForumCreateDto, Forum>(); CreateMap<ForumPreviewDto, Forum>(); @@ -53,6 +54,7 @@ namespace Tsi1.BusinessLayer.Helpers CreateMap<TenantCourseDto, Tenant>(); CreateMap<UserTypeDto, UserType>(); CreateMap<FileDto, File>(); + CreateMap<SectionItemCreateDto, SectionItem>(); } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/SectionItemTypes.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/SectionItemTypes.cs new file mode 100644 index 0000000..0eee4f0 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/SectionItemTypes.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Helpers +{ + public static class SectionItemTypes + { + public const string Forum = nameof(Forum); + public const string File = nameof(File); + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ServiceResult.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ServiceResult.cs index ae294d7..2e9e2fe 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ServiceResult.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ServiceResult.cs @@ -9,5 +9,17 @@ namespace Tsi1.BusinessLayer.Helpers public T Data { get; set; } public string Message { get; set; } public bool HasError { get; set; } + + public void AddMessage(string message) + { + if (string.IsNullOrEmpty(this.Message)) + { + this.Message = message; + } + else + { + this.Message += System.Environment.NewLine + message; + } + } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IFileService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IFileService.cs index a42bbf6..ced47ca 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IFileService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IFileService.cs @@ -11,9 +11,8 @@ namespace Tsi1.BusinessLayer.Interfaces public interface IFileService { Task<ServiceResult<FileDto>> Create(IFormFile file, int tenantId, int courseId); - void CreateFolder(string folderPath); - void DeleteFolder(string folderPath); + bool ExistFile(string relativePath); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IForumService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IForumService.cs index 12d9460..db95e50 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IForumService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IForumService.cs @@ -10,7 +10,7 @@ namespace Tsi1.BusinessLayer.Interfaces { public interface IForumService { - Task<ServiceResult<List<ForumPreviewDto>>> GetForums(int courseId); + Task<ServiceResult<List<ForumPreviewDto>>> GetForums(int sectionId); Task<ServiceResult<Forum>> Create(ForumCreateDto newForum); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs new file mode 100644 index 0000000..d169c20 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ISectionItemService.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; + +namespace Tsi1.BusinessLayer.Interfaces +{ + public interface ISectionItemService + { + Task<ServiceResult<bool>> Create(SectionItemCreateDto newSectionItem); + Task<ServiceResult<bool>> Delete(int sectionItemId); + Task<ServiceResult<bool>> OrderSectionItems(List<SectionItemOrderDto> orderDtos); + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs index 3152501..937a334 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs @@ -135,5 +135,11 @@ namespace Tsi1.BusinessLayer.Services Directory.Delete(path); } + + public bool ExistFile(string relativePath) + { + var path = Path.Combine(_path, relativePath); + return System.IO.File.Exists(path); + } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs index 1865a42..6418cd5 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs @@ -29,25 +29,12 @@ namespace Tsi1.BusinessLayer.Services public async Task<ServiceResult<Forum>> Create(ForumCreateDto newForum) { var result = new ServiceResult<Forum>(); - - var existingForum = await _context.Forums - .FirstOrDefaultAsync(x => x.Name == newForum.Name && x.CourseId == newForum.CourseId); - - if (existingForum != null) - { - result.HasError = true; - result.Message = string.Format(ErrorMessages.DuplicateForumName, newForum.Name); - return result; - } - var forum = _mapper.Map<Forum>(newForum); _context.Forums.Add(forum); - await _context.SaveChangesAsync(); result.Data = forum; - return result; } @@ -71,13 +58,29 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<List<ForumPreviewDto>>> GetForums(int courseId) + public async Task<ServiceResult<List<ForumPreviewDto>>> GetForums(int sectionId) { var result = new ServiceResult<List<ForumPreviewDto>>(); - var forums = await _context.Forums - .Where(x => x.CourseId == courseId) - .ToListAsync(); + var section = await _context.Sections + .Where(x => x.Id == sectionId) + .Include(x => x.SectionItems) + .ThenInclude(x => x.SectionItemType) + .Include(x => x.SectionItems) + .ThenInclude(x => x.Forum) + .FirstOrDefaultAsync(); + + if (section == null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.SectionDoesNotExist, sectionId); + return result; + } + + var forums = section.SectionItems + .Where(x => x.SectionItemType.Name == SectionItemTypes.Forum) + .Select(x => x.Forum) + .ToList(); var forumDtos = _mapper.Map<List<ForumPreviewDto>>(forums); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs new file mode 100644 index 0000000..fe4f531 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/SectionItemService.cs @@ -0,0 +1,156 @@ +using AutoMapper; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +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.Services +{ + public class SectionItemService : ISectionItemService + { + private readonly IMapper _mapper; + private readonly Tsi1Context _context; + private readonly IFileService _fileService; + + public SectionItemService(IMapper mapper, Tsi1Context context, IFileService fileService) + { + _mapper = mapper; + _context = context; + _fileService = fileService; + } + + public async Task<ServiceResult<bool>> Create(SectionItemCreateDto newSectionItem) + { + var sectionItem = _mapper.Map<SectionItem>(newSectionItem); + + var result = await this.SectionItemValidations(sectionItem); + + if (result.HasError) + { + return result; + } + + _context.SectionItems.Add(sectionItem); + await _context.SaveChangesAsync(); + + return result; + } + + public async Task<ServiceResult<bool>> Delete(int sectionItemId) + { + var result = new ServiceResult<bool>(); + + var sectionItem = await _context.SectionItems.FirstOrDefaultAsync(x => x.Id == sectionItemId); + if (sectionItem == null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.SectionItemDoesNotExist, sectionItemId)); + return result; + } + + _context.SectionItems.Remove(sectionItem); + await _context.SaveChangesAsync(); + + return result; + } + + public async Task<ServiceResult<bool>> OrderSectionItems(List<SectionItemOrderDto> orderDtos) + { + var result = new ServiceResult<bool>(); + + var sectionItemsIds = orderDtos.Select(x => x.SectionItemId); + var orders = orderDtos.Select(x => x.Order).Distinct(); + + if (orders.Count() != orderDtos.Count()) + { + result.HasError = true; + result.AddMessage(ErrorMessages.DuplicateSectionItemOrder); + return result; + } + + var sectionItems = await _context.SectionItems + .Where(x => sectionItemsIds.Contains(x.Id)) + .ToListAsync(); + + foreach (var orderDto in orderDtos) + { + var sectionItem = sectionItems.FirstOrDefault(x => x.Id == orderDto.SectionItemId); + if (sectionItem == null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.SectionItemDoesNotExist, orderDto.SectionItemId)); + } + + sectionItem.Order = orderDto.Order; + } + + if (!result.HasError) + { + await _context.SaveChangesAsync(); + } + + return result; + } + + private async Task<ServiceResult<bool>> SectionItemValidations(SectionItem sectionItem) + { + var result = new ServiceResult<bool>(); + + var sectionItemType = await _context.SectionItemTypes + .FirstOrDefaultAsync(x => x.Id == sectionItem.SectionItemTypeId); + + if (sectionItemType == null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemType, sectionItem.SectionItemTypeId)); + return result; + } + + if (sectionItemType.Name == SectionItemTypes.Forum) + { + if (sectionItem.Forum == null) + { + result.HasError = true; + result.AddMessage(ErrorMessages.ForumIsNull); + } + + if (sectionItem.File != null) + { + result.HasError = true; + result.AddMessage(string.Format(ErrorMessages.InvalidSectionItemData, sectionItemType.Name, SectionItemTypes.File)); + } + } + + if (sectionItemType.Name == SectionItemTypes.File) + { + if (sectionItem.File == null) + { + result.HasError = true; + result.AddMessage(ErrorMessages.FileIsNull); + } + + if (sectionItem.Forum != 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)); + } + } + + return result; + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs index 0f4f050..d77eefa 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs @@ -14,13 +14,10 @@ namespace Tsi1.DataLayer.Entities } public int Id { get; set; } - public string Name { get; set; } - public int TenantId { get; set; } public Tenant Tenant { get; set; } - public ICollection<StudentCourse> StudentCourses { get; set; } public ICollection<ProfessorCourse> ProfessorCourses { get; set; } public ICollection<Forum> Forums { get; set; } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/File.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/File.cs index 4643d9c..19ce03f 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/File.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/File.cs @@ -7,9 +7,10 @@ namespace Tsi1.DataLayer.Entities public class File { public int Id { get; set; } - + public int SectionItemId { get; set; } public string Name { get; set; } - public string Path { get; set; } + + public SectionItem SectionItem { get; set; } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs index 197e1e0..1bb2559 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs @@ -13,13 +13,10 @@ namespace Tsi1.DataLayer.Entities } public int Id { get; set; } - public string Name { get; set; } + public int SectionItemId { get; set; } - public int CourseId { get; set; } - - public Course Course { get; set; } - + public SectionItem SectionItem { get; set; } public ICollection<Post> Posts { get; set; } public ICollection<ForumUser> ForumUsers { get; set; } } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Section.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Section.cs new file mode 100644 index 0000000..033231a --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Section.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.DataLayer.Entities +{ + public class Section + { + public Section() + { + SectionItems = new HashSet<SectionItem>(); + } + + public int Id { get; set; } + public string Name { get; set; } + public int Order { get; set; } + public ICollection<SectionItem> SectionItems { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/SectionItem.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/SectionItem.cs new file mode 100644 index 0000000..3bd1ea2 --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/SectionItem.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.DataLayer.Entities +{ + public class SectionItem + { + public int Id { get; set; } + public int Order { get; set; } + public int SectionId { get; set; } + public int SectionItemTypeId { get; set; } + public int? ForumId { get; set; } + public int? FileId { get; set; } + + public Section Section { get; set; } + public SectionItemType SectionItemType { get; set; } + public Forum Forum { get; set; } + public File File { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/SectionItemType.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/SectionItemType.cs new file mode 100644 index 0000000..438d864 --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/SectionItemType.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.DataLayer.Entities +{ + public class SectionItemType + { + public SectionItemType() + { + SectionItems = new HashSet<SectionItem>(); + } + + public int Id { get; set; } + public string Name { get; set; } + + public ICollection<SectionItem> SectionItems { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs index ac2d370..be40300 100644 --- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs @@ -8,23 +8,19 @@ using Tsi1.DataLayer.Entities; namespace Tsi1.DataLayer.EntityConfiguration { - class ForumConfiguration : IEntityTypeConfiguration<Forum> + public class ForumConfiguration : IEntityTypeConfiguration<Forum> { public void Configure(EntityTypeBuilder<Forum> builder) { builder.HasKey(x => x.Id); - builder.HasIndex(x => new { x.CourseId, x.Name}) - .IsUnique(); - builder.Property(x => x.Name) .IsRequired() .HasColumnType("character varying(50)"); - builder.HasOne(x => x.Course) - .WithMany(x => x.Forums) - .HasForeignKey(x => x.CourseId); - + builder.HasOne(x => x.SectionItem) + .WithOne(x => x.Forum) + .HasForeignKey<Forum>(x => x.SectionItemId); } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionConfiguration.cs new file mode 100644 index 0000000..8b2f806 --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionConfiguration.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using System; +using System.Collections.Generic; +using System.Text; +using Tsi1.DataLayer.Entities; + +namespace Tsi1.DataLayer.EntityConfiguration +{ + public class SectionConfiguration : IEntityTypeConfiguration<Section> + { + public void Configure(EntityTypeBuilder<Section> builder) + { + builder.HasKey(x => x.Id); + + builder.Property(x => x.Name) + .IsRequired() + .HasColumnType("character varying(255)"); + + builder.Property(x => x.Order) + .IsRequired(); + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionItemConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionItemConfiguration.cs new file mode 100644 index 0000000..ffcda8d --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionItemConfiguration.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using System; +using System.Collections.Generic; +using System.Text; +using Tsi1.DataLayer.Entities; + +namespace Tsi1.DataLayer.EntityConfiguration +{ + public class SectionItemConfiguration : IEntityTypeConfiguration<SectionItem> + { + public void Configure(EntityTypeBuilder<SectionItem> builder) + { + builder.HasKey(x => x.Id); + + builder.Property(x => x.Order) + .IsRequired(); + + builder.HasOne(x => x.File) + .WithOne(x => x.SectionItem) + .HasForeignKey<SectionItem>(x => x.FileId); + + builder.HasOne(x => x.Forum) + .WithOne(x => x.SectionItem) + .HasForeignKey<SectionItem>(x => x.ForumId); + + builder.HasOne(x => x.SectionItemType) + .WithMany(x => x.SectionItems) + .HasForeignKey(x => x.SectionItemTypeId); + + builder.HasOne(x => x.Section) + .WithMany(x => x.SectionItems) + .HasForeignKey(x => x.SectionId); + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionItemTypeConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionItemTypeConfiguration.cs new file mode 100644 index 0000000..fdef281 --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/SectionItemTypeConfiguration.cs @@ -0,0 +1,21 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using System; +using System.Collections.Generic; +using System.Text; +using Tsi1.DataLayer.Entities; + +namespace Tsi1.DataLayer.EntityConfiguration +{ + public class SectionItemTypeConfiguration : IEntityTypeConfiguration<SectionItemType> + { + public void Configure(EntityTypeBuilder<SectionItemType> builder) + { + builder.HasKey(x => x.Id); + + builder.Property(x => x.Name) + .IsRequired() + .HasColumnType("character varying(100)"); + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/20201106232002_section_items.Designer.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201106232002_section_items.Designer.cs new file mode 100644 index 0000000..bffe3f0 --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201106232002_section_items.Designer.cs @@ -0,0 +1,612 @@ +// <auto-generated /> +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using Tsi1.DataLayer; + +namespace Tsi1.DataLayer.Migrations +{ + [DbContext(typeof(Tsi1Context))] + [Migration("20201106232002_section_items")] + partial class section_items + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) + .HasAnnotation("ProductVersion", "3.1.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Connection", b => + { + b.Property<string>("ConnectionId") + .HasColumnType("text"); + + b.Property<string>("GroupName") + .HasColumnType("text"); + + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.HasKey("ConnectionId"); + + b.HasIndex("GroupName"); + + b.ToTable("Connections"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Course", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("character varying(50)"); + + b.Property<int>("TenantId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("TenantId"); + + b.HasIndex("Name", "TenantId") + .IsUnique(); + + b.ToTable("Courses"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.File", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("character varying(50)"); + + b.Property<string>("Path") + .IsRequired() + .HasColumnType("character varying(1000)"); + + b.Property<int>("SectionItemId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("Path") + .IsUnique(); + + b.ToTable("Files"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<int?>("CourseId") + .HasColumnType("integer"); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("character varying(50)"); + + b.Property<int>("SectionItemId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CourseId"); + + b.ToTable("Forums"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b => + { + b.Property<int>("ForumId") + .HasColumnType("integer"); + + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.HasKey("ForumId", "UserId"); + + b.HasIndex("UserId"); + + b.ToTable("ForumUsers"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Group", b => + { + b.Property<string>("Name") + .HasColumnType("text"); + + b.HasKey("Name"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Post", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<DateTime>("Date") + .HasColumnType("timestamp without time zone"); + + b.Property<int>("ForumId") + .HasColumnType("integer"); + + b.Property<string>("Title") + .IsRequired() + .HasColumnType("character varying(100)"); + + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.HasIndex("ForumId", "Title") + .IsUnique(); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.PostMessage", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Content") + .IsRequired() + .HasColumnType("character varying(10485760)"); + + b.Property<DateTime>("Date") + .HasColumnType("timestamp without time zone"); + + b.Property<int>("PostId") + .HasColumnType("integer"); + + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("PostId"); + + b.HasIndex("UserId"); + + b.ToTable("PostMessages"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Professor", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("IdentityCard") + .IsRequired() + .HasColumnType("character varying(50)"); + + b.Property<int>("TenantId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("TenantId"); + + b.HasIndex("IdentityCard", "TenantId") + .IsUnique(); + + b.ToTable("Professors"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.ProfessorCourse", b => + { + b.Property<int>("ProfessorId") + .HasColumnType("integer"); + + b.Property<int>("CourseId") + .HasColumnType("integer"); + + b.HasKey("ProfessorId", "CourseId"); + + b.HasIndex("CourseId"); + + b.ToTable("ProfessorCourses"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Section", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("character varying(255)"); + + b.Property<int>("Order") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Sections"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.SectionItem", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<int?>("FileId") + .HasColumnType("integer"); + + b.Property<int?>("ForumId") + .HasColumnType("integer"); + + b.Property<int>("Order") + .HasColumnType("integer"); + + b.Property<int>("SectionId") + .HasColumnType("integer"); + + b.Property<int>("SectionItemTypeId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FileId") + .IsUnique(); + + b.HasIndex("ForumId") + .IsUnique(); + + b.HasIndex("SectionId"); + + b.HasIndex("SectionItemTypeId"); + + b.ToTable("SectionItems"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.SectionItemType", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.ToTable("SectionItemTypes"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<int>("Age") + .HasColumnType("integer"); + + b.Property<string>("IdentityCard") + .IsRequired() + .HasColumnType("character varying(50)"); + + b.Property<int>("TenantId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("TenantId"); + + b.HasIndex("IdentityCard", "TenantId") + .IsUnique(); + + b.ToTable("Students"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.StudentCourse", b => + { + b.Property<int>("StudentId") + .HasColumnType("integer"); + + b.Property<int>("CourseId") + .HasColumnType("integer"); + + b.HasKey("StudentId", "CourseId"); + + b.HasIndex("CourseId"); + + b.ToTable("StudentCourses"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Tenant", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("character varying(50)"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Tenants"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.User", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Email") + .IsRequired() + .HasColumnType("character varying(255)"); + + b.Property<string>("FirstName") + .IsRequired() + .HasColumnType("character varying(255)"); + + b.Property<string>("LastName") + .IsRequired() + .HasColumnType("character varying(255)"); + + b.Property<string>("Password") + .IsRequired() + .HasColumnType("character varying(255)"); + + b.Property<int?>("ProfessorId") + .HasColumnType("integer"); + + b.Property<int?>("StudentId") + .HasColumnType("integer"); + + b.Property<int>("TenantId") + .HasColumnType("integer"); + + b.Property<int>("UserTypeId") + .HasColumnType("integer"); + + b.Property<string>("Username") + .IsRequired() + .HasColumnType("character varying(50)"); + + b.HasKey("Id"); + + b.HasIndex("ProfessorId") + .IsUnique(); + + b.HasIndex("StudentId") + .IsUnique(); + + b.HasIndex("TenantId"); + + b.HasIndex("UserTypeId"); + + b.HasIndex("Username", "TenantId") + .IsUnique(); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.UserType", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("character varying(50)"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("UserTypes"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Connection", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Group", "Group") + .WithMany("Connections") + .HasForeignKey("GroupName"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Course", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant") + .WithMany("Courses") + .HasForeignKey("TenantId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Course", null) + .WithMany("Forums") + .HasForeignKey("CourseId"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum") + .WithMany("ForumUsers") + .HasForeignKey("ForumId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.User", "User") + .WithMany("ForumUsers") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Post", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum") + .WithMany("Posts") + .HasForeignKey("ForumId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.User", "User") + .WithMany("Posts") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.PostMessage", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Post", "Post") + .WithMany("PostMessages") + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.User", "User") + .WithMany("PostMessages") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Professor", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant") + .WithMany("Professors") + .HasForeignKey("TenantId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.ProfessorCourse", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Course", "Course") + .WithMany("ProfessorCourses") + .HasForeignKey("CourseId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.Professor", "Professor") + .WithMany("ProfessorCourses") + .HasForeignKey("ProfessorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.SectionItem", b => + { + b.HasOne("Tsi1.DataLayer.Entities.File", "File") + .WithOne("SectionItem") + .HasForeignKey("Tsi1.DataLayer.Entities.SectionItem", "FileId"); + + b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum") + .WithOne("SectionItem") + .HasForeignKey("Tsi1.DataLayer.Entities.SectionItem", "ForumId"); + + b.HasOne("Tsi1.DataLayer.Entities.Section", "Section") + .WithMany("SectionItems") + .HasForeignKey("SectionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.SectionItemType", "SectionItemType") + .WithMany("SectionItems") + .HasForeignKey("SectionItemTypeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant") + .WithMany("Students") + .HasForeignKey("TenantId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.StudentCourse", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Course", "Course") + .WithMany("StudentCourses") + .HasForeignKey("CourseId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.Student", "Student") + .WithMany("StudentCourses") + .HasForeignKey("StudentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.User", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Professor", "Professor") + .WithOne("User") + .HasForeignKey("Tsi1.DataLayer.Entities.User", "ProfessorId"); + + b.HasOne("Tsi1.DataLayer.Entities.Student", "Student") + .WithOne("User") + .HasForeignKey("Tsi1.DataLayer.Entities.User", "StudentId"); + + b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant") + .WithMany("Users") + .HasForeignKey("TenantId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.UserType", "UserType") + .WithMany() + .HasForeignKey("UserTypeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/20201106232002_section_items.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201106232002_section_items.cs new file mode 100644 index 0000000..c3b4c6e --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201106232002_section_items.cs @@ -0,0 +1,191 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace Tsi1.DataLayer.Migrations +{ + public partial class section_items : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Forums_Courses_CourseId", + table: "Forums"); + + migrationBuilder.DropIndex( + name: "IX_Forums_CourseId_Name", + table: "Forums"); + + migrationBuilder.AlterColumn<int>( + name: "CourseId", + table: "Forums", + nullable: true, + oldClrType: typeof(int), + oldType: "integer"); + + migrationBuilder.AddColumn<int>( + name: "SectionItemId", + table: "Forums", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn<int>( + name: "SectionItemId", + table: "Files", + nullable: false, + defaultValue: 0); + + migrationBuilder.CreateTable( + name: "SectionItemTypes", + columns: table => new + { + Id = table.Column<int>(nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column<string>(type: "character varying(100)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SectionItemTypes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Sections", + columns: table => new + { + Id = table.Column<int>(nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column<string>(type: "character varying(255)", nullable: false), + Order = table.Column<int>(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Sections", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "SectionItems", + columns: table => new + { + Id = table.Column<int>(nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Order = table.Column<int>(nullable: false), + SectionId = table.Column<int>(nullable: false), + SectionItemTypeId = table.Column<int>(nullable: false), + ForumId = table.Column<int>(nullable: true), + FileId = table.Column<int>(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_SectionItems", x => x.Id); + table.ForeignKey( + name: "FK_SectionItems_Files_FileId", + column: x => x.FileId, + principalTable: "Files", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_SectionItems_Forums_ForumId", + column: x => x.ForumId, + principalTable: "Forums", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + table.ForeignKey( + name: "FK_SectionItems_Sections_SectionId", + column: x => x.SectionId, + principalTable: "Sections", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SectionItems_SectionItemTypes_SectionItemTypeId", + column: x => x.SectionItemTypeId, + principalTable: "SectionItemTypes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Forums_CourseId", + table: "Forums", + column: "CourseId"); + + migrationBuilder.CreateIndex( + name: "IX_SectionItems_FileId", + table: "SectionItems", + column: "FileId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_SectionItems_ForumId", + table: "SectionItems", + column: "ForumId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_SectionItems_SectionId", + table: "SectionItems", + column: "SectionId"); + + migrationBuilder.CreateIndex( + name: "IX_SectionItems_SectionItemTypeId", + table: "SectionItems", + column: "SectionItemTypeId"); + + migrationBuilder.AddForeignKey( + name: "FK_Forums_Courses_CourseId", + table: "Forums", + column: "CourseId", + principalTable: "Courses", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Forums_Courses_CourseId", + table: "Forums"); + + migrationBuilder.DropTable( + name: "SectionItems"); + + migrationBuilder.DropTable( + name: "Sections"); + + migrationBuilder.DropTable( + name: "SectionItemTypes"); + + migrationBuilder.DropIndex( + name: "IX_Forums_CourseId", + table: "Forums"); + + migrationBuilder.DropColumn( + name: "SectionItemId", + table: "Forums"); + + migrationBuilder.DropColumn( + name: "SectionItemId", + table: "Files"); + + migrationBuilder.AlterColumn<int>( + name: "CourseId", + table: "Forums", + type: "integer", + nullable: false, + oldClrType: typeof(int), + oldNullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Forums_CourseId_Name", + table: "Forums", + columns: new[] { "CourseId", "Name" }, + unique: true); + + migrationBuilder.AddForeignKey( + name: "FK_Forums_Courses_CourseId", + table: "Forums", + column: "CourseId", + principalTable: "Courses", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs index 9f2b80d..419247e 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs @@ -76,6 +76,9 @@ namespace Tsi1.DataLayer.Migrations .IsRequired() .HasColumnType("character varying(1000)"); + b.Property<int>("SectionItemId") + .HasColumnType("integer"); + b.HasKey("Id"); b.HasIndex("Path") @@ -91,17 +94,19 @@ namespace Tsi1.DataLayer.Migrations .HasColumnType("integer") .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); - b.Property<int>("CourseId") + b.Property<int?>("CourseId") .HasColumnType("integer"); b.Property<string>("Name") .IsRequired() .HasColumnType("character varying(50)"); + b.Property<int>("SectionItemId") + .HasColumnType("integer"); + b.HasKey("Id"); - b.HasIndex("CourseId", "Name") - .IsUnique(); + b.HasIndex("CourseId"); b.ToTable("Forums"); }); @@ -229,6 +234,78 @@ namespace Tsi1.DataLayer.Migrations b.ToTable("ProfessorCourses"); }); + modelBuilder.Entity("Tsi1.DataLayer.Entities.Section", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("character varying(255)"); + + b.Property<int>("Order") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Sections"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.SectionItem", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<int?>("FileId") + .HasColumnType("integer"); + + b.Property<int?>("ForumId") + .HasColumnType("integer"); + + b.Property<int>("Order") + .HasColumnType("integer"); + + b.Property<int>("SectionId") + .HasColumnType("integer"); + + b.Property<int>("SectionItemTypeId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("FileId") + .IsUnique(); + + b.HasIndex("ForumId") + .IsUnique(); + + b.HasIndex("SectionId"); + + b.HasIndex("SectionItemTypeId"); + + b.ToTable("SectionItems"); + }); + + modelBuilder.Entity("Tsi1.DataLayer.Entities.SectionItemType", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.ToTable("SectionItemTypes"); + }); + modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b => { b.Property<int>("Id") @@ -384,11 +461,9 @@ namespace Tsi1.DataLayer.Migrations modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b => { - b.HasOne("Tsi1.DataLayer.Entities.Course", "Course") + b.HasOne("Tsi1.DataLayer.Entities.Course", null) .WithMany("Forums") - .HasForeignKey("CourseId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .HasForeignKey("CourseId"); }); modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b => @@ -460,6 +535,29 @@ namespace Tsi1.DataLayer.Migrations .IsRequired(); }); + modelBuilder.Entity("Tsi1.DataLayer.Entities.SectionItem", b => + { + b.HasOne("Tsi1.DataLayer.Entities.File", "File") + .WithOne("SectionItem") + .HasForeignKey("Tsi1.DataLayer.Entities.SectionItem", "FileId"); + + b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum") + .WithOne("SectionItem") + .HasForeignKey("Tsi1.DataLayer.Entities.SectionItem", "ForumId"); + + b.HasOne("Tsi1.DataLayer.Entities.Section", "Section") + .WithMany("SectionItems") + .HasForeignKey("SectionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.SectionItemType", "SectionItemType") + .WithMany("SectionItems") + .HasForeignKey("SectionItemTypeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b => { b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant") diff --git a/Tsi1.Api/Tsi1.DataLayer/Tsi1Context.cs b/Tsi1.Api/Tsi1.DataLayer/Tsi1Context.cs index d442517..ba1524c 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Tsi1Context.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Tsi1Context.cs @@ -24,6 +24,9 @@ namespace Tsi1.DataLayer public DbSet<Group> Groups { get; set; } public DbSet<Connection> Connections { get; set; } public DbSet<File> Files { get; set; } + public DbSet<Section> Sections { get; set; } + public DbSet<SectionItem> SectionItems { get; set; } + public DbSet<SectionItemType> SectionItemTypes { get; set; } public Tsi1Context(DbContextOptions options) : base(options) { } @@ -45,6 +48,9 @@ namespace Tsi1.DataLayer modelBuilder.ApplyConfiguration(new GroupConfiguration()); modelBuilder.ApplyConfiguration(new ConnectionConfiguration()); modelBuilder.ApplyConfiguration(new FileConfiguration()); + modelBuilder.ApplyConfiguration(new SectionConfiguration()); + modelBuilder.ApplyConfiguration(new SectionItemConfiguration()); + modelBuilder.ApplyConfiguration(new SectionItemTypeConfiguration()); } } } -- GitLab