diff --git a/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs b/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs index c0e99fbbcffdbea35382eda2355c5ce83d4ce6f6..a34cad3f2a12a8128da4362cc316a077d7b189b1 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/ForumController.cs @@ -36,7 +36,7 @@ namespace Tsi1.Api.Controllers return Ok(result.Data); } - [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] + [Authorize(UserTypes.Professor)] [HttpPost("Create")] public async Task<IActionResult> Create(ForumCreateDto newForum) { @@ -50,7 +50,7 @@ namespace Tsi1.Api.Controllers return Ok(); } - [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] + [Authorize(UserTypes.Professor)] [HttpDelete("Delete/{forumId}")] public async Task<IActionResult> Delete(int forumId) { @@ -63,5 +63,22 @@ namespace Tsi1.Api.Controllers return Ok(); } + + [Authorize(Roles = UserTypes.Student)] + [HttpGet("Subscribe/{forumId}")] + public async Task<IActionResult> Subscribe(int forumId) + { + var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value); + + var result = await _forumService.Subscribe(forumId, userId); + + if (result.HasError) + { + return BadRequest(result.Message); + } + + return Ok(); + } + } } diff --git a/Tsi1.Api/Tsi1.Api/Controllers/PostController.cs b/Tsi1.Api/Tsi1.Api/Controllers/PostController.cs index eb1c19e5a54fc654f061ea3fea103b473a04ec17..7c64c6c2d027f352c60633d935e7911475b4b593 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/PostController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/PostController.cs @@ -17,11 +17,19 @@ namespace Tsi1.Api.Controllers { private readonly IPostService _postService; - public PostController(IPostService postService) + private readonly IForumService _forumService; + + private readonly IEmailService _emailService; + + public PostController(IPostService postService, IForumService forumService, + IEmailService emailService) { _postService = postService; + _forumService = forumService; + _emailService = emailService; } + [Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)] [HttpGet("GetPosts/{forumId}")] public async Task<IActionResult> GetPosts(int forumId) @@ -51,6 +59,10 @@ namespace Tsi1.Api.Controllers return BadRequest(result.Message); } + var userEmails = await _forumService.GetSubscribedUsers(newPost.ForumId); + + await _emailService.NotifyNewPostOrMessage(newPost, userEmails.Data); + return Ok(); } diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs index c6a4f5f77e219c2b0ef5d769949efae4528551d4..c2039f807c016c5a5cc472fab047c366d4234e7d 100644 --- a/Tsi1.Api/Tsi1.Api/Startup.cs +++ b/Tsi1.Api/Tsi1.Api/Startup.cs @@ -55,7 +55,10 @@ namespace Tsi1.Api services.AddScoped<ICourseService, CourseService>(); services.AddScoped<IForumService, ForumService>(); services.AddScoped<IPostService, PostService>(); - services.AddScoped<IPostMessageService, PostMessageService>(); + services.AddScoped<IPostMessageService, PostMessageService>(); + + services.Configure<MailSettings>(Configuration.GetSection("MailSettings")); + services.AddScoped<IEmailService, EmailService>(); services.AddCors(); diff --git a/Tsi1.Api/Tsi1.Api/appsettings.json b/Tsi1.Api/Tsi1.Api/appsettings.json index 8de3212efd973a8247f91a2ae359f60135d9b95d..e637ca6e8eb7e6a16d907ad18a2eb348b0fe590c 100644 --- a/Tsi1.Api/Tsi1.Api/appsettings.json +++ b/Tsi1.Api/Tsi1.Api/appsettings.json @@ -14,6 +14,13 @@ "accessTokenExpiration": 20, "refreshTokenExpiration": 60 }, + "MailSettings": { + "Mail": "tsi1.grupo2.2020@gmail.com", + "DisplayName": "tsi1 grupo 2 2020", + "Password": "VamoTSI.2020", + "Host": "smtp.gmail.com", + "Port": 587 + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs index 31f2b3b0c045864ad893ff0566cf445e4beea5ac..0dbeef495bbeaf299bc1edeeeac6b1b653eaa583 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/ErrorMessages.cs @@ -13,5 +13,9 @@ namespace Tsi1.BusinessLayer.Helpers public const string ForumDoesNotExist = "El foro con id '{0}' no existe"; public const string PostDoesNotExist = "El post con id '{0}' no existe"; public const string PostMessageDoesNotExist = "El mensage con id '{0}' no existe"; + + public const string CannotConnectToSmtpServer = "No se pudo conectar al servidor SMTP"; + public const string CannotAuthenticateToSmtpServer = "No se pudo autenticar en el servidor SMTP"; + public const string CannotSendEmail = "No se pudo mandar el mail con asunto {0}"; } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MailSettings.cs b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MailSettings.cs new file mode 100644 index 0000000000000000000000000000000000000000..c7500384882dff2a95347fc29a607ee6c292d47c --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Helpers/MailSettings.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Helpers +{ + public class MailSettings + { + public string Mail { get; set; } + + public string DisplayName { get; set; } + + public string Password { get; set; } + + public string Host { get; set; } + + public int Port { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs new file mode 100644 index 0000000000000000000000000000000000000000..4f68ea6c1064f22937bc6ded6f426c0f2829fab7 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IEmailService.cs @@ -0,0 +1,18 @@ +using MimeKit; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; +using Tsi1.DataLayer.Entities; + +namespace Tsi1.BusinessLayer.Interfaces +{ + public interface IEmailService + { + Task<ServiceResult<bool>> SendEmailAsync(MimeMessage message); + + Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> users); + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IForumService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IForumService.cs index 254f08f137c110c9c6e6cb8378a0d4432fc0f240..12d94600ab697452919232f3f12239614f811fe2 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IForumService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IForumService.cs @@ -15,5 +15,9 @@ namespace Tsi1.BusinessLayer.Interfaces Task<ServiceResult<Forum>> Create(ForumCreateDto newForum); Task<ServiceResult<Forum>> Delete(int forumId); + + Task<ServiceResult<List<string>>> GetSubscribedUsers(int forumId); + + Task<ServiceResult<List<bool>>> Subscribe(int forumId, int userId); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs new file mode 100644 index 0000000000000000000000000000000000000000..fad2cf7baa0a7069eb16970db32cc6b0f8efe24e --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/EmailService.cs @@ -0,0 +1,94 @@ +using MailKit; +using MailKit.Net.Smtp; +using MailKit.Security; +using Microsoft.Extensions.Options; +using MimeKit; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Tsi1.BusinessLayer.Dtos; +using Tsi1.BusinessLayer.Helpers; +using Tsi1.BusinessLayer.Interfaces; +using Tsi1.DataLayer.Entities; + +namespace Tsi1.BusinessLayer.Services +{ + public class EmailService : IEmailService + { + private readonly MailSettings _mailSettings; + + public EmailService(IOptions<MailSettings> mailSettings) + { + _mailSettings = mailSettings.Value; + } + + public async Task<ServiceResult<bool>> SendEmailAsync(MimeMessage message) + { + ServiceResult<bool> result = new ServiceResult<bool>(); + + message.Sender = MailboxAddress.Parse(_mailSettings.Mail); + + var client = new SmtpClient(); + client.CheckCertificateRevocation = false; + + try + { + await client.ConnectAsync(_mailSettings.Host, _mailSettings.Port, SecureSocketOptions.StartTls); + } + catch (Exception e) + { + result.HasError = true; + result.Message = ErrorMessages.CannotConnectToSmtpServer; + return result; + } + + try + { + await client.AuthenticateAsync(_mailSettings.Mail, _mailSettings.Password); + } + catch (Exception) + { + result.HasError = true; + result.Message = ErrorMessages.CannotAuthenticateToSmtpServer; + return result; + } + + try + { + await client.SendAsync(message); + } + catch (Exception) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.CannotSendEmail, message.Subject); + return result; + } + + await client.DisconnectAsync(true); + + return result; + } + + public async Task<ServiceResult<bool>> NotifyNewPostOrMessage(PostCreateDto postCreateDto, List<string> users) + { + var message = new MimeMessage(); + + foreach (var user in users) + { + message.To.Add(MailboxAddress.Parse(user)); + } + + message.Subject = $"Nuevo Post: {postCreateDto.Title}"; + message.Body = new TextPart("html") + { + Text = $"<p> Hay un nuevo post perteneciente al foro suscripto." + }; + + var result = await SendEmailAsync(message); + + return result; + } + + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs index e17d9c881b7de8040e53d692ac4615f877f650f2..827cd381abf5ff45ff7dc02e1ab5029ae9e127f7 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/ForumService.cs @@ -1,5 +1,6 @@ using AutoMapper; using Microsoft.EntityFrameworkCore; +using Org.BouncyCastle.Math.EC.Rfc7748; using System; using System.Collections.Generic; using System.Linq; @@ -73,5 +74,45 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<List<string>>> GetSubscribedUsers(int forumId) + { + var result = new ServiceResult<List<string>>(); + + var userEmails = await _context.ForumUsers + .Where(x => x.ForumId == forumId) + .Select(x => x.User.Email) + .ToListAsync(); + + result.Data = userEmails; + + return result; + } + + public async Task<ServiceResult<List<bool>>> Subscribe(int forumId, int userId) + { + var result = new ServiceResult<List<bool>>(); + + var forum = await _context.Forums.FirstOrDefaultAsync(x => x.Id == forumId); + + if (forum == null) + { + result.HasError = true; + result.Message = string.Format(ErrorMessages.ForumDoesNotExist, forumId); + return result; + } + + var forumUser = new ForumUser + { + ForumId = forumId, + UserId = userId, + }; + + _context.ForumUsers.Add(forumUser); + + await _context.SaveChangesAsync(); + + return result; + } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Tsi1.BusinessLayer.csproj b/Tsi1.Api/Tsi1.BusinessLayer/Tsi1.BusinessLayer.csproj index bf03c351a4e0b3702f4c73b7b986ddf95efa3291..a21bff7c2af2796490606eab2c96c0b6c51598ae 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Tsi1.BusinessLayer.csproj +++ b/Tsi1.Api/Tsi1.BusinessLayer/Tsi1.BusinessLayer.csproj @@ -7,6 +7,8 @@ <ItemGroup> <PackageReference Include="AutoMapper" Version="10.1.1" /> <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" /> + <PackageReference Include="MailKit" Version="2.9.0" /> + <PackageReference Include="MimeKit" Version="2.9.2" /> </ItemGroup> <ItemGroup> diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs index 9cb5a15180ecd75c36bcba58e02d4bdb6e5a8d91..4eda36d552fb2050d83c289eabc6aea048b4e418 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/Forum.cs @@ -9,6 +9,7 @@ namespace Tsi1.DataLayer.Entities public Forum() { Posts = new HashSet<Post>(); + ForumUsers = new HashSet<ForumUser>(); } public int Id { get; set; } @@ -24,5 +25,6 @@ namespace Tsi1.DataLayer.Entities public Course Course { get; set; } public ICollection<Post> Posts { get; set; } + public ICollection<ForumUser> ForumUsers { get; set; } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/ForumUser.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/ForumUser.cs new file mode 100644 index 0000000000000000000000000000000000000000..1559edb73db90702240b13c7a5e7d7fc04a35bbe --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/ForumUser.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.DataLayer.Entities +{ + public class ForumUser + { + public int ForumId { get; set; } + public int UserId { get; set; } + + public Forum Forum { get; set; } + public User User { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs b/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs index 22d5a1802400aa0b6fd99fa74d86842ea22fc3c3..dd7e8041f4af07d4d4fc0da1289bc83989cea95f 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Entities/User.cs @@ -6,6 +6,13 @@ namespace Tsi1.DataLayer.Entities { public class User { + public User() + { + Posts = new HashSet<Post>(); + PostMessages = new HashSet<PostMessage>(); + ForumUsers = new HashSet<ForumUser>(); + } + public int Id { get; set; } public int UserTypeId { get; set; } public int? StudentId { get; set; } @@ -22,5 +29,6 @@ namespace Tsi1.DataLayer.Entities public ICollection<Post> Posts { get; set; } public ICollection<PostMessage> PostMessages { get; set; } + public ICollection<ForumUser> ForumUsers { get; set; } } } diff --git a/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumUserConfiguration.cs b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumUserConfiguration.cs new file mode 100644 index 0000000000000000000000000000000000000000..e1baf07c54c78b3405d6aff2088493ad54f07195 --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/EntityConfiguration/ForumUserConfiguration.cs @@ -0,0 +1,25 @@ +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 ForumUserConfiguration : IEntityTypeConfiguration<ForumUser> + { + public void Configure(EntityTypeBuilder<ForumUser> builder) + { + builder.HasKey(x => new { x.ForumId, x.UserId }); + + builder.HasOne(x => x.Forum) + .WithMany(x => x.ForumUsers) + .HasForeignKey(x => x.ForumId); + + builder.HasOne(x => x.User) + .WithMany(x => x.ForumUsers) + .HasForeignKey(x => x.UserId); + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/20201020233929_add-ForumUser-relation.Designer.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201020233929_add-ForumUser-relation.Designer.cs new file mode 100644 index 0000000000000000000000000000000000000000..1043ddfe8720ed2cba0a63af3e2249bfae32c700 --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201020233929_add-ForumUser-relation.Designer.cs @@ -0,0 +1,384 @@ +// <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("20201020233929_add-ForumUser-relation")] + partial class addForumUserrelation + { + 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.HasKey("Id"); + + b.HasIndex("Name") + .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.HasKey("Id"); + + b.HasIndex("CourseId", "Name") + .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<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.HasKey("Id"); + + b.HasIndex("IdentityCard") + .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.HasKey("Id"); + + b.HasIndex("IdentityCard") + .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.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>("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("UserTypeId"); + + b.HasIndex("Username") + .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.Forum", b => + { + b.HasOne("Tsi1.DataLayer.Entities.Course", "Course") + .WithMany("Forums") + .HasForeignKey("CourseId") + .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.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.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.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.UserType", "UserType") + .WithMany() + .HasForeignKey("UserTypeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/20201020233929_add-ForumUser-relation.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201020233929_add-ForumUser-relation.cs new file mode 100644 index 0000000000000000000000000000000000000000..6774e6c4df9edc6f6abab4d73202c68d9f640fd9 --- /dev/null +++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/20201020233929_add-ForumUser-relation.cs @@ -0,0 +1,45 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace Tsi1.DataLayer.Migrations +{ + public partial class addForumUserrelation : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "ForumUsers", + columns: table => new + { + ForumId = table.Column<int>(nullable: false), + UserId = table.Column<int>(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ForumUsers", x => new { x.ForumId, x.UserId }); + table.ForeignKey( + name: "FK_ForumUsers_Forums_ForumId", + column: x => x.ForumId, + principalTable: "Forums", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ForumUsers_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ForumUsers_UserId", + table: "ForumUsers", + column: "UserId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ForumUsers"); + } + } +} diff --git a/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs b/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs index 3cbd29d3c18fa4564c466da2e18055065ebb6454..993f66e459effd11bb2dc0b80bf5af6f6a65213c 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Migrations/Tsi1ContextModelSnapshot.cs @@ -60,6 +60,21 @@ namespace Tsi1.DataLayer.Migrations 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") @@ -270,6 +285,21 @@ namespace Tsi1.DataLayer.Migrations .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") diff --git a/Tsi1.Api/Tsi1.DataLayer/Tsi1Context.cs b/Tsi1.Api/Tsi1.DataLayer/Tsi1Context.cs index 83f1d1526a4cb166a45ff7bee494484ae12fa9e7..c9fb14f74a1844a4fdc09fb7a9dea866975c2430 100644 --- a/Tsi1.Api/Tsi1.DataLayer/Tsi1Context.cs +++ b/Tsi1.Api/Tsi1.DataLayer/Tsi1Context.cs @@ -20,6 +20,7 @@ namespace Tsi1.DataLayer public DbSet<Post> Posts { get; set; } public DbSet<PostMessage> PostMessages { get; set; } public DbSet<Tenant> Tenants { get; set; } + public DbSet<ForumUser> ForumUsers { get; set; } public Tsi1Context(DbContextOptions options) : base(options) { } @@ -37,6 +38,7 @@ namespace Tsi1.DataLayer modelBuilder.ApplyConfiguration(new PostConfiguration()); modelBuilder.ApplyConfiguration(new PostMessageConfiguration()); modelBuilder.ApplyConfiguration(new TenantConfiguration()); + modelBuilder.ApplyConfiguration(new ForumUserConfiguration()); } } }