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

add sections

parent 419ed9b5
No related branches found
No related tags found
2 merge requests!26Develop,!21course sections
Pipeline #10297 passed
Showing
with 929 additions and 31 deletions
......@@ -4,6 +4,8 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Interfaces;
namespace Tsi1.Api.Controllers
{
......@@ -11,5 +13,59 @@ namespace Tsi1.Api.Controllers
[ApiController]
public class SectionController : ControllerBase
{
private readonly ISectionService _sectionService;
public SectionController(ISectionService sectionService)
{
_sectionService = sectionService;
}
[HttpPost("Create")]
public async Task<IActionResult> Create(SectionCreateDto section)
{
var result = await _sectionService.Create(section);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
[HttpPut("Modify/{sectionId}")]
public async Task<IActionResult> Modify(int sectionId, string newName)
{
var result = await _sectionService.Modify(sectionId, newName);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
[HttpDelete("Delete/{sectionId}")]
public async Task<IActionResult> Delete(int sectionId)
{
var result = await _sectionService.Delete(sectionId);
if (result.HasError)
{
return NotFound(result.Message);
}
return Ok();
}
[HttpPost("ChangeOrder")]
public async Task<IActionResult> ChangeOrder(List<OrderDto> orderDtos)
{
var result = await _sectionService.OrderSections(orderDtos);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
}
}
......@@ -45,7 +45,7 @@ namespace Tsi1.Api.Controllers
}
[HttpPost("ChangeOrder")]
public async Task<IActionResult> ChangeOrder(List<SectionItemOrderDto> orderDtos)
public async Task<IActionResult> ChangeOrder(List<OrderDto> orderDtos)
{
var result = await _sectionItemService.OrderSectionItems(orderDtos);
if (result.HasError)
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
......@@ -9,8 +7,6 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
......@@ -80,6 +76,7 @@ namespace Tsi1.Api
services.AddScoped<IPostMessageService, PostMessageService>();
services.AddScoped<ITenantService, TenantService>();
services.AddScoped<IFileService, FileService>();
services.AddScoped<ISectionService, SectionService>();
services.AddScoped<ISectionItemService, SectionItemService>();
services.Configure<MailSettings>(Configuration.GetSection("MailSettings"));
......
......@@ -4,9 +4,9 @@ using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class SectionItemOrderDto
public class OrderDto
{
public int SectionItemId { get; set; }
public int Id { get; set; }
public int Order { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class SectionCreateDto
{
public int CourseId { get; set; }
public string Name { get; set; }
public int Order { get; set; }
}
}
......@@ -45,6 +45,7 @@ namespace Tsi1.BusinessLayer.Helpers
public const string FileDoesNotExist = "El archivo '{0}' no existe fisicamente en el file server";
public const string SectionDoesNotExist = "La seccion con id '{0}' no existe";
public const string DuplicateSectionOrder = "Hay secciones con el mismo orden";
public const string SectionItemDoesNotExist = "El item de seccion con id '{0}' no existe";
public const string DuplicateSectionItemOrder = "Hay items de seccion con el mismo orden";
......
......@@ -33,6 +33,7 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<UserType, UserTypeDto>();
CreateMap<File, FileDto>();
CreateMap<SectionItem, SectionItemCreateDto>();
CreateMap<Section, SectionCreateDto>();
CreateMap<ForumCreateDto, Forum>();
CreateMap<ForumPreviewDto, Forum>();
......@@ -55,6 +56,7 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<UserTypeDto, UserType>();
CreateMap<FileDto, File>();
CreateMap<SectionItemCreateDto, SectionItem>();
CreateMap<SectionCreateDto, Section>();
}
}
}
......@@ -11,6 +11,6 @@ namespace Tsi1.BusinessLayer.Interfaces
{
Task<ServiceResult<bool>> Create(SectionItemCreateDto newSectionItem);
Task<ServiceResult<bool>> Delete(int sectionItemId);
Task<ServiceResult<bool>> OrderSectionItems(List<SectionItemOrderDto> orderDtos);
Task<ServiceResult<bool>> OrderSectionItems(List<OrderDto> orderDtos);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
namespace Tsi1.BusinessLayer.Interfaces
{
public interface ISectionService
{
Task<ServiceResult<bool>> Create(SectionCreateDto newSection);
Task<ServiceResult<bool>> Delete(int sectionId);
Task<ServiceResult<bool>> OrderSections(List<OrderDto> orderDtos);
Task<ServiceResult<bool>> Modify(int sectionId, string name);
}
}
......@@ -62,11 +62,11 @@ namespace Tsi1.BusinessLayer.Services
return result;
}
public async Task<ServiceResult<bool>> OrderSectionItems(List<SectionItemOrderDto> orderDtos)
public async Task<ServiceResult<bool>> OrderSectionItems(List<OrderDto> orderDtos)
{
var result = new ServiceResult<bool>();
var sectionItemsIds = orderDtos.Select(x => x.SectionItemId);
var sectionItemIds = orderDtos.Select(x => x.Id);
var orders = orderDtos.Select(x => x.Order).Distinct();
if (orders.Count() != orderDtos.Count())
......@@ -77,16 +77,16 @@ namespace Tsi1.BusinessLayer.Services
}
var sectionItems = await _context.SectionItems
.Where(x => sectionItemsIds.Contains(x.Id))
.Where(x => sectionItemIds.Contains(x.Id))
.ToListAsync();
foreach (var orderDto in orderDtos)
{
var sectionItem = sectionItems.FirstOrDefault(x => x.Id == orderDto.SectionItemId);
var sectionItem = sectionItems.FirstOrDefault(x => x.Id == orderDto.Id);
if (sectionItem == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.SectionItemDoesNotExist, orderDto.SectionItemId));
result.AddMessage(string.Format(ErrorMessages.SectionItemDoesNotExist, orderDto.Id));
}
sectionItem.Order = orderDto.Order;
......
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Services
{
public class SectionService : ISectionService
{
private readonly Tsi1Context _context;
private readonly IMapper _mapper;
public SectionService(Tsi1Context context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<ServiceResult<bool>> Create(SectionCreateDto newSection)
{
var result = new ServiceResult<bool>();
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == newSection.CourseId);
if (course == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, newSection.CourseId));
return result;
}
var section = _mapper.Map<Section>(newSection);
_context.Sections.Add(section);
await _context.SaveChangesAsync();
return result;
}
public async Task<ServiceResult<bool>> Delete(int sectionId)
{
var result = new ServiceResult<bool>();
var section = await _context.Sections.FirstOrDefaultAsync(x => x.Id == sectionId);
if (section == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.SectionDoesNotExist, sectionId));
return result;
}
_context.Sections.Remove(section);
await _context.SaveChangesAsync();
return result;
}
public async Task<ServiceResult<bool>> Modify(int sectionId, string name)
{
var result = new ServiceResult<bool>();
var section = await _context.Sections.FirstOrDefaultAsync(x => x.Id == sectionId);
if (section == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.SectionDoesNotExist, sectionId));
return result;
}
section.Name = name;
await _context.SaveChangesAsync();
return result;
}
public async Task<ServiceResult<bool>> OrderSections(List<OrderDto> orderDtos)
{
var result = new ServiceResult<bool>();
var sectionIds = orderDtos.Select(x => x.Id);
var orders = orderDtos.Select(x => x.Order).Distinct();
if (orders.Count() != orderDtos.Count())
{
result.HasError = true;
result.AddMessage(ErrorMessages.DuplicateSectionOrder);
return result;
}
var sections = await _context.Sections
.Where(x => sectionIds.Contains(x.Id))
.ToListAsync();
foreach (var orderDto in orderDtos)
{
var section = sections.FirstOrDefault(x => x.Id == orderDto.Id);
if (section == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.SectionDoesNotExist, orderDto.Id));
}
section.Order = orderDto.Order;
}
if (!result.HasError)
{
await _context.SaveChangesAsync();
}
return result;
}
}
}
......@@ -10,7 +10,7 @@ namespace Tsi1.DataLayer.Entities
{
StudentCourses = new HashSet<StudentCourse>();
ProfessorCourses = new HashSet<ProfessorCourse>();
Forums = new HashSet<Forum>();
Sections = new HashSet<Section>();
}
public int Id { get; set; }
......@@ -20,6 +20,6 @@ namespace Tsi1.DataLayer.Entities
public Tenant Tenant { get; set; }
public ICollection<StudentCourse> StudentCourses { get; set; }
public ICollection<ProfessorCourse> ProfessorCourses { get; set; }
public ICollection<Forum> Forums { get; set; }
public ICollection<Section> Sections { get; set; }
}
}
......@@ -12,8 +12,11 @@ namespace Tsi1.DataLayer.Entities
}
public int Id { get; set; }
public int CourseId { get; set; }
public string Name { get; set; }
public int Order { get; set; }
public Course Course { get; set; }
public ICollection<SectionItem> SectionItems { get; set; }
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Tsi1.DataLayer.Entities;
namespace Tsi1.DataLayer.EntityConfiguration
......
......@@ -19,6 +19,10 @@ namespace Tsi1.DataLayer.EntityConfiguration
builder.Property(x => x.Order)
.IsRequired();
builder.HasOne(x => x.Course)
.WithMany(x => x.Sections)
.HasForeignKey(x => x.CourseId);
}
}
}
This diff is collapsed.
using Microsoft.EntityFrameworkCore.Migrations;
namespace Tsi1.DataLayer.Migrations
{
public partial class sections : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Forums_Courses_CourseId",
table: "Forums");
migrationBuilder.DropIndex(
name: "IX_Forums_CourseId",
table: "Forums");
migrationBuilder.DropColumn(
name: "CourseId",
table: "Forums");
migrationBuilder.AddColumn<int>(
name: "CourseId",
table: "Sections",
nullable: false,
defaultValue: 0);
migrationBuilder.CreateIndex(
name: "IX_Sections_CourseId",
table: "Sections",
column: "CourseId");
migrationBuilder.AddForeignKey(
name: "FK_Sections_Courses_CourseId",
table: "Sections",
column: "CourseId",
principalTable: "Courses",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Sections_Courses_CourseId",
table: "Sections");
migrationBuilder.DropIndex(
name: "IX_Sections_CourseId",
table: "Sections");
migrationBuilder.DropColumn(
name: "CourseId",
table: "Sections");
migrationBuilder.AddColumn<int>(
name: "CourseId",
table: "Forums",
type: "integer",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Forums_CourseId",
table: "Forums",
column: "CourseId");
migrationBuilder.AddForeignKey(
name: "FK_Forums_Courses_CourseId",
table: "Forums",
column: "CourseId",
principalTable: "Courses",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
}
}
......@@ -94,9 +94,6 @@ namespace Tsi1.DataLayer.Migrations
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int?>("CourseId")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
......@@ -106,8 +103,6 @@ namespace Tsi1.DataLayer.Migrations
b.HasKey("Id");
b.HasIndex("CourseId");
b.ToTable("Forums");
});
......@@ -241,6 +236,9 @@ namespace Tsi1.DataLayer.Migrations
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("CourseId")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(255)");
......@@ -250,6 +248,8 @@ namespace Tsi1.DataLayer.Migrations
b.HasKey("Id");
b.HasIndex("CourseId");
b.ToTable("Sections");
});
......@@ -459,13 +459,6 @@ namespace Tsi1.DataLayer.Migrations
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Course", null)
.WithMany("Forums")
.HasForeignKey("CourseId");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum")
......@@ -535,6 +528,15 @@ namespace Tsi1.DataLayer.Migrations
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Section", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
.WithMany("Sections")
.HasForeignKey("CourseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.SectionItem", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.File", "File")
......
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