diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs index ef582f9fa58c2f162524f00d267a04c789091325..ffd3608d231c078c29b2a555f4b20ae565bfeaa8 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateDto.cs @@ -1,11 +1,15 @@ using System; using System.Collections.Generic; using System.Text; +using System.Text.Json.Serialization; namespace Tsi1.BusinessLayer.Dtos { public class CourseCreateDto { public string Name { get; set; } + + [JsonIgnore] + public int TenantId { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs index 2756562ff74849602f0f89d682c40384ea6d219f..b4ebbecf257f0cf9667d615450a776738d3bac0c 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/ForumCreateDto.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Text.Json.Serialization; namespace Tsi1.BusinessLayer.Dtos { diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/MessageCreateDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/MessageCreateDto.cs index 628b095fe51b156d84d73089fd18c79f9a1b9ff9..399bc0c012b0f37a93225778eb670c294e2ec90b 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/MessageCreateDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/MessageCreateDto.cs @@ -13,5 +13,8 @@ namespace Tsi1.BusinessLayer.Dtos public int ReceiverId { get; set; } public string Content { get; set; } + + [JsonIgnore] + public int TenantId { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs index 24616b382bf5c2ef36249f7b8af52e727fbcdae1..2cc5d2740edf2171ffa30d17e60389bc4290a17a 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserRegisterDto.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text; +using System.Text.Json.Serialization; namespace Tsi1.BusinessLayer.Dtos { @@ -28,6 +29,9 @@ namespace Tsi1.BusinessLayer.Dtos [Required] public string IdentityCard { get; set; } + [JsonIgnore] + public int TenantId { get; set; } + public int Age { get; set; } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs index df6907fa874b47fad6ce918464f4e0df12eb697e..d99c8411b5ba655306a387f6ce5221f96d259382 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/ICourseService.cs @@ -10,7 +10,7 @@ namespace Tsi1.BusinessLayer.Interfaces { public interface ICourseService { - Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType); + Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(string userType, int tenantId); Task<ServiceResult<Course>> Create(CourseCreateDto newCourse); } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs index 4f68ea6c1064f22937bc6ded6f426c0f2829fab7..a97ea5c312cf5448689c731402cf293ea49ebca4 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs @@ -13,6 +13,6 @@ namespace Tsi1.BusinessLayer.Interfaces { Task<ServiceResult<bool>> SendEmailAsync(MimeMessage message); - Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> users); + Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> mails); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IMessageService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IMessageService.cs index 785b46134f790b90ade833746481cb16b914c3f5..f328cf436e1a44241407725b7a906837b61d6641 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IMessageService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IMessageService.cs @@ -9,7 +9,7 @@ namespace Tsi1.BusinessLayer.Interfaces { public interface IMessageService { - Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId); + Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId, int tenantId); Task<ServiceResult<MessagePreviewDto>> Send(MessageCreateDto newMessage); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs index 3e6742718302d55ca3993f3e526e8871efa5a5cd..8b2f9d7ce3ae138b4428776b0f908011466dd142 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IUserService.cs @@ -10,10 +10,10 @@ namespace Tsi1.BusinessLayer.Interfaces { public interface IUserService { - Task<ServiceResult<User>> Authenticate(string username, string password); + Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId); Task<ServiceResult<User>> Create(UserRegisterDto dto, string type); - Task<ServiceResult<List<UserPreviewDto>>> GetAll(); + Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs index 05c175789cc8eb480a976c4db2076e781dcefa1d..266341df8b2290ff89af02c27ffbe31276128d6d 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/CourseService.cs @@ -37,7 +37,7 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType) + public async Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(string userType, int tenantId) { var result = new ServiceResult<List<CoursePreviewDto>>(); result.Data = new List<CoursePreviewDto>(); @@ -48,6 +48,7 @@ namespace Tsi1.BusinessLayer.Services { courses = await _context.StudentCourses .Include(x => x.Course) + .Where(x => x.Course.TenantId == tenantId) .Select(x => x.Course) .ToListAsync(); } @@ -55,6 +56,7 @@ namespace Tsi1.BusinessLayer.Services { courses = await _context.ProfessorCourses .Include(x => x.Course) + .Where(x => x.Course.TenantId == tenantId) .Select(x => x.Course) .ToListAsync(); } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs index fad2cf7baa0a7069eb16970db32cc6b0f8efe24e..d00e398d81ef6e05d34547d90d87d5a6e11f7ffd 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs @@ -70,13 +70,13 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> users) + public async Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> mails) { var message = new MimeMessage(); - foreach (var user in users) + foreach (var mail in mails) { - message.To.Add(MailboxAddress.Parse(user)); + message.To.Add(MailboxAddress.Parse(mail)); } message.Subject = $"Nuevo Post: {postCreateDto.Title}"; diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/MessageService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/MessageService.cs index eab3cb3f4a2440d25007d09237e542219de54096..b49bf792f6777608c45c0f28b1fa3b06f750b23e 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/MessageService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/MessageService.cs @@ -28,14 +28,14 @@ namespace Tsi1.BusinessLayer.Services _mapper = mapper; } - public async Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId) + public async Task<ServiceResult<List<MessagePreviewDto>>> GetMessages(int userId, int otherUserId, int tenantId) { var result = new ServiceResult<List<MessagePreviewDto>>(); var sort = Builders<Message>.Sort.Ascending("Date"); - var messages = await _messages.Find(m => (m.SenderId == userId && m.ReceiverId == otherUserId) - || (m.SenderId == otherUserId && m.ReceiverId == userId)) + var messages = await _messages.Find(m => (m.SenderId == userId && m.ReceiverId == otherUserId && m.TenantId == tenantId) + || (m.SenderId == otherUserId && m.ReceiverId == userId && m.TenantId == tenantId)) .Sort(sort) .ToListAsync(); diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs index 067a3317b0b92812470a91b85d613ef5266e440a..5385c44c7f4934315f71647f334d1f15b64ae58a 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/PostMessageService.cs @@ -45,7 +45,8 @@ namespace Tsi1.BusinessLayer.Services { var result = new ServiceResult<PostMessage>(); - var postMessage = await _context.PostMessages.FirstOrDefaultAsync(x => x.Id == postMessageId); + var postMessage = await _context.PostMessages + .FirstOrDefaultAsync(x => x.Id == postMessageId); if (postMessage == null) { diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs index e824af21ada188ab0a3221025a1959230a1135ef..b78defc03d10da7eb24b91eee8304d6fd8972e9b 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/UserService.cs @@ -25,13 +25,13 @@ namespace Tsi1.BusinessLayer.Services _mapper = mapper; } - public async Task<ServiceResult<User>> Authenticate(string username, string password) + public async Task<ServiceResult<User>> Authenticate(string username, string password, int tenantId) { var result = new ServiceResult<User>(); var user = await _context.Users .Include(x => x.UserType) - .FirstOrDefaultAsync(x => x.Username == username); + .FirstOrDefaultAsync(x => x.Username == username && x.TenantId == tenantId); if (user == null) { @@ -63,7 +63,8 @@ namespace Tsi1.BusinessLayer.Services Password = dto.Password, Email = dto.Email, FirstName = dto.FirstName, - LastName = dto.LastName + LastName = dto.LastName, + TenantId = dto.TenantId }; if (type == UserTypes.Student) @@ -71,7 +72,8 @@ namespace Tsi1.BusinessLayer.Services user.Student = new Student() { IdentityCard = dto.IdentityCard, - Age = dto.Age + Age = dto.Age, + TenantId = dto.TenantId }; } @@ -79,7 +81,8 @@ namespace Tsi1.BusinessLayer.Services { user.Professor = new Professor() { - IdentityCard = dto.IdentityCard + IdentityCard = dto.IdentityCard, + TenantId = dto.TenantId }; } @@ -90,12 +93,12 @@ namespace Tsi1.BusinessLayer.Services return result; } - public async Task<ServiceResult<List<UserPreviewDto>>> GetAll() + public async Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId) { var result = new ServiceResult<List<UserPreviewDto>>(); var users = await _context.Users - .Where(x => x.UserType.Name != UserTypes.FacultyAdmin) + .Where(x => x.UserType.Name != UserTypes.FacultyAdmin && x.TenantId == tenantId) .ToListAsync(); var usersDto = _mapper.Map<List<UserPreviewDto>>(users); diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs index c2ee29e9f389e639bb6e0602e86689d371eba172..0f4f050705ec5081ef0cd43de7f76b59f259d269 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Course.cs @@ -14,8 +14,13 @@ 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/Forum.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs index 4eda36d552fb2050d83c289eabc6aea048b4e418..197e1e01af9d0576764a418b53a2e165a0834ada 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs @@ -18,10 +18,6 @@ namespace Tsi1.DataLayer.Entities public int CourseId { get; set; } - public int TenantId { get; set; } - - public Tenant Tenant { get; set; } - public Course Course { get; set; } public ICollection<Post> Posts { get; set; } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Message.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Message.cs index 4925699629ad1863a4860433900dd189ea8d5fc7..76a37fc52f27639504a9b0b328beb1e457a032a1 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/Message.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Message.cs @@ -18,7 +18,9 @@ namespace Tsi1.DataLayer.Entities public string Content { get; set; } - public DateTime Date { get; set; } + public DateTime Date { get; set; } + + public int TenantId { get; set; } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/PostMessage.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/PostMessage.cs index 0e73c2ab6ea02ae3122df9a739f404022360cc2b..93638335a61d8b925e4ef49ffe44476e58f6db47 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/PostMessage.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/PostMessage.cs @@ -13,11 +13,10 @@ namespace Tsi1.DataLayer.Entities public DateTime Date { get; set; } public int UserId { get; set; } - - public User User { get; set; } - + public int PostId { get; set; } public Post Post { get; set; } + public User User { get; set; } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Professor.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Professor.cs index a095017f2316ce399aa783be02564ac58fc19a48..9fb5aeea36d7d4ffb999782b0b339f2125e48410 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/Professor.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Professor.cs @@ -12,8 +12,12 @@ namespace Tsi1.DataLayer.Entities } public int Id { get; set; } + public string IdentityCard { get; set; } + public int TenantId { get; set; } + + public Tenant Tenant { get; set; } public User User { get; set; } public ICollection<ProfessorCourse> ProfessorCourses { get; set; } } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Student.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Student.cs index acc85724b189d884047106a3d7423a138d5c639e..2688e376d376fd5b435f4f0be41fcec95c3dc2f0 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/Student.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Student.cs @@ -12,9 +12,14 @@ namespace Tsi1.DataLayer.Entities } public int Id { get; set; } + public string IdentityCard { get; set; } + public int Age { get; set; } + public int TenantId { get; set; } + + public Tenant Tenant { get; set; } public User User { get; set; } public ICollection<StudentCourse> StudentCourses { get; set; } } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs index d5201eca1c83babdb7268e9c9d6dcb78f537dd96..ce807be9044482303da464874681b0f0b4814d67 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Tenant.cs @@ -8,13 +8,19 @@ namespace Tsi1.DataLayer.Entities { public Tenant() { - Forums = new HashSet<Forum>(); + Courses = new HashSet<Course>(); + Professors = new HashSet<Professor>(); + Students = new HashSet<Student>(); + Users = new HashSet<User>(); } public int Id { get; set; } public string Name { get; set; } - public ICollection<Forum> Forums { get; set; } + public ICollection<Course> Courses { get; set; } + public ICollection<Professor> Professors { get; set; } + public ICollection<Student> Students { get; set; } + public ICollection<User> Users { get; set; } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs index dd7e8041f4af07d4d4fc0da1289bc83989cea95f..a62c4662edc5a781040ab78d454b33875ebb76d3 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs @@ -14,15 +14,26 @@ namespace Tsi1.DataLayer.Entities } public int Id { get; set; } + public int UserTypeId { get; set; } + public int? StudentId { get; set; } + public int? ProfessorId { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Email { get; set; } + public int TenantId { get; set; } + + public Tenant Tenant { get; set; } public UserType UserType { get; set; } public Student Student { get; set; } public Professor Professor { get; set; } diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/CourseConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/CourseConfiguration.cs index 2bec81eac60a7426e67afd8bba3392d9cf17c89c..4cea40149d552d51d4c6fab8bc3d4ae20e5b3b48 100644 --- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/CourseConfiguration.cs +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/CourseConfiguration.cs @@ -13,12 +13,16 @@ namespace Tsi1.DataLayer.EntityConfiguration { builder.HasKey(x => x.Id); - builder.HasIndex(x => x.Name) + builder.HasIndex(x => new { x.Name, x.TenantId }) .IsUnique(); builder.Property(x => x.Name) .IsRequired() .HasColumnType("character varying(50)"); + + builder.HasOne(x => x.Tenant) + .WithMany(x => x.Courses) + .HasForeignKey(x => x.TenantId); } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs index 1de5bbdcf518e9ee1b0fa9a2d566c804f74000d6..ac2d37069880b40303ee5208d896a1b3e1ed968e 100644 --- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumConfiguration.cs @@ -14,7 +14,7 @@ namespace Tsi1.DataLayer.EntityConfiguration { builder.HasKey(x => x.Id); - builder.HasIndex(x => new { x.CourseId, x.Name }) + builder.HasIndex(x => new { x.CourseId, x.Name}) .IsUnique(); builder.Property(x => x.Name) @@ -25,10 +25,6 @@ namespace Tsi1.DataLayer.EntityConfiguration .WithMany(x => x.Forums) .HasForeignKey(x => x.CourseId); - builder.HasOne(x => x.Tenant) - .WithMany(x => x.Forums) - .HasForeignKey(x => x.TenantId); - } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/PostConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/PostConfiguration.cs index 6635726cf454e1d916ff57627c04e9e90927f73e..9ae3c716e0baa8b5d6b0367655cde6c275cb6c49 100644 --- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/PostConfiguration.cs +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/PostConfiguration.cs @@ -14,7 +14,7 @@ namespace Tsi1.DataLayer.EntityConfiguration { builder.HasKey(x => x.Id); - builder.HasIndex(x => new { x.ForumId, x.Title }) + builder.HasIndex(x => new { x.ForumId, x.Title}) .IsUnique(); builder.Property(x => x.Title) diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ProfessorConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ProfessorConfiguration.cs index 679dae301ad59a3b95baaf17c70661b3926f00b9..47de03f75c540dda0c5cae76e37c88c9276c614b 100644 --- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ProfessorConfiguration.cs +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ProfessorConfiguration.cs @@ -13,12 +13,16 @@ namespace Tsi1.DataLayer.EntityConfiguration { builder.HasKey(x => x.Id); - builder.HasIndex(x => x.IdentityCard) + builder.HasIndex(x => new { x.IdentityCard, x.TenantId }) .IsUnique(); builder.Property(x => x.IdentityCard) .IsRequired() .HasColumnType("character varying(50)"); + + builder.HasOne(x => x.Tenant) + .WithMany(x => x.Professors) + .HasForeignKey(x => x.TenantId); } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/StudentConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/StudentConfiguration.cs index a7877ace1f806e899748ba2d436186b7dad33de4..7e841dbb8df245e655c401ea5894362f10993a95 100644 --- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/StudentConfiguration.cs +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/StudentConfiguration.cs @@ -13,7 +13,7 @@ namespace Tsi1.DataLayer.EntityConfiguration { builder.HasKey(x => x.Id); - builder.HasIndex(x => x.IdentityCard) + builder.HasIndex(x => new { x.IdentityCard, x.TenantId }) .IsUnique(); builder.Property(x => x.IdentityCard) @@ -22,6 +22,10 @@ namespace Tsi1.DataLayer.EntityConfiguration builder.Property(x => x.Age) .IsRequired(); + + builder.HasOne(x => x.Tenant) + .WithMany(x => x.Students) + .HasForeignKey(x => x.TenantId); } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/UserConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/UserConfiguration.cs index eadf441d032c10cc45e2b2552d386af604262e26..c10f5609c2debb61c96c15d0ee45049a79dacb8c 100644 --- a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/UserConfiguration.cs +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/UserConfiguration.cs @@ -13,7 +13,7 @@ namespace Tsi1.DataLayer.EntityConfiguration { builder.HasKey(x => x.Id); - builder.HasIndex(x => x.Username) + builder.HasIndex(x => new { x.Username, x.TenantId }) .IsUnique(); builder.Property(x => x.Username) @@ -43,6 +43,10 @@ namespace Tsi1.DataLayer.EntityConfiguration builder.HasOne(x => x.Professor) .WithOne(x => x.User) .HasForeignKey<User>(x => x.ProfessorId); + + builder.HasOne(x => x.Tenant) + .WithMany(x => x.Users) + .HasForeignKey(x => x.TenantId); } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.Designer.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.Designer.cs new file mode 100644 index 0000000000000000000000000000000000000000..61c250cfe0742eab60532524a5817db1d063c646 --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.Designer.cs @@ -0,0 +1,489 @@ +// <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("20201022010216_add-multy-tenancy")] + partial class addmultytenancy + { + 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.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.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>("TenantId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("TenantId"); + + b.HasIndex("CourseId", "Name", "TenantId") + .IsUnique(); + + 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.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<int>("TenantId") + .HasColumnType("integer"); + + b.Property<string>("Title") + .IsRequired() + .HasColumnType("character varying(100)"); + + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("TenantId"); + + b.HasIndex("UserId"); + + b.HasIndex("ForumId", "Title", "TenantId") + .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>("TenantId") + .HasColumnType("integer"); + + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("PostId"); + + b.HasIndex("TenantId"); + + 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.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.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", "Course") + .WithMany("Forums") + .HasForeignKey("CourseId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant") + .WithMany("Forums") + .HasForeignKey("TenantId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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.Tenant", "Tenant") + .WithMany("Posts") + .HasForeignKey("TenantId") + .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.Tenant", "Tenant") + .WithMany("PostMessages") + .HasForeignKey("TenantId") + .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.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/20201022010216_add-multy-tenancy.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.cs new file mode 100644 index 0000000000000000000000000000000000000000..c205fcedc807c1e2d1422e3cf13fab4bb87c484d --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201022010216_add-multy-tenancy.cs @@ -0,0 +1,373 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace Tsi1.DataLayer.Migrations +{ + public partial class addmultytenancy : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropIndex( + name: "IX_Users_Username", + table: "Users"); + + migrationBuilder.DropIndex( + name: "IX_Students_IdentityCard", + table: "Students"); + + migrationBuilder.DropIndex( + name: "IX_Professors_IdentityCard", + table: "Professors"); + + migrationBuilder.DropIndex( + name: "IX_Posts_ForumId_Title", + table: "Posts"); + + migrationBuilder.DropIndex( + name: "IX_Forums_CourseId_Name", + table: "Forums"); + + migrationBuilder.DropIndex( + name: "IX_Courses_Name", + table: "Courses"); + + migrationBuilder.AddColumn<int>( + name: "TenantId", + table: "Users", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn<int>( + name: "TenantId", + table: "Students", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn<int>( + name: "TenantId", + table: "Professors", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn<int>( + name: "TenantId", + table: "Posts", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn<int>( + name: "TenantId", + table: "PostMessages", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn<int>( + name: "TenantId", + table: "Forums", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn<int>( + name: "TenantId", + table: "Courses", + nullable: false, + defaultValue: 0); + + migrationBuilder.CreateTable( + name: "Tenants", + columns: table => new + { + Id = table.Column<int>(nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column<string>(type: "character varying(50)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Tenants", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Users_TenantId", + table: "Users", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_Users_Username_TenantId", + table: "Users", + columns: new[] { "Username", "TenantId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Students_TenantId", + table: "Students", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_Students_IdentityCard_TenantId", + table: "Students", + columns: new[] { "IdentityCard", "TenantId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Professors_TenantId", + table: "Professors", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_Professors_IdentityCard_TenantId", + table: "Professors", + columns: new[] { "IdentityCard", "TenantId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Posts_TenantId", + table: "Posts", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_Posts_ForumId_Title_TenantId", + table: "Posts", + columns: new[] { "ForumId", "Title", "TenantId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_PostMessages_TenantId", + table: "PostMessages", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_Forums_TenantId", + table: "Forums", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_Forums_CourseId_Name_TenantId", + table: "Forums", + columns: new[] { "CourseId", "Name", "TenantId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Courses_TenantId", + table: "Courses", + column: "TenantId"); + + migrationBuilder.CreateIndex( + name: "IX_Courses_Name_TenantId", + table: "Courses", + columns: new[] { "Name", "TenantId" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Tenants_Name", + table: "Tenants", + column: "Name", + unique: true); + + migrationBuilder.AddForeignKey( + name: "FK_Courses_Tenants_TenantId", + table: "Courses", + column: "TenantId", + principalTable: "Tenants", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Forums_Tenants_TenantId", + table: "Forums", + column: "TenantId", + principalTable: "Tenants", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_PostMessages_Tenants_TenantId", + table: "PostMessages", + column: "TenantId", + principalTable: "Tenants", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Posts_Tenants_TenantId", + table: "Posts", + column: "TenantId", + principalTable: "Tenants", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Professors_Tenants_TenantId", + table: "Professors", + column: "TenantId", + principalTable: "Tenants", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Students_Tenants_TenantId", + table: "Students", + column: "TenantId", + principalTable: "Tenants", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Users_Tenants_TenantId", + table: "Users", + column: "TenantId", + principalTable: "Tenants", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Courses_Tenants_TenantId", + table: "Courses"); + + migrationBuilder.DropForeignKey( + name: "FK_Forums_Tenants_TenantId", + table: "Forums"); + + migrationBuilder.DropForeignKey( + name: "FK_PostMessages_Tenants_TenantId", + table: "PostMessages"); + + migrationBuilder.DropForeignKey( + name: "FK_Posts_Tenants_TenantId", + table: "Posts"); + + migrationBuilder.DropForeignKey( + name: "FK_Professors_Tenants_TenantId", + table: "Professors"); + + migrationBuilder.DropForeignKey( + name: "FK_Students_Tenants_TenantId", + table: "Students"); + + migrationBuilder.DropForeignKey( + name: "FK_Users_Tenants_TenantId", + table: "Users"); + + migrationBuilder.DropTable( + name: "Tenants"); + + migrationBuilder.DropIndex( + name: "IX_Users_TenantId", + table: "Users"); + + migrationBuilder.DropIndex( + name: "IX_Users_Username_TenantId", + table: "Users"); + + migrationBuilder.DropIndex( + name: "IX_Students_TenantId", + table: "Students"); + + migrationBuilder.DropIndex( + name: "IX_Students_IdentityCard_TenantId", + table: "Students"); + + migrationBuilder.DropIndex( + name: "IX_Professors_TenantId", + table: "Professors"); + + migrationBuilder.DropIndex( + name: "IX_Professors_IdentityCard_TenantId", + table: "Professors"); + + migrationBuilder.DropIndex( + name: "IX_Posts_TenantId", + table: "Posts"); + + migrationBuilder.DropIndex( + name: "IX_Posts_ForumId_Title_TenantId", + table: "Posts"); + + migrationBuilder.DropIndex( + name: "IX_PostMessages_TenantId", + table: "PostMessages"); + + migrationBuilder.DropIndex( + name: "IX_Forums_TenantId", + table: "Forums"); + + migrationBuilder.DropIndex( + name: "IX_Forums_CourseId_Name_TenantId", + table: "Forums"); + + migrationBuilder.DropIndex( + name: "IX_Courses_TenantId", + table: "Courses"); + + migrationBuilder.DropIndex( + name: "IX_Courses_Name_TenantId", + table: "Courses"); + + migrationBuilder.DropColumn( + name: "TenantId", + table: "Users"); + + migrationBuilder.DropColumn( + name: "TenantId", + table: "Students"); + + migrationBuilder.DropColumn( + name: "TenantId", + table: "Professors"); + + migrationBuilder.DropColumn( + name: "TenantId", + table: "Posts"); + + migrationBuilder.DropColumn( + name: "TenantId", + table: "PostMessages"); + + migrationBuilder.DropColumn( + name: "TenantId", + table: "Forums"); + + migrationBuilder.DropColumn( + name: "TenantId", + table: "Courses"); + + migrationBuilder.CreateIndex( + name: "IX_Users_Username", + table: "Users", + column: "Username", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Students_IdentityCard", + table: "Students", + column: "IdentityCard", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Professors_IdentityCard", + table: "Professors", + column: "IdentityCard", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Posts_ForumId_Title", + table: "Posts", + columns: new[] { "ForumId", "Title" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Forums_CourseId_Name", + table: "Forums", + columns: new[] { "CourseId", "Name" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Courses_Name", + table: "Courses", + column: "Name", + unique: true); + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs index 993f66e459effd11bb2dc0b80bf5af6f6a65213c..87302ae42f0421fb44c201b4575e0be6e7a4cdd3 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs @@ -30,9 +30,14 @@ namespace Tsi1.DataLayer.Migrations .IsRequired() .HasColumnType("character varying(50)"); + b.Property<int>("TenantId") + .HasColumnType("integer"); + b.HasKey("Id"); - b.HasIndex("Name") + b.HasIndex("TenantId"); + + b.HasIndex("Name", "TenantId") .IsUnique(); b.ToTable("Courses"); @@ -52,9 +57,14 @@ namespace Tsi1.DataLayer.Migrations .IsRequired() .HasColumnType("character varying(50)"); + b.Property<int>("TenantId") + .HasColumnType("integer"); + b.HasKey("Id"); - b.HasIndex("CourseId", "Name") + b.HasIndex("TenantId"); + + b.HasIndex("CourseId", "Name", "TenantId") .IsUnique(); b.ToTable("Forums"); @@ -88,6 +98,9 @@ namespace Tsi1.DataLayer.Migrations b.Property<int>("ForumId") .HasColumnType("integer"); + b.Property<int>("TenantId") + .HasColumnType("integer"); + b.Property<string>("Title") .IsRequired() .HasColumnType("character varying(100)"); @@ -97,9 +110,11 @@ namespace Tsi1.DataLayer.Migrations b.HasKey("Id"); + b.HasIndex("TenantId"); + b.HasIndex("UserId"); - b.HasIndex("ForumId", "Title") + b.HasIndex("ForumId", "Title", "TenantId") .IsUnique(); b.ToTable("Posts"); @@ -122,6 +137,9 @@ namespace Tsi1.DataLayer.Migrations b.Property<int>("PostId") .HasColumnType("integer"); + b.Property<int>("TenantId") + .HasColumnType("integer"); + b.Property<int>("UserId") .HasColumnType("integer"); @@ -129,6 +147,8 @@ namespace Tsi1.DataLayer.Migrations b.HasIndex("PostId"); + b.HasIndex("TenantId"); + b.HasIndex("UserId"); b.ToTable("PostMessages"); @@ -145,9 +165,14 @@ namespace Tsi1.DataLayer.Migrations .IsRequired() .HasColumnType("character varying(50)"); + b.Property<int>("TenantId") + .HasColumnType("integer"); + b.HasKey("Id"); - b.HasIndex("IdentityCard") + b.HasIndex("TenantId"); + + b.HasIndex("IdentityCard", "TenantId") .IsUnique(); b.ToTable("Professors"); @@ -182,9 +207,14 @@ namespace Tsi1.DataLayer.Migrations .IsRequired() .HasColumnType("character varying(50)"); + b.Property<int>("TenantId") + .HasColumnType("integer"); + b.HasKey("Id"); - b.HasIndex("IdentityCard") + b.HasIndex("TenantId"); + + b.HasIndex("IdentityCard", "TenantId") .IsUnique(); b.ToTable("Students"); @@ -205,6 +235,25 @@ namespace Tsi1.DataLayer.Migrations 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") @@ -234,6 +283,9 @@ namespace Tsi1.DataLayer.Migrations b.Property<int?>("StudentId") .HasColumnType("integer"); + b.Property<int>("TenantId") + .HasColumnType("integer"); + b.Property<int>("UserTypeId") .HasColumnType("integer"); @@ -249,9 +301,11 @@ namespace Tsi1.DataLayer.Migrations b.HasIndex("StudentId") .IsUnique(); + b.HasIndex("TenantId"); + b.HasIndex("UserTypeId"); - b.HasIndex("Username") + b.HasIndex("Username", "TenantId") .IsUnique(); b.ToTable("Users"); @@ -276,6 +330,15 @@ namespace Tsi1.DataLayer.Migrations b.ToTable("UserTypes"); }); + 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", "Course") @@ -283,6 +346,12 @@ namespace Tsi1.DataLayer.Migrations .HasForeignKey("CourseId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant") + .WithMany("Forums") + .HasForeignKey("TenantId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b => @@ -308,6 +377,12 @@ namespace Tsi1.DataLayer.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant") + .WithMany("Posts") + .HasForeignKey("TenantId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("Tsi1.DataLayer.Entities.User", "User") .WithMany("Posts") .HasForeignKey("UserId") @@ -323,6 +398,12 @@ namespace Tsi1.DataLayer.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant") + .WithMany("PostMessages") + .HasForeignKey("TenantId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("Tsi1.DataLayer.Entities.User", "User") .WithMany("PostMessages") .HasForeignKey("UserId") @@ -330,6 +411,15 @@ namespace Tsi1.DataLayer.Migrations .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") @@ -345,6 +435,15 @@ namespace Tsi1.DataLayer.Migrations .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") @@ -370,6 +469,12 @@ namespace Tsi1.DataLayer.Migrations .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")