Skip to content
Snippets Groups Projects
Commit 90b944e0 authored by Lucca Santangelo Dodera's avatar Lucca Santangelo Dodera
Browse files

Merge branch 'feature/forum-automatic-subscription' into 'develop'

student automatic subscription

See merge request !36
parents ca95ca28 d01da17f
No related branches found
No related tags found
1 merge request!36student automatic subscription
Pipeline #10596 passed
Showing
with 1089 additions and 13 deletions
......@@ -190,7 +190,6 @@ namespace Tsi1.Api.Controllers
return Ok(result.Data);
}
[Authorize(Roles = UserTypes.Professor + ", " + UserTypes.FacultyAdmin)]
[HttpPut("Modify/{courseId}")]
public async Task<IActionResult> Modify(int courseId, CourseModifyDto courseDto)
......
......@@ -36,12 +36,31 @@ namespace Tsi1.Api.Controllers
return Ok(result.Data);
}
//[Authorize(Roles = UserTypes.Professor)]
//[HttpPost("Create")]
//public async Task<IActionResult> Create(ForumCreateDto newForum)
//{
// var result = await _forumService.Create(newForum);
// if (result.HasError)
// {
// return BadRequest(result.Message);
// }
// return Ok(result.Data);
//}
[Authorize(Roles = UserTypes.Professor)]
[HttpPost("Create")]
public async Task<IActionResult> Create(ForumCreateDto newForum)
[HttpPut("{id}")]
public async Task<IActionResult> Modify(int id, ForumModifyDto forum)
{
var result = await _forumService.Create(newForum);
var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
forum.TenantId = tenantId;
forum.ProfessorUserId = userId;
var result = await _forumService.Modify(id, forum);
if (result.HasError)
{
return BadRequest(result.Message);
......
......@@ -8,5 +8,6 @@ namespace Tsi1.BusinessLayer.Dtos
public class ForumCreateDto
{
public string Name { get; set; }
public bool IsAutomaticSubscription { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
namespace Tsi1.BusinessLayer.Dtos
{
public class ForumModifyDto
{
public string Name { get; set; }
public bool IsAutomaticSubscription { get; set; }
[JsonIgnore]
public int TenantId { get; set; }
[JsonIgnore]
public int ProfessorUserId { get; set; }
}
}
......@@ -14,6 +14,10 @@ namespace Tsi1.BusinessLayer.Helpers
{
CreateMap<Forum, ForumCreateDto>();
CreateMap<Forum, ForumPreviewDto>();
CreateMap<Forum, ForumModifyDto>()
.ForMember(x => x.TenantId, opt => opt.Ignore())
.ForMember(x => x.ProfessorUserId, opt => opt.Ignore());
CreateMap<Post, PostCreateDto>();
CreateMap<Post, PostPreviewDto>();
CreateMap<PostMessage, PostMessageCreateDto>();
......@@ -61,6 +65,7 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<ForumCreateDto, Forum>();
CreateMap<ForumPreviewDto, Forum>();
CreateMap<ForumModifyDto, Forum>();
CreateMap<PostCreateDto, Post>();
CreateMap<PostPreviewDto, Post>();
CreateMap<PostMessageCreateDto, PostMessage>();
......
......@@ -11,13 +11,10 @@ namespace Tsi1.BusinessLayer.Interfaces
public interface IForumService
{
Task<ServiceResult<List<ForumPreviewDto>>> GetForums(int sectionId);
Task<ServiceResult<int>> Create(ForumCreateDto newForum);
Task<ServiceResult<int>> Modify(int id, ForumModifyDto forum);
Task<ServiceResult<Forum>> Delete(int forumId);
Task<ServiceResult<List<string>>> GetSubscribedUsers(int forumId);
Task<ServiceResult<List<bool>>> Subscribe(int forumId, int userId);
}
}
......@@ -108,7 +108,11 @@ namespace Tsi1.BusinessLayer.Services
return result;
}
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == courseId);
var course = await _context.Courses
.Include(x => x.Sections)
.ThenInclude(x => x.SectionItems)
.ThenInclude(x => x.Forum)
.FirstOrDefaultAsync(x => x.Id == courseId);
if (course == null)
{
......@@ -134,14 +138,28 @@ namespace Tsi1.BusinessLayer.Services
return result;
}
var automaticSubscriptionForums = course.Sections
.SelectMany(x => x.SectionItems)
.Select(x => x.Forum)
.Where(x => x.IsAutomaticSubscription);
foreach (var forum in automaticSubscriptionForums)
{
var userForum = new ForumUser()
{
ForumId = forum.Id,
UserId = user.Id
};
user.ForumUsers.Add(userForum);
}
var studentCourse = new StudentCourse
{
Course = course,
Student = user.Student
Course = course
};
_context.StudentCourses.Add(studentCourse);
user.Student.StudentCourses.Add(studentCourse);
await _context.SaveChangesAsync();
result.Data = true;
......
......@@ -103,6 +103,49 @@ namespace Tsi1.BusinessLayer.Services
return result;
}
public async Task<ServiceResult<int>> Modify(int id, ForumModifyDto forum)
{
var result = new ServiceResult<int>();
var dbForum = await _context.Forums
.Include(x => x.SectionItem)
.ThenInclude(x => x.Section)
.ThenInclude(x => x.Course)
.ThenInclude(x => x.ProfessorCourses)
.Where(x => x.Id == id)
.FirstOrDefaultAsync();
var dbProfessorUser = await _context.Users
.Include(x => x.Professor)
.FirstOrDefaultAsync(x => x.Id == forum.ProfessorUserId);
if (dbForum == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.ForumDoesNotExist, id));
return result;
}
var dbCourse = dbForum.SectionItem.Section.Course;
var dbProfessorUserIds = dbCourse.ProfessorCourses.Select(x => x.ProfessorId);
var dbProfessor = dbProfessorUser.Professor;
if (!dbProfessorUserIds.Contains(dbProfessor.Id))
{
result.HasError = true;
result.AddMessage(string.Format(
ErrorMessages.ProfessorCourseDoesNotExists,
string.Join(" ", dbProfessorUser.FirstName, dbProfessorUser.LastName),
dbCourse.Name));
return result;
}
_mapper.Map(forum, dbForum);
await _context.SaveChangesAsync();
return result;
}
public async Task<ServiceResult<List<bool>>> Subscribe(int forumId, int userId)
{
var result = new ServiceResult<List<bool>>();
......
......@@ -14,6 +14,7 @@ namespace Tsi1.DataLayer.Entities
public int Id { get; set; }
public string Name { get; set; }
public bool IsAutomaticSubscription { get; set; }
public SectionItem SectionItem { get; set; }
public ICollection<Post> Posts { get; set; }
......
using Microsoft.EntityFrameworkCore.Migrations;
namespace Tsi1.DataLayer.Migrations
{
public partial class forumisAutomaticSubscription : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsAutomaticSubscription",
table: "Forums",
nullable: false,
defaultValue: false);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsAutomaticSubscription",
table: "Forums");
}
}
}
......@@ -195,6 +195,9 @@ namespace Tsi1.DataLayer.Migrations
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<bool>("IsAutomaticSubscription")
.HasColumnType("boolean");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment