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

course templates

parent 9db36006
No related branches found
No related tags found
2 merge requests!26Develop,!24Course template
Showing
with 762 additions and 21 deletions
......@@ -170,10 +170,25 @@ namespace Tsi1.Api.Controllers
return Ok(result.Data);
}
[Authorize(Roles = UserTypes.FacultyAdmin)]
[HttpGet("GetAllTemplates")]
public async Task<IActionResult> GetAllTemplates()
{
var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
var result = await _courseService.GetAll(tenantId, true);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
[Authorize(Roles = UserTypes.Professor + ", " + UserTypes.FacultyAdmin)]
[HttpPut("Modify/{courseId}")]
public async Task<IActionResult> Modify(int courseId, CourseCreateDto courseDto)
public async Task<IActionResult> Modify(int courseId, CourseModifyDto courseDto)
{
var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
courseDto.TenantId = tenantId;
......@@ -238,5 +253,19 @@ namespace Tsi1.Api.Controllers
return Ok(result.Data);
}
[AllowAnonymous]
[Authorize(Roles = UserTypes.FacultyAdmin)]
[HttpPost("CreateFromTemplate")]
public async Task<IActionResult> CreateFromTemplate(CourseTemplateCreateDto courseTemplate)
{
var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
var result = await _courseService.CreateFromTemplate(courseTemplate, tenantId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
}
}
......@@ -8,6 +8,7 @@ namespace Tsi1.BusinessLayer.Dtos
public class CourseCreateDto
{
public string Name { get; set; }
public bool IsTemplate { get; set; }
[JsonIgnore]
public int TenantId { get; set; }
......
using System.Text.Json.Serialization;
namespace Tsi1.BusinessLayer.Dtos
{
public class CourseModifyDto
{
public string Name { get; set; }
public bool IsTemplate { get; set; }
[JsonIgnore]
public int TenantId { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class CourseTemplateCreateDto
{
public string Name { get; set; }
public int CourseTemplateId { get; set; }
}
}
......@@ -31,8 +31,9 @@ namespace Tsi1.BusinessLayer.Helpers
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}";
public const string CourseDoesNotExist = "El curso '{0}' no existe";
public const string CourseDoesNotExist = "El curso con id '{0}' no existe";
public const string DuplicateCourseName = "Ya existe un curso con nombre '{0}'";
public const string CourseIsTemplate = "El curso con id '{0}' es un template";
public const string TenantDoesNotExist = "La Facultad '{0}' no existe";
public const string DuplicateTenantName = "Ya existe una Facultad con nombre '{0}'";
......
......@@ -72,6 +72,13 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<SectionDetailDto, Section>();
CreateMap<SectionItemDetailDto, SectionItem>();
CreateMap<FileDetailDto, File>();
CreateMap<Course, Course>().ForMember(x => x.Id, opt => opt.Ignore());
CreateMap<Tenant, Tenant>().ForMember(x => x.Id, opt => opt.Ignore());
CreateMap<Section, Section>().ForMember(x => x.Id, opt => opt.Ignore());
CreateMap<SectionItem, SectionItem>().ForMember(x => x.Id, opt => opt.Ignore());
CreateMap<SectionItemType, SectionItemType>().ForMember(x => x.Id, opt => opt.Ignore());
CreateMap<Forum, Forum>().ForMember(x => x.Id, opt => opt.Ignore());
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
......@@ -13,14 +11,15 @@ namespace Tsi1.BusinessLayer.Interfaces
Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(int userId, string userType);
Task<ServiceResult<Course>> Create(CourseCreateDto newCourse);
Task<ServiceResult<bool>> CreateFromTemplate(CourseTemplateCreateDto courseTemplate, int tenantId);
Task<ServiceResult<bool>> Matriculate(int userId, int courseId);
Task<ServiceResult<bool>> AddProfessorToCourse(ProfessorCourseDto professorCourseDto);
Task<ServiceResult<List<CoursePreviewDto>>> GetAll(int tenantId);
Task<ServiceResult<List<CoursePreviewDto>>> GetAll(int tenantId, bool isTemplate = false);
Task<ServiceResult<bool>> Modify(int courseId, CourseCreateDto courseDto);
Task<ServiceResult<bool>> Modify(int courseId, CourseModifyDto courseDto);
Task<ServiceResult<Course>> Delete(int courseId);
......
......@@ -97,6 +97,7 @@ namespace Tsi1.BusinessLayer.Services
if (user == null || user.Student == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
......@@ -105,10 +106,18 @@ namespace Tsi1.BusinessLayer.Services
if (course == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId);
return result;
}
if (course.IsTemplate)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.CourseIsTemplate, courseId));
return result;
}
var existingStudentCourse = await _context.StudentCourses
.FirstOrDefaultAsync(x => x.StudentId == user.StudentId && x.CourseId == course.Id);
......@@ -144,6 +153,7 @@ namespace Tsi1.BusinessLayer.Services
if (user == null || user.Professor == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, user.Username);
return result;
}
......@@ -153,10 +163,18 @@ namespace Tsi1.BusinessLayer.Services
if (course == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.CourseDoesNotExist, professorCourseDto.CourseId);
return result;
}
if (course.IsTemplate)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.CourseIsTemplate, professorCourseDto.CourseId));
return result;
}
var existingProfessorCourse = await _context.ProfessorCourses
.FirstOrDefaultAsync(x => x.ProfessorId == user.ProfessorId && x.CourseId == course.Id);
......@@ -174,30 +192,28 @@ namespace Tsi1.BusinessLayer.Services
};
_context.ProfessorCourses.Add(professorCourse);
await _context.SaveChangesAsync();
result.Data = true;
return result;
}
public async Task<ServiceResult<List<CoursePreviewDto>>> GetAll(int tenantId)
public async Task<ServiceResult<List<CoursePreviewDto>>> GetAll(int tenantId, bool isTemplate = false)
{
var result = new ServiceResult<List<CoursePreviewDto>>();
var courses = await _context.Courses
.Where(x => x.TenantId == tenantId)
.Where(x => x.TenantId == tenantId
&& x.IsTemplate == isTemplate)
.ToListAsync();
var coursesDto = _mapper.Map<List<CoursePreviewDto>>(courses);
result.Data = coursesDto;
return result;
}
public async Task<ServiceResult<bool>> Modify(int courseId, CourseCreateDto courseDto)
public async Task<ServiceResult<bool>> Modify(int courseId, CourseModifyDto courseDto)
{
var result = new ServiceResult<bool>();
......@@ -210,12 +226,10 @@ namespace Tsi1.BusinessLayer.Services
return result;
}
_mapper.Map(courseDto, course);
_mapper.Map(courseDto, course);
await _context.SaveChangesAsync();
result.Data = true;
return result;
}
......@@ -233,11 +247,9 @@ namespace Tsi1.BusinessLayer.Services
}
_context.Courses.Remove(course);
await _context.SaveChangesAsync();
result.Data = course;
return result;
}
......@@ -316,11 +328,9 @@ namespace Tsi1.BusinessLayer.Services
}
_context.ProfessorCourses.Remove(professorCourse);
await _context.SaveChangesAsync();
result.Data = true;
return result;
}
......@@ -338,7 +348,6 @@ namespace Tsi1.BusinessLayer.Services
var userDtos = _mapper.Map<List<UserPreviewDto>>(users);
result.Data = userDtos;
return result;
}
......@@ -366,5 +375,34 @@ namespace Tsi1.BusinessLayer.Services
return result;
}
public async Task<ServiceResult<bool>> CreateFromTemplate(CourseTemplateCreateDto courseTemplate, int tenantId)
{
var result = new ServiceResult<bool>();
var course = await _context.Courses.AsNoTracking()
.Include(x => x.Sections)
.ThenInclude(x => x.SectionItems)
.ThenInclude(x => x.Forum)
.FirstOrDefaultAsync(x =>
x.TenantId == tenantId
&& x.IsTemplate
&& x.Id == courseTemplate.CourseTemplateId);
if (course == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, courseTemplate.CourseTemplateId));
return result;
}
var newCourse = _mapper.Map<Course>(course);
newCourse.Name = courseTemplate.Name;
_context.Add(newCourse);
await _context.SaveChangesAsync();
return result;
}
}
}
......@@ -16,6 +16,7 @@ namespace Tsi1.DataLayer.Entities
public int Id { get; set; }
public string Name { get; set; }
public int TenantId { get; set; }
public bool IsTemplate { get; set; }
public Tenant Tenant { get; set; }
public ICollection<StudentCourse> StudentCourses { get; set; }
......
......@@ -20,6 +20,9 @@ namespace Tsi1.DataLayer.EntityConfiguration
.IsRequired()
.HasColumnType("character varying(50)");
builder.Property(x => x.IsTemplate)
.IsRequired();
builder.HasOne(x => x.Tenant)
.WithMany(x => x.Courses)
.HasForeignKey(x => x.TenantId);
......
using Microsoft.EntityFrameworkCore.Migrations;
namespace Tsi1.DataLayer.Migrations
{
public partial class coursetemplate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsTemplate",
table: "Courses",
nullable: false,
defaultValue: false);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsTemplate",
table: "Courses");
}
}
}
......@@ -44,6 +44,9 @@ namespace Tsi1.DataLayer.Migrations
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<bool>("IsTemplate")
.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